Search code examples
abapopensql

Select from where in ( ) table?


I want a select with dynamic where conditions in ABAP Syntax.

An SQL Statement would look like this:

SELECT * FROM MCH1 WHERE MATNR IN (...) AND CHARG IN (...)

My approach was to add 2 structures ZMATN_STR and ZCHARG_STR to the dictionary with associated components as line (MATNR, CHARG). Then create 2 table types with associated line types.

Now im stucked in ABAP because I don't know how to write the where clause. That's what I have so far:

SELECT *
 FROM
  mch1
  FOR ALL ENTRIES IN @matnrs
 WHERE
    matnr = @matnrs-matnr
INTO TABLE @DATA(lt_result).

It works for either matnr or charg but not with both of them.

Additional Info

This select happens in a function module where 2 import parameter exists (the 2 table types) - so I cannot just write where in ('xxx', 'yyy')


Solution

  • data lr_matnr type range of matnr.
    data lr_charg type range of MCH1-charg.
    "Fill lr_matnr and lr_charg...
    SELECT * FROM MCH1 WHERE MATNR IN @lr_matnr AND CHARG IN @lr_charg
      INTO TABLE @data(lt_result).