Reputation: 163
I had a requirement in which I wanted to dynamically run this query. Has anyone have worked on this kind of query?
READ TABLE table_name TRANSPORTING feild_name INTO table_name
WITH KEY key_feild1 = value1,
key_feild2 = value2,
key_feild3 = value3.
Upvotes: 3
Views: 4231
Reputation: 10621
Here is one more possible solution, based on this answer. Though, it is a bit ugly, but it definitely works.
DATA: table_tab TYPE TABLE OF rsdstabs,
selid TYPE rsdynsel-selid,
cond_tab TYPE rsds_twhere,
field_tab TYPE TABLE OF rsdsfields,
string TYPE string.
FIELD-SYMBOLS <cond> LIKE LINE OF cond_tab.
table_tab = VALUE #( ( prim_tab = 'USR02') ).
CALL FUNCTION 'FREE_SELECTIONS_INIT'
EXPORTING
kind = 'T'
IMPORTING
selection_id = selid
TABLES
tables_tab = table_tab
EXCEPTIONS
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE 'Error in initialization' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDIF.
CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
EXPORTING
selection_id = selid
title = 'Free Selection'
as_window = ' '
IMPORTING
where_clauses = cond_tab
TABLES
fields_tab = field_tab
EXCEPTIONS
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE 'No free selection created' TYPE 'I'.
LEAVE PROGRAM.
ENDIF.
FIELD-SYMBOLS: <fld> TYPE any.
DATA: field1 TYPE c LENGTH 10, value1 TYPE string,
field2 TYPE c LENGTH 10, value2 TYPE string,
field3 TYPE c LENGTH 10, value3 TYPE string,
field4 TYPE c LENGTH 10, value4 TYPE string,
field5 TYPE c LENGTH 10, value5 TYPE string,
field6 TYPE c LENGTH 10, value6 TYPE string,
field7 TYPE c LENGTH 10, value7 TYPE string,
field8 TYPE c LENGTH 10, value8 TYPE string,
field9 TYPE c LENGTH 10, value9 TYPE string,
num1(1) TYPE n,
fldname TYPE fieldname,
valname TYPE fieldname.
ASSIGN cond_tab[ tablename = 'USR02' ] TO <cond>.
IF sy-subrc = 0.
LOOP AT <cond>-where_tab ASSIGNING FIELD-SYMBOL(<fs_cond>).
num1 = sy-tabix.
CONCATENATE 'field' num1 INTO fldname.
CONCATENATE 'value' num1 INTO valname.
ASSIGN (fldname) TO <fld>. "assigning field name
string = <fs_cond>.
REPLACE REGEX `(^.*\(\s)(.*)(EQ.*)` IN string WITH '$2'.
<fld> = string.
ASSIGN (valname) TO <fld>. "assigning value
string = <fs_cond>.
REPLACE REGEX `(.*EQ\s)'(.*)('\s*\))` IN string WITH '$2'.
<fld> = string.
ENDLOOP.
ENDIF.
SELECT *
FROM usr02
INTO TABLE @DATA(lt_usr).
READ TABLE lt_usr ASSIGNING FIELD-SYMBOL(<fs_usr>)
WITH KEY (field1) = value1
(field2) = value2
(field3) = value3
(field4) = value4
(field5) = value5
(field6) = value6
(field7) = value7
(field8) = value8
(field9) = value9.
The sample is built on USR02 table.
FREE_SELECTIONS_INIT
FM builds WHERE condition based on these fields.READ TABLE
statement template, empty fields are simply ignored and table is read only by populated keys. Upvotes: 2
Reputation: 1301
READ TABLE
has dynamic syntax when specifying secondary keys (here) and key components (here), but in a limited fashion.
I think LOOP AT
is the command you are looking for. More details about the usage and example here. Look at Part 4:
Dynamic WHERE Condition cond_syntax can be specified as a character-like data object or standard table with character-like row type
Dummy code:
LOOP AT table INTO line WHERE (condition).
EXIT. --> Exit at first found line.
ENDLOOP.
IF sy-subrc = 0.
...
ENDIF.
Upvotes: 4