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

Transfer dynamic data from source table to target table

Former Member
0 Likes
913

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

1 ACCEPTED SOLUTION
Read only

arseni_gallardo
Active Participant
0 Likes
879

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.

7 REPLIES 7
Read only

arseni_gallardo
Active Participant
0 Likes
880

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.

Read only

0 Likes
879

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.

Read only

0 Likes
879

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.

Read only

0 Likes
879

Of course... Seems I'm still asleep

Much easier this way!

Kr,

m.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
879

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.

Read only

0 Likes
879

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.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
879

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