Multiple condition in SAP Query

I use 5 parameters to retrieve data from a 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 extract data from the MARA table using the select query . In this case, I must use the WHERE clause to get a record of a specific one . But I got confused about the conditions. We can check which parameter matters using the INITIAL clause . But there is a possibility that there are values ​​for parameters 2/3/4/5. For each case, should we write a selection request (if this leads to a problem with perforation) or is there a way to use the dynamic part of the condition in the selected request?

Thanks in advance.

Dhivya.

+3
source share
2 answers

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 this, the selection screen allows you to use multiple values ​​and ranges for the data.

If you need single values ​​such as parameter-command, you must set additional options for SELECT-OPTION:

  • NO INTERVALSto allow only single values
  • NO-EXTENSIONto allow only one value.
  • OBLIGATORYif an empty value is unacceptable (as far as I understand your question, you have the opposite situation, so you do not need it).

So your choice:

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.

Note:

  • MARAshould be defined as TABLEif you useSELECT-OPTIONS
  • Do you really need MATCHCODE OBJECT? Usually usage FORalready defines the correct matchcode object (via data item / domain).

Denial of responsibility:

  • SAP-, . - .
+4

, - select-options. select-option in .
, , ( , ), WHERE . :

tables: mara.
select-options number for mara-matnr matchcode object mat1 no-extension no intervals.
select-options type for mara-mtart matchcode object h_t134 no-extension no intervals.
select-options sector for mara-mbrsh matchcode object h_t137 no-extension no intervals.
select-options group for mara-matkl matchcode object h_t023 no-extension no intervals.
select-options unit for mara-meins matchcode object h_t006 no-extension no intervals.

select distinct mara~matnr makt~maktx marc~werks mard~lgort into table ta_materials
  from mara
  inner join makt on makt~matnr = mara~matnr
  inner join marc on marc~matnr = mara~matnr
  inner join mard on mard~matnr = mara~matnr
  where makt~spras = sy-langu and
        mara~matnr in number and
        mara~mtart in type and
        mara~mbrsh in sector and
        mara~matkl in group and
        mara~meins in unit
  order by mara~matnr.

no-extension no intervals select ( ) .

+4

All Articles