Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic where clause with for all entries?

0 Kudos
733

Hello Experts.

I am trying to create a simple dynamic where clause with the addition of for all entries, but I am new to the concept and kind of stuck.

    LOOP AT c_tab ASSIGNING FIELD-SYMBOL(<fs>).

              IF i_fieldname = 'MATNR'.
                SELECT COUNT( * )
                FROM (select_table)
                FOR ALL ENTRIES IN @i_data
                WHERE matnr = @i_data-data. "Make this matnr dynamic

                IF sy-subrc EQ 0 AND sy-dbcnt >= 1.
                  <fs>-num_entries = sy-dbcnt.
                ELSE.
                  DELETE c_tab INDEX tabix.
                ENDIF.

              ELSE.

                SELECT COUNT( * )
                 FROM (select_table)
                 FOR ALL ENTRIES IN @i_data
                 WHERE artnr = @i_data-data. "Dynamic

                IF sy-subrc EQ 0 AND sy-dbcnt >= 1.
                  <fs>-num_entries = sy-dbcnt.
                ELSE.
                  DELETE c_tab INDEX tabix.
                ENDIF.

              ENDIF.
ENDLOOP.

Basically I want to expand the matnr/artnr functionality. I wish to be able to select for any field provided by the user and for that I need a dynamic where clause. Inside i_data I hold the values the user provides. I also need to enchant that structure, so the type there is dynamic too, but I'll work on that seperately.
How can I achieve this?
Thank you

4 REPLIES 4

thkolz
Contributor
0 Kudos
618

I guess you can't use a table of type ANY TABLE as you need to know the field(s) in the WHERE statement.

You could use a stringtab to achieve it:

DATA:
  t_stringtab TYPE stringtab.

PARAMETERS:
  p_table TYPE tablenam.

SELECT COUNT( * )
  FROM (p_table)
  FOR ALL ENTRIES IN @t_stringtab
  WHERE matnr = @t_stringtab-table_line.

Then you just need to fill t_stringtab dynamically before performing the SELECT.

raymond_giuseppi
Active Contributor
0 Kudos
618

Did you already try to create such a simple text (must be character-like data object or a standard table) to use in dynamic WHERE clause ?

Sandra_Rossi
Active Contributor
0 Kudos
618

Methodology to develop fast:

  1. Do it the static way and make sure it works fine.
  2. Change to dynamic

Sandra_Rossi
Active Contributor
618

enchant -> enhance 😉