Application Development and Automation 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: 
Read only

Field selection at runtime

Former Member
0 Likes
876

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
852

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

6 REPLIES 6
Read only

pole_li
Active Participant
0 Likes
852

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

Read only

Azeemquadri
Contributor
0 Likes
852

Use dynamic fields :

SELECT * FROM ZTAB WHERE (FIELD) EQ SEARCH_KEY

Now (FIELD) can contain FIELD1 FIELD2 etc....

Read only

0 Likes
852

Azeem,

Thanks for your reply. Could you post some example code? Thanks.

Regards

Read only

Former Member
0 Likes
852

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

Read only

Former Member
0 Likes
852

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.

Read only

Former Member
0 Likes
853

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