This article illustrates how to write a dynamic where clause in ABAP SELECT queries using the function module 'RH_DYNAMIC_WHERE_BUILD'.
CALL FUNCTION 'RH_DYNAMIC_WHERE_BUILD'
EXPORTING
dbtable = v_table_name
tables
condtab = t_condtab
where_clause = t_where_clause
EXCEPTIONS
EMPTY_CONDTAB = 1
NO_DB_FIELD = 2
UNKNOWN_DB = 3
WRONG_CONDITION = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
Input parameters
v_table_name = Table name
t_condtab = Internal table contains the fieldnames and their
values used in where clause.
Output parameters
t_where_clause = Output string which will be used along
with the 'WHERE' clause in Select query. This internal table contains one field
with type character.
SFLIGHT table is used in this example to fetch data using dynamic Select query. CARRID, CONNID and FLDATE are the 3 key fields used for fetching data from SFLIGHT (MANDT field is not considered in this example). The internal table t_condtab will be populated using these 3 fields and passed to the function module. Function module will return the internal table t_where_clause, which can be used along with the where clause in the Select query.
TYPES : BEGIN OF ty_where_clause,
line TYPE char72,
END OF ty_where_clause.
DATA : t_condtab TYPE STANDARD TABLE OF hrcond,
t_sflight TYPE STANDARD TABLE OF sflight,
t_where_clause TYPE STANDARD TABLE OF ty_where_clause.
PERFORM fill_condtab USING 'CARRID' 'EQ' 'AA'.
PERFORM fill_condtab USING 'CONNID' 'EQ' '17'.
PERFORM fill_condtab USING 'FLDATE' 'EQ' '20130724'.
CALL FUNCTION 'RH_DYNAMIC_WHERE_BUILD'
EXPORTING
dbtable = 'SFLIGHT'
TABLES
condtab = t_condtab
where_clause = t_where_clause
EXCEPTIONS
empty_condtab = 1
no_db_field = 2
unknown_db = 3
wrong_condition = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
SELECT *
FROM sflight
INTO TABLE t_sflight
WHERE (t_where_clause).
*&---------------------------------------------------------------------*
*& Form FILL_CONDTAB
*&---------------------------------------------------------------------*
FORM fill_condtab USING value(pv_field) TYPE dbfeld
value(pv_operator) TYPE char2
value(pv_low) TYPE hrvalue
DATA : lx_condtab TYPE hrcond.
lx_condtab-field = pv_field.
lx_condtab-opera = pv_operator.
lx_condtab-low = pv_low.
APPEND lx_condtab TO t_condtab.
ENDFORM.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 |