‎2011 Sep 26 10:17 AM
HI All ,
There is two table lt_source and lt_target which both are type any table ,I want to move data from
lt_source to lt_target ,lt_target have some identical column but not all of them,
for instance
in lt_source I have Column F1 - F10 and lt_target I have just F2 and F7 .
What is the best way to do that ...
Thanks,
Joy
‎2011 Sep 26 10:26 AM
Try this:
FIELD-SYMBOLS: <ls_source> type any,
<ls_target> type any.
loop at lt_source assigning <ls_source>.
append initial line to lt_target assigning <ls_target>.
move-corresponding <ls_source> to <ls_target>.
endloop.
‎2011 Sep 26 10:26 AM
Try this:
FIELD-SYMBOLS: <ls_source> type any,
<ls_target> type any.
loop at lt_source assigning <ls_source>.
append initial line to lt_target assigning <ls_target>.
move-corresponding <ls_source> to <ls_target>.
endloop.
‎2011 Sep 26 10:50 AM
Hi,
However this won't work if your internal tables have the type ANY TABLE...
If so, you will have to play with the class cl_abap_structdescr to first know the components and be able to do the matching.
such as:
DATA: go_typedesc TYPE REF TO cl_abap_typedescr.
FIELD-SYMBOLS: <tab1> TYPE ANY TABLE.
"...
DATA: l_dref TYPE REF TO data.
GET REFERENCE OF <tab1> INTO l_dref.
go_typedesc ?= cl_abap_structdescr=>describe_by_data_ref( l_dref ).
"Then components of your tab should then be in go_typedesc->key[]
Kr,
m.
‎2011 Sep 26 11:15 AM
You are right. My sample code will only work correctly for STANDARD TABLES. In a general case this would do the trick:
FIELD-SYMBOLS: <ls_source> TYPE ANY,
<ls_target> TYPE ANY.
DATA: l_ref TYPE REF TO data.
CREATE DATA l_ref LIKE LINE OF lt_target.
ASSIGN l_ref->* TO <ls_target>.
LOOP AT lt_source ASSIGNING <ls_source>.
MOVE-CORRESPONDING <ls_source> TO <ls_target>.
INSERT <ls_target> INTO TABLE lt_target.
ENDLOOP.
‎2011 Sep 26 11:20 AM
Of course... Seems I'm still asleep
Much easier this way!
Kr,
m.
‎2011 Sep 26 11:37 AM
Hello M,
You're code won't work if the target table is ANY TABLE(because index access is not possible for HASHED TABLEs).
You can prevent the usage of the RTTI class by using the following code:
FIELD-SYMBOLS: <itab1> TYPE ANY TABLE,
<itab2> TYPE ANY TABLE,
<wa1> TYPE ANY,
<wa2> TYPE ANY.
LOOP AT <itab1> ASSIGNING <wa1>.
INSERT INITIAL LINE INTO TABLE <itab2> ASSIGNING <wa2>.
MOVE-CORRESPONDING <wa1> TO <wa2>.
ENDLOOP.NB: Please remember that duplicate records(if any) will get filtered out.
BR,
Suhas
PS: I try not to use the generic table type(ANY TABLE) in most of the cases. I prefer using the more specific STANDARD TABLE, SORTED TABLE or HASHED TABLE as or when required.
‎2011 Sep 26 11:45 AM
Sushas,
Your code won't work either for HASHED or SORTED tables because it is forbidden to change the value of the key fields (runtime exception). The code in my second post prevents this from happening.
‎2011 Sep 26 12:07 PM
I forgot that i was trying to change the key fields of a SORTED / HASHED table. My bad
Thanks for pointing it out.
Cheers,
Suhas
Edited by: Suhas Saha on Sep 26, 2011 4:38 PM