2013 Apr 08 7:18 PM
Hi Experts,
I need to dynamically update a field in a internal table from another internal table. The field and table name will be dynamic. Below is the code I am trying to run. I do not want classes or ref to.
I want the fname (fieldname) and value_new from t_input and update the internal table t_KNA1 according to the field and value selected from t_input.
Below is the code.
t_input :
tabname type cdpos-tabname (. Table Name)
fname type cdpos-fname (field name)
value_new type cdpos-value)_new.
field-symbols : <fs_table> type any,
<fs_fld> type any,
<fs_val> type any.
assign t_input to <fs_table>.
LOOP AT t_input." ASSIGNING <fs_table>.
CASE t_input-tabname.
WHEN 'KNA1'.
select * from kna1 into table t_kna1 where kunnr = t_input-objectid. (kunnr)
* <fs_fld> = t_input-fname.
ASSIGN COMPONENT t_input-fname of STRUCTURE <fs_table> to <fs_val>.
move t_input-value_new to <fs_val>.
* modify TABLE t_kna1 from <fs_table>.
WHEN 'KNVV'.
ENDCASE.
ENDLOOP.
Thanks
Rahul
2013 Apr 09 7:46 AM
hi rahul,
field-symbols : <fs_wa> type any,
<fs_fld> type any,
<fs_val> type any,
<fs_objectid> type any,
<fs_tabname> type any.
LOOP AT t_input ASSIGNING <fs_wa>.
assign component 'tabname' of structure <fs_wa> to <fs_tabname>.
CASE <fs_tabname>.
WHEN 'KNA1'.
assign component 'objectid' of structure <fs_wa> to <fs_objectid>.
select * from kna1 into table t_kna1 where kunnr = <fs_objectid>.
ASSIGN COMPONENT 'fname' of STRUCTURE <fs_wa> to <fs_val>.
assign component 'value' for structure <fs_wa> to <fs_value>.
move <fs_value> to <fs_val>.
modify TABLE t_kna1 from <fs_wa>.
WHEN 'KNVV'.
ENDCASE.
ENDLOOP.
I am assuming that t_input is a table and is a importing parameter.....rest all modification is based on your code.........
thanks and regards,
narayan
2013 Apr 09 5:55 AM
Hi Rahul,
Try the Following
How can you directly refer t_input inside the LOOP? You need to get Row of an internal table into a workarea and then refer the fields,
Inyour case t_input-tabname is just not possible as t_input is a table which may contain many rows, so you have to get Row by Row into a workarea and then access value
You could do this,
Loop t_input Assigning <fs_row>.
Now <fs_row> has the you specific row, but here you cant directle use <fs_row>-tabname, this will give you compile time error as <fs_row> is of Type ANY so directly specifying fieldname is not possible, solution for this is
Data lv_fieldname TYPE STRING VALUE 'tabname'.
Loop t_input Assigning <fs_row>.
<fs_row>-(lv_fieldname) = some value.
ENDLOOP.
Hope this was your problem
2013 Apr 09 7:46 AM
hi rahul,
field-symbols : <fs_wa> type any,
<fs_fld> type any,
<fs_val> type any,
<fs_objectid> type any,
<fs_tabname> type any.
LOOP AT t_input ASSIGNING <fs_wa>.
assign component 'tabname' of structure <fs_wa> to <fs_tabname>.
CASE <fs_tabname>.
WHEN 'KNA1'.
assign component 'objectid' of structure <fs_wa> to <fs_objectid>.
select * from kna1 into table t_kna1 where kunnr = <fs_objectid>.
ASSIGN COMPONENT 'fname' of STRUCTURE <fs_wa> to <fs_val>.
assign component 'value' for structure <fs_wa> to <fs_value>.
move <fs_value> to <fs_val>.
modify TABLE t_kna1 from <fs_wa>.
WHEN 'KNVV'.
ENDCASE.
ENDLOOP.
I am assuming that t_input is a table and is a importing parameter.....rest all modification is based on your code.........
thanks and regards,
narayan
2013 Apr 09 12:13 PM
Hi Guys,
Thanks for ur replies.. but the problem still remains.. Let me try to give you a example of what I want.
we have two internal tables. t_input and t_kna1.
t_kna1 is kna1.
based on the fname (fieldname) from t_input and value_new we have to select the same fieldname from KNA1 and update it with Value_new.
suppose :
t_input :
kunnr fname value_new.
1 land1 US
2 pstlz 12345
now for internal table KNA1 2 records should be created
t_KNA1
KUNNR LAND1 PSTLZ ORT01 etc etc
1 US
2 12345
Thanks
2013 Apr 09 1:13 PM
Hi Rahul,
Create a Work Area for t_kna1 as ls_kna1.
<fs_wa> is to store record wise detail
<fs_fname> is to store field name
<fs_val_new> is for New Value
ls_kna1 is workarea for t_kna1 table field
Do this coding inside your when 'KNA1' block
Loop at t_input ASSIGNING <fs_wa>.
ASSIGN COMPONENT 'fname' OF STRUCTURE <fs_wa> TO <fs_fname>.
ASSIGN COMPONENT 'value_new' OF STRUCTURE <fs_wa> TO <fs_val_new>.
ASSIGN COMPONENT <fs_fname> OF STRUCTURE ls_kna1 TO <fs_kna1>.
<fs_kna1> = <fs_val_new>.
append ls_kna1 TO t_kna1.
ENDLOOP.
2013 Apr 09 1:19 PM
Hi Rahul,
Try using below code... I hope it would be useful for u..
TYPES: BEGIN OF ty,
kunnr TYPE kna1-kunnr,
fname TYPE char30,
fvalue TYPE char80,
END OF ty.
DATA: t_input TYPE TABLE OF ty,
s_input TYPE ty,
t_kna1 TYPE table of kna1.
FIELD-SYMBOLS: <fs_kna1> TYPE kna1,
<fs> TYPE ANY.
*** i have taken 2 customer for example***
CLEAR t_input.
s_input-kunnr = '0000001390'.
s_input-fname = 'LAND1'.
s_input-fvalue = 'US'.
APPEND s_input TO t_input.
s_input-kunnr = '0000001400'.
s_input-fname = 'PSTLZ'.
s_input-fvalue = '12345'.
APPEND s_input TO t_input.
CHECK t_input IS NOT INITIAL.
SELECT * FROM kna1
INTO TABLE t_kna1
FOR ALL ENTRIES IN t_input
WHERE kunnr = t_input-kunnr.
LOOP AT t_input INTO s_input.
READ TABLE t_kna1 ASSIGNING <fs_kna1>
WITH KEY kunnr = s_input-kunnr.
IF sy-subrc = 0.
ASSIGN COMPONENT s_input-fname
OF STRUCTURE <fs_kna1> TO <fs>.
IF sy-subrc = 0.
<fs> = s_input-fvalue.
ENDIF.
ENDIF.
ENDLOOP.
at the end this loop, t_input will have as expected...
Regards,
Barath
2013 Apr 09 4:32 PM
Thanks for the suggestions.. But not yet resolved. Very close .. not sure what we are missing.
Replying the problem on the main thread..
we have two internal tables. t_input and t_kna1.
t_kna1 is kna1.
based on the fname (fieldname) from t_input and value_new we have to select the same fieldname from KNA1 and update it with Value_new.
suppose :
t_input :
kunnr fname value_new.
1 land1 US
2 pstlz 12345
now for internal table KNA1 2 records should be created
t_KNA1
KUNNR LAND1 PSTLZ ORT01 etc etc
1 US
2 12345
Thanks
2013 Apr 09 5:36 PM
Hi Rahul,
let's get some things straight first.
In your Text, you tell us, you want to create entries, whereas in your code-sample you want to modify, where the latter implies, that the entry already exists.
So you have to know which way to go.
Then, you have a case on tabname in your code. This implies, that there are not that many possible tablenames involved. In this case, it would be far easier (though admittedly less impressive) if you just wrote your coding according to each possible table with properly defined workarea. Probably put in distinct routines and call them dynamically (if you must).
If you really want to go the hard way and get a grip on the whole field-symbols thing, you'll have to understand, that a field-symbol is not a variable. There are some differences in the use of field-symbols as opposed to variables. From your code it is obvious, that you are aquainted to the syntax basics with FS.
Let me give you an example.
You have an internal table and want to acces a certain row.
Now usually you would take a simple approach like this
LOOP AT mytab INTO my_wa.
IF mycondition.
* do something to that kine
my_wa-some_field = new_content.
MODIFY mytab FROM my_wa INDEX sy-tabix.
ENDIF.
ENDLOOP.
Now with field-symbols, this looks different
LOOP AT mytab ASSIGNING <line_fs>.
ASSIGN COMPONENT fieldname OF STRUCTURE <line_fs> TO <field_fs>.
IF mycondition.
<field_fs> = new_content.
ENDIF.
ENDLOOP.
Note that no modify is necessary, since you already manipulate the memory-region that is occupied by your desired field of your desired tablerow.
If you want to create a new table entry using field-symbols, your approach will have to be a different one.
APPEND INITIAL LINE TO mytab ASSIGNING <line_fs>.
now you have a memory-region, you can manipulate like before, linked to your field-symbol.
As before, you can
ASSIGN COMPONENT fieldname OF STRUCTURE <line_fs> TO <field_fs>.
<field_fs> = newvalue.
Your new entry now sports your new value in your specified field. No modify!
Hope this helps
Regards
Jörg
2013 Apr 09 6:33 PM
Hi Jorg,
Thanks for the insights. the line of codes which are not appropriate were written during the R & D and I was looking for there effect after assigning. That is the reason I have provided a example showcasing my actual need. The query is resolved now.
Thanks
Rahul