Search code examples
selectparametersabapopensql

Range table and IN not behaving properly


The program just selects everything if the carrid is ok even if it is not ok with the lt_spfli. And there aren't any entries with that carrid it gets runtime error. If I try with for all entries he just selects absolutely the entire SFLIGHT.

PARAMETERS: pa_airp TYPE S_FROMAIRP,
            pa_carid TYPE S_CARR_ID.
DATA: lt_spfli TYPE RANGE OF SPFLI,
      lt_sflight TYPE TABLE OF SFLIGHT.

SELECT CONNID FROM SPFLI
INTO TABLE lt_spfli
WHERE AIRPFROM = pa_airp.

SELECT * FROM SFLIGHT
INTO TABLE lt_sflight
WHERE CARRID = pa_carid AND CONNID in lt_spfli.

Solution

  • I just suppose, that you want every flight connection from a given airport...

    Notice, that a RANGE structure has two more fields in front of the actual "compare value". So selecting directly into it will result in a very gibberish table.

    Possible Solutions:

    Selecting with RANGE

    If you really want to use this temporary table, you can have a look at my answer here where I describe the way to fill RANGEs without any overhead. After this step, your current snippet will work the way to wanted it too. Just make sure, that it really has been filled or everything will be selected.


    Selecting with FOR ALL ENTRIES

    Before you use this variant you should make absolutely sure, that your specified data object is filled. Otherwise it will result in the same mess as the first solution. To do that, you could write:

    * select connid
    IF lt_spfli[] IS NOT INITIAL.
    *    select on SFLIGHT
    ELSE.
    *    no result
    ENDIF.
    

    Selecting with JOIN

    The "correct" approach in this case would be a JOIN like:

    SELECT t~* 
      FROM spfli AS i
      JOIN sflight AS t
        ON t~carrid = @pa_carid
       AND t~connid = i~connid
      INTO TABLE @DATA(li_conns)
      WHERE i~airpfrom = @pa_airp.