Recommand · October 14, 2021 0

How can I write below statement into single Select Statement?

I have been learning abap recently and working on select operations but then I came across this question. How can I put these 2 select statements into single select statement?

SELECT * FROM SPFLI INTO SPFLI_WA.

      SELECT * FROM SFLIGHT INTO SFLIGHT_WA    
            WHERE CARRID = SPFLI_WA-CARRID    
             AND CONNID = SPFLI_WA-CONNID.

      ENDSELECT.

ENDSELECT.

You can use inner join for getting matching records from other table.

SELECT SFLIGHT~*
  INTO @SFLIGHT_WA
  FROM SFLIGHT
 INNER JOIN SPFLI ON SFLIGHT~CARRID = SPFLI~CARRID
                   AND SFLIGHT~CONNID = SPFLI~CONNID.

ENDSELECT.

Here is an extension of correct answer by mkysoft, just for the fun, to mimic exactly the original code ; it works from ABAP 7.40:

SELECT *
      INTO @DATA(spfli_sflight_wa)
      FROM spfli
      INNER JOIN sflight ON sflight~carrid = spfli~carrid
                        AND sflight~connid = spfli~connid.

  DATA(spfli_wa) = spfli_sflight_wa-spfli.
  DATA(sflight_wa) = spfli_sflight_wa-sflight.

  ...

ENDSELECT.