‎2008 Jul 02 7:33 AM
Hi,
I have a large number of very similar select statements run on the same (custom) DDIC table. Each select statement uses a different field of the table in the where condition.
Table fields: FIELD1, FIELD2, FIELD3, ..., FIELDN
E.g.
SELECT * FROM ZTAB WHERE FIELD1 EQ SEARCH_KEY
...
ENDSELECT
SELECT * FROM ZTAB WHERE FIELD2 EQ SEARCH_KEY
...
ENDSELECT
SELECT * FROM ZTAB WHERE FIELD3 EQ SEARCH_KEY
...
ENDSELECT
SEARCH_KEY is a local text field. Rather than copying the identical code several times and changing the selection field in where each time, is there any method for dynamically determining the field at runtime? I.e. is there a way to replace these multiple select statements with a single select statement like:
SELECT * FROM ZTAB WHERE FIELD EQ SEARCH_KEY
...
ENDSELECT
where FIELD can be determined to be any of FIELD1, FIELD2,... FIELDN?
I want to put the select statement in a subroutine and just call it with whatever field is required. Is this possible? If so, how? Help is appreciated.
Regards
‎2008 Jul 02 8:42 AM
Hi
You create a sub routine and pass the field name and search key as input parameters and follow this code, here p1 is the searchkey and ws_fieldname is the fieldname
********************************************************************
parameters : p1 type vbak-vbeln.
data: ws_field_name(50) type c value 'vbeln = ',
itab1 like standard table of ws_field_name,
itab2 like line of itab1,
itab type standard table of vbak.
concatenate ws_field_name '''' p1 '''' into itab2.
append itab2 to itab1.
Select * from vbak
into corresponding fields of table itab
where (itab1).
********************************************************************
Reward if useful.
Divya
‎2008 Jul 02 7:43 AM
Hi,
Pls try:
DATA: str type string.
Concatenate 'FIELD1' 'EQ SEARCH_KEY'
into str.
SELECT * FROM ZTAB WHERE (str)
...
ENDSELECT
You can concatenate FieldX to str.
Regards,
Pole
‎2008 Jul 02 7:45 AM
Use dynamic fields :
SELECT * FROM ZTAB WHERE (FIELD) EQ SEARCH_KEY
Now (FIELD) can contain FIELD1 FIELD2 etc....
‎2008 Jul 02 10:24 AM
Azeem,
Thanks for your reply. Could you post some example code? Thanks.
Regards
‎2008 Jul 02 7:48 AM
Hi,
Try like this:
DATA: ws_where TYPE string. "Workarea for the where cond.
DATA: wtab LIKE TABLE OF ws_where. "Table for the where cond.
concatenate (FIELD1) '=' SEARCH_KEY into ws_where separated by space.
append ws_where to w_tab.
SELECT * FROM ZTAB WHERE (wtab) .
...
ENDSELECT
Regards,
Subramanian
‎2008 Jul 02 7:59 AM
hi,
you can do with the below way,
data : begin of itab occurs 0,
field(6),
end of itab.
data : field(5).
itab-field = field1.
append itab.
itab-field = field2.
append itab.
itab-field = field3.
append itab.
loop at itab.
field = itab-field.
SELECT * FROM ZTAB WHERE (FIELD) EQ SEARCH_KEY
...
ENDSELECT
endloop.
‎2008 Jul 02 8:42 AM
Hi
You create a sub routine and pass the field name and search key as input parameters and follow this code, here p1 is the searchkey and ws_fieldname is the fieldname
********************************************************************
parameters : p1 type vbak-vbeln.
data: ws_field_name(50) type c value 'vbeln = ',
itab1 like standard table of ws_field_name,
itab2 like line of itab1,
itab type standard table of vbak.
concatenate ws_field_name '''' p1 '''' into itab2.
append itab2 to itab1.
Select * from vbak
into corresponding fields of table itab
where (itab1).
********************************************************************
Reward if useful.
Divya