‎2009 Sep 10 10:51 PM
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-
‎2009 Sep 10 11:35 PM
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.
‎2009 Sep 10 11:35 PM
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.
‎2009 Sep 11 3:27 PM
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-
‎2009 Sep 11 3:54 PM
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?
‎2009 Sep 10 11:41 PM
Hi,
Check this thread for Dynamic programming:
http://sap.ittoolbox.com/groups/technical-functional/sap-abap/abap-dynamic-programing-2354885
Regards,
Subramanian