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

using table field name as a variable

Former Member
0 Likes
4,511

Hi ,

Is it possible to map some values to a field in a Ztable , if I have the field name of the Ztable in a variable.

For ex:

I have a ZTABLE -- which has some fields - ZF1 and ZF2.

In my code, I determine the field in which I want to put data at runtime. For ex, in my code i determine at runtime I want to update field ZF2 , but I have this field name in a variable <V_FIELDNAME>

So, Is it possible to do something as:

<V_FIELDNAME> = ZF2.

ZTABLE-<V_FIELDNAME> = <VARIABLE -SOME VALUE>.

and this value should actually maps to ZTABLE-ZF2.

If it is possible , how to do it ?

Thanks-

1 ACCEPTED SOLUTION
Read only

Tamas_Hoznek
Product and Topic Expert
Product and Topic Expert
0 Likes
1,753

Yes, it's possible using a dynamic ASSIGN via a field-symbol.

You could have something like this:

data: fieldname(20) type c,
          fname(10) type c.
field-symbols: <f>.

If [condition].
  fname = 'ZF1'.
else.
  fname = 'ZF2'.
endif.
concatenate 'ZTABLE-' fname into fieldname.

assign (fieldname) to <f>.

<f> = 'abc'.

This will result in the value of ZTABLE-ZF1 or ZTABLE-ZF2 to be 'abc', depending on the condition.

Look up the help for the ASSIGN statement - there are many variations and possibilities.

4 REPLIES 4
Read only

Tamas_Hoznek
Product and Topic Expert
Product and Topic Expert
0 Likes
1,754

Yes, it's possible using a dynamic ASSIGN via a field-symbol.

You could have something like this:

data: fieldname(20) type c,
          fname(10) type c.
field-symbols: <f>.

If [condition].
  fname = 'ZF1'.
else.
  fname = 'ZF2'.
endif.
concatenate 'ZTABLE-' fname into fieldname.

assign (fieldname) to <f>.

<f> = 'abc'.

This will result in the value of ZTABLE-ZF1 or ZTABLE-ZF2 to be 'abc', depending on the condition.

Look up the help for the ASSIGN statement - there are many variations and possibilities.

Read only

0 Likes
1,753

Hi Tamas,

Thanks a lot for ur post ! it did certainly help. However, I need some more help.

Here is what I need -

I will have a string coming up from an interface which would be something like : Val1;Val2;Val3

And in SAP R/3 I have a variable maintained as : Field1;Field2;Field3

Field1 Field2 and Field3 are fields of a ZTABLE.

So , at runtime, using the variable I have to determine which Value goes to which field and then insert this field in ZTABLE.

So, ultimately , code should perform the following actions:

ZTABLE-FIELD1 = Val1

ZTABLE-FIELD2 = Val2

ZTABLE-FIELD3 = Val3

Insert ZTABLE.

Going by code you provided, I can assign individual values, But then ultimately I need to map it to a structure which can be used to insert in a DB table. How can I do this?

Thanks-

Read only

Tamas_Hoznek
Product and Topic Expert
Product and Topic Expert
0 Likes
1,753

I'm not sure I understand exactly what the issue is... I mean, in the most simplistic example, if ZTABLE is your DB table and it was declared with TABLES, then using INSERT ZTABLE after assigning values to fields ZTABLE-FIELD1...-FIELD3 will just do what you need.

Or, if ZTABLE is not your DB table but a field string representing one record in a DB table, then using INSERT <dbtable> FROM ZTABLE will do the same.

Could you please explain if this isn't your goal?

Read only

Former Member
0 Likes
1,753

Hi,

Check this thread for Dynamic programming:

http://sap.ittoolbox.com/groups/technical-functional/sap-abap/abap-dynamic-programing-2354885

Regards,

Subramanian