Skip to Content
Author's profile photo Anson John Abraham

Writing dynamic where clause in ABAP Select query

This article illustrates how to write a dynamic where clause in ABAP SELECT queries using the function module ‘RH_DYNAMIC_WHERE_BUILD’.

Calling 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.

Populating t_condtab internal table

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.

Sample code for fetching data from SFLIGHT using dynamic where clause

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.  

Assigned tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sharath Yaralkattimath
      Sharath Yaralkattimath

      There is a similar function module 'CONVERT_SELECT_INTO_WHERE' which could serve the same purpose.

       

      Thanks,

      Sharath

      Author's profile photo Anson Abraham
      Anson Abraham
      Blog Post Author

      Thanks Sharath for sharing the FM

      Author's profile photo Former Member
      Former Member

      Thanks for sharing .. it was very useful

      Author's profile photo Anson Abraham
      Anson Abraham
      Blog Post Author

      Glad to know that it was useful to you.

      Author's profile photo Pascal Bayle
      Pascal Bayle

      Very useful ! Thank you for sharing that knowledge.

       

      Have a nice day.

      Author's profile photo Devansh Pandey
      Devansh Pandey

      As per my knowledge , this FM is not released for customer.So, its better to avoid the use of this FM.

      Author's profile photo Former Member
      Former Member

      You are right it the mentioned FMs are not released for customer. 'FREE_SELECTIONS_RANGE_2_WHERE' is released and can serve the same purpose may be.