Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

how to write select within select in ABAP

Hi Guys

I want to write a select statement in ABAP to select from 2 tables below is my code I want to write select within select statment in --->A and replace with ---> B

FORM sales_production_order USING p_vbeln TYPE vbeln_va

CHANGING p_aufnr TYPE aufnr

p_rtcod TYPE sysubrc.

CLEAR: p_rtcod,

p_aufnr.

              • A *******

SELECT aufnr FROM aufk INTO p_aufnr UP TO 1 ROWS

WHERE kdauf EQ p_vbeln .

                  • B *******

SELECT kdauf from aufk into p_aufnr UP TO 1 ROWS

WHERE KDAUF IN ( SELECT VBELN FROM VBAP where vbeln EQ p_vbeln) .

ENDSELECT.

p_rtcod = sy-subrc.

ENDFORM. " sales

T

Tags:
Former Member
Former Member replied

Hi Eslam,

In select statements, ENDSELECT must be added when single line returned without the addition 'SINGLE'.

The innner query in your code, returns a single record but with the addition 'UP TO 1 ROWS'. The outer query also like this only. But the outer query ended with 'ENDSELECT', not for inner query.

Else you can add 'SINGLE' keyword.


  SELECT aufnr FROM aufk INTO p_aufnr UP TO 1 ROWS
    WHERE kdauf EQ '10' .

    SELECT SINGLE kdauf from aufk into p_aufnr
    WHERE KDAUF IN ( SELECT VBELN FROM VBAP where vbeln EQ p_vbeln) .

  ENDSELECT.


  SELECT aufnr FROM aufk INTO p_aufnr UP TO 1 ROWS
    WHERE kdauf EQ '10' .

***** It assigns Nth record's KDAUF to p_aufnr provided N records satisfy the condition.
    SELECT  kdauf from aufk into p_aufnr UPTO 1 ROWS
    WHERE KDAUF IN ( SELECT VBELN FROM VBAP where vbeln EQ 'p_vbeln) .
    ENDSELECT.

  ENDSELECT.

Regards,

R.Nagarajan.

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question