How to include multiple conditions in a SELECT?

Dhivya picture Dhivya · May 15, 2012 · Viewed 16.7k times · Source

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?

Answer

knut picture knut · May 15, 2012

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:

  • NO INTERVALS to allow only single values
  • NO-EXTENSION to allow only one value.
  • OBLIGATORY if an empty value is not allowed (As far I understand your question, you have the opposite situation, so you don't need it).

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:

  • MARA must be defined as a TABLE if you use SELECT-OPTIONS
  • Do you really need the MATCHCODE OBJECT? Normally the usage of the FOR already defines the correct matchcode object (via data element/domain).

Disclaimer:

  • I have no SAP-system available yet, so the code may contain syntax errors. - I will check it tomorrow.