cancel
Showing results for 
Search instead for 
Did you mean: 

Copy field from internal table using For

deepak_h3
Participant
0 Kudos

I have a itab with 3 fields - field1, field2, field3

I want to copy only field 3 into another Itab2 with different structure.

Also i want to set same value in the other fields of itab2.

Can I do something as below - I tried but it did not work ? Is it possible ?

DATA(gt_itab2) = VALUE #( Field1 = 'A' Field2 = 'B' FOR ls_itab1 IN gt_itab1 ( ls_itab1–field3 ) ).

raymond_giuseppi
Active Contributor

Could you reformulate

  • Your first itab has fields Field1-3, and you want to copy field D
  • Your second itab has another structure, but you map the same fields than defined in the first table
View Entire Topic
aoyang
Contributor

Please refer to below code. It is passing FIELD1 of LT_DATA1 to all 3 fields in LT_DATA2. You can map any fields you like from LT_DATA1 to LT_DATA2. The end result of below code will look like

FIELD1 FIELD2 FIELD3

A A A

D D D

G G G

TYPES: BEGIN OF TY_W_DATA,
FIELD1 TYPE STRING,
FIELD2 TYPE STRING,
FIELD3 TYPE STRING,
END OF TY_W_DATA.

TYPES: TY_T_DATA1 TYPE STANDARD TABLE OF TY_W_DATA,
TY_T_DATA2 TYPE SORTED TABLE OF TY_W_DATA
WITH NON-UNIQUE KEY
FIELD1
FIELD2
FIELD3.


START-OF-SELECTION.

DATA:LT_DATA1 TYPE TY_T_DATA1,
LT_DATA2 TYPE TY_T_DATA2.

LT_DATA1 = VALUE #( ( FIELD1 = 'A' FIELD2 = 'B' FIELD3 = 'C' )
( FIELD1 = 'D' FIELD2 = 'E' FIELD3 = 'F' )
( FIELD1 = 'G' FIELD2 = 'H' FIELD3 = 'I' ) ).

LT_DATA2 = VALUE TY_T_DATA2( FOR LW_DATA1 IN LT_DATA1
( FIELD1 = LW_DATA1-FIELD1
FIELD2 = LW_DATA1-FIELD1
FIELD3 = LW_DATA1-FIELD1
) ).
deepak_h3
Participant
0 Kudos

Thank you, can use it with some tweak. Maybe not clear from my question. I wanted the field1 and field 2 in target to be different.