Search code examples
selectabapopensql

How to use SELECT on an internal table?


I have an internal table that I want to filter further and further based on whether data is present. I have tried the following, but unfortunately I cannot apply a SELECT an internal table.

How can I solve this problem?

"Here I'm getting the hole database into my internal table 
SELECT * FROM TABLE
  INTO CORRESPONDING FIELDS OF TABLE @itab.

"This should be my first filter if iv_name is not initial
IF iv_name IS NOT INITIAL.
  SELECT * FROM itab
    WHERE NAME = @iv_name
    INTO CORRESPONDING FIELDS OF TABLE @itab. 
ENDIF.

"This should be my second filter if iv_age is not initial
IF iv_age IS NOT INITIAL.
  SELECT * FROM itab
    WHERE AGE = @iv_age
    INTO CORRESPONDING FIELDS OF TABLE @itab. 
ENDIF.

Solution

  • There are several ways in ABAP to achieve your goal. You can use the DELETE keyword to filter the data in an internal table:

    IF iv_name IS NOT INITIAL    
      DELETE itab WHERE name NE iv_name.
    ENDIF.
    

    Another possibility is to use the FILTER keyword, but the prerequisite is, that the internal table is TYPE SORTED or HASHED:

    itab = FILTER #( itab WHERE name = iv_name ).