where-clauseabapopensqlsap-selection-screens

How to include multiple conditions in a SELECT?


I am using 5 Parameters to fetch the details from DB table (mara, makt, marc, mard).

 PARAMETERS :number TYPE matnr MATCHCODE OBJECT MAT1 ,
             type TYPE MTART MATCHCODE OBJECT H_T134 ,
             sector TYPE MBRSH MATCHCODE OBJECT H_T137 ,
             group TYPE MATKL MATCHCODE OBJECT H_T023 ,
             unit TYPE MEINS MATCHCODE OBJECT H_T006 .

First I tried to fetch data from MARA table using the select query. In that to retrieve the particulare record, I have to use the WHERE condition. But I get confused in the condition part. We can check which parameter has value by using INITIAL condition.

But there is a chanced for 2/3/4/5 parameters to have values. For each case we have to write select query (if so it will lead to performance issue) or is there any way for using dynamic condition part in select query?


Solution

  • You can use SELECT-OPTIONS:

    TABLES MARA.  
    
    SELECT-OPTIONS:
      s_matnr FOR mara-matnr MATCHCODE OBJECT MAT1 ,
      s_mtart FOR mara-MTART MATCHCODE OBJECT H_T134 ,
      s_mbrsh FOR mara-MBRSH MATCHCODE OBJECT H_T137 ,
      s_matkl FOR mara-MATKL MATCHCODE OBJECT H_T023 ,
      s_meins FOR mara-MEINS MATCHCODE OBJECT H_T006 .
    
    * [...]  
    
    SELECT * FROM MARA where 
      matnr in s_matnr and
      mtart in s_mtart and
      mbrsh in s_mbrsh and
      matkl in s_matkl and
      meins in s_meins.
    

    When you do so, you selection screen will allow multiple values and ranges for the data.

    If you need single values like the parameter-command, you must set aditional options for the SELECT-OPTION:

    So your selection is:

    SELECT-OPTIONS:
      s_matnr FOR mara-matnr NO-EXTENSION NO INTERVALS,
      s_mtart FOR mara-MTART NO-EXTENSION NO INTERVALS,
      s_mbrsh FOR mara-MBRSH NO-EXTENSION NO INTERVALS,
      s_matkl FOR mara-MATKL NO-EXTENSION NO INTERVALS,
      s_meins FOR mara-MEINS NO-EXTENSION NO INTERVALS.
    

    Remark:

    Disclaimer: