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

Dynamic SQL with field symbol

tim82
Explorer
1,286

Hi experts,

my requirement seems a little weird, but maybe you can help me.

We have a custom db-table, which comes with two colomns: table and field.

In this table (ZB01), the customer has values like KNA1 & NAME1, or KNVV & AUFSD.

The customer number is always known.

I would like to know, if it is possible to create a dynamic select with dynamic output like:

DATA: LT_ZB01 type STANDARD TABLE OF zb01.

Select * from zb01 into TABLE lt_zb01.

loop at lt_zb01 ASSIGNING FIELD-SYMBOL(<zb01>).

Select single (<zb01>-FIELD) from (<zb01>-TABLE)
into @data(lv_var)
where
kunnr = '0000030047'.
ENDLOOP.

In this custom table, there could be any table and table-field coming to have being selected.

I have tried with creation of "type ref to data" and @data(var) as output, but nothing works.

Is this possible? (We are in a S/4HANA environment)


Thanks in advance

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
1,192

You have to create the data object dynamically:

DATA dref_field TYPE REF TO data.
FIELD-SYMBOLS <field> TYPE ANY.

CREATE DATA dref_field TYPE (<zb01>-field).
ASSIGN dref_field->* TO <field>.

Select single (<zb01>-FIELD) 
    from (<zb01>-TABLE)
    into @<field>
    where kunnr = '0000030047'.

Of course, handle all exception situations like field is not valid...

4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,192

Please use the CODE button when you post code.

Read only

Sandra_Rossi
Active Contributor
1,193

You have to create the data object dynamically:

DATA dref_field TYPE REF TO data.
FIELD-SYMBOLS <field> TYPE ANY.

CREATE DATA dref_field TYPE (<zb01>-field).
ASSIGN dref_field->* TO <field>.

Select single (<zb01>-FIELD) 
    from (<zb01>-TABLE)
    into @<field>
    where kunnr = '0000030047'.

Of course, handle all exception situations like field is not valid...

Read only

tim82
Explorer
0 Likes
1,192

Thank your very much, this works!

Read only

1,192

Don't tag your own answer as the correct one, tag sandra.rossi's one.