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

copying fields froma dynamic table

Former Member
0 Likes
767

i have a dyanmic table say t1. now i have craeted another dynamic table say t2 with some fields in common with table t1. i won't be knowing which fields are in common. the common fields will be stord in another table t3. now i need to copy the common fields from t1 to t2 after reading them from t3. how do i go about it???

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
0 Likes
736

In the latest versions of ABAP, use MOVE-CORRESPONDING.

matt

5 REPLIES 5
Read only

matt
Active Contributor
0 Likes
737

In the latest versions of ABAP, use MOVE-CORRESPONDING.

matt

Read only

Former Member
0 Likes
736
TYPES:
  BEGIN OF s_t3.
    INCLUDE TYPE A.
    INCLUDE TYPE B.
TYPES:
  END OF s_t3.

DATA:
  t1                TYPE TABLE OF A,
  t2                TYPE TABLE OF B,
  ls_t3            TYPE s_t3,
  t3                TYPE TABLE OF s_t3.

FIELD-SYMBOLS:
  <fs_t1>        TYPE A,
  <fs_t2>        TYPE B.

LOOP AT t1 ASSIGNING <fs_t1>.
  READ TABLE t2 ASSIGNING <fs_t2>
    WITH KEY
      [key_t2] = [key_t1].
  CHECK sy-subrc IS INITIAL.

  MOVE-CORRESPONDING <fs_t1> TO <ls_t3>.
  MOVE-CORRESPONDING <fs_t2> TO <ls_t3>.
  APPEND ls_t3 TO t3.
ENDLOOP.
Read only

0 Likes
736

Hm, ok, that doesn't work. You cannot include the same structure fields twice...

Read only

Former Member
0 Likes
736

OK, first you have to find out what are the common fields of A and B.

You can use FM 'DDIF_NAMETAB_GET' to read the structure fields.

Then you have to create a dynamic type with the common fields of A and B and assign the values as described above in my first answer.

It's quite tricky...

Read only

Former Member
0 Likes
736

Oh, I think for your requirement you don't need t3 at all.

This should be the solution:


FIELD-SYMBOLS:
  <fs_t1>        TYPE A,
  <fs_t2>        TYPE B.
 
LOOP AT t1 ASSIGNING <fs_t1>.
  READ TABLE t2 ASSIGNING <fs_t2>
    WITH KEY
      [key_t2] = [key_t1].
  CHECK sy-subrc IS INITIAL.
 
  MOVE-CORRESPONDING <fs_t1> TO <fs_t2>.
ENDLOOP.

But in this case you have to know what is the key field for t1 and t2.