In very few developments we need Dynamic Where Condition for data base (DB) queries. Meaning; during runtime your program may decide which all fields of the data base table to be used for the DB query. We can very well write our custom code for developing those Dynamic Where Condition. But we know the effort and pain it takes to write such a code or else we can use a Standard SAP function module CRS_CREATE_WHERE_CONDITION to achieve the requirement.
Sample Example:
Lets consider, we have a program which will follow some logic and finally it comes to the conclusion that it will have to do a query on Data Base table MARA based on the fields MATNR (Material Number) and MTART (Material Type). There could be some other possible fields based on which the query can be made and this will be decided at run time. The table fields and their corresponding values can not be know until run time . So we need a Dynamic Where Condition for the Data Base Query. In such a condition you may choose to explore all possible combinations of fields to be used in the where condition and write your custom code to manufacture a Dynamic Where Condition or you can just use a Function Module (FM) which will do the job you.
We will discuss the above situation considering a program, with a selection screen having both the fields MATNR and MTART. (Let us assume these two fields and their corresponding values will be not be available until runtime).
REPORT z_dynamic_where_condition.
* Types
TYPES: BEGIN OF x_mara,
matnr TYPE matnr, "Material Number
mtart TYPE mtart, "Material Type
END OF x_mara.
DATA:
* Internal table
l_i_range TYPE STANDARD TABLE OF crmselstr,
l_i_output TYPE STANDARD TABLE OF mcondition,
l_i_mara TYPE STANDARD TABLE OF x_mara,
* Work area
l_wa_range TYPE crmselstr,
* Variable
v_matnr TYPE mara-matnr,
v_mtart TYPE mara-mtart.
* Selection Screen
SELECT-OPTIONS: s_matnr FOR v_matnr,
s_mtart FOR v_mtart.
* For Select Option 1
LOOP AT s_matnr. "Looping to get multiple (single) values
l_wa_range-table = 'MARA'. "Name of the DB table
l_wa_range-field = 'MATNR'. "Field name the user has selected
l_wa_range-sign = s_matnr-sign. "Sign
l_wa_range-option = s_matnr-option."option
l_wa_range-low = s_matnr-low. "Lower Value
l_wa_range-high = s_matnr-high. "Higher Value
APPEND l_wa_range TO l_i_range.
ENDLOOP..
* For Select Option 2
LOOP AT s_mtart. "Looping to get multiple (single) values
CLEAR l_wa_range.
l_wa_range-table = 'MARA'. "Name of the DB table
l_wa_range-field = 'MTART'. "Field name the user has selected
l_wa_range-sign = s_mtart-sign. "Sign
l_wa_range-option = s_mtart-option. "option
l_wa_range-low = s_mtart-low. "Lower Value
l_wa_range-high = s_mtart-high. "Higher Value
APPEND l_wa_range TO l_i_range.
ENDLOOP.
IF NOT l_i_range[] IS INITIAL.
* Call the FM to create the Dynamic Where condition
CALL FUNCTION 'CRS_CREATE_WHERE_CONDITION'
TABLES
ti_range = l_i_range
to_cond = l_i_output
EXCEPTIONS
invalid_input = 1
OTHERS = 2.
IF sy-subrc = 0.
* Special way to write the query
SELECT matnr "Material Number
mtart "Material Type
FROM mara
INTO TABLE l_i_mara
WHERE (l_i_output).
IF sy-subrc = 0.
ENDIF.
ENDIF. "IF sy-subrc = 0: SELECT matnr mtart
ENDIF. "IF NOT l_i_range[] IS INITIAL.
******************************************************************
** You may need an Dynamic Internal table in such a scenario.
So find here the link for the same **
http://help-sap.blogspot.com/search/label/Internal%20table
******************************************************************