05-27-2011 7:42 AM
I have two tables tab1 & tab2, want to assign field values from tab1 to tab2 suing field symbols. Deatils are :
TYPES: BEGIN OF ty_tab1,
item1 TYPE char20,
item2 TYPE char20,
val1 type i,
val2 type i,
END OF ty_tab1,
BEGIN OF ty_tab2,
item1 TYPE char20,
item2 TYPE char20,
END OF ty_tab2.
DATA: it_tab1 TYPE TABLE OF ty_tab1,
it_tab2 TYPE TABLE OF ty_tab2.
FIELD-SYMBOLS <fs_tab1> TYPE ty_tab1.
<fs_tab2t> type ty_tab2.
LOOP AT it_tab1 assigning <fs_tab1>.
READ TABLE it_tab2 assigning <fs_tab2> WITH KEY item1 = <fs_tab1>-item1
item2 = <fs_tab1>-item2.
IF sy-subrc EQ 0.
ASSIGN COMPONENT 'ITEM1' OF STRUCTURE <fs_tab1> TO <fs_tab2>.
ASSIGN COMPONENT 'ITEM2' OF STRUCTURE <fs_tab1> TO <fs_tab2>.
endif.
endloop.
Getting error msg
You attempted to assign a field to a typed field symbol, but the field does not have the required type.
whats the reason of error.
Edited by: Matt on May 27, 2011 8:59 AM - added tags
05-27-2011 7:54 AM
I guess the problem lies here
ASSIGN COMPONENT 'ITEM1' OF STRUCTURE <fs_tab1> TO <fs_tab2>.
You are obviously trying to assign a field ITEM1 to two fields ITEM1 and ITEM2 which are represented by <fs_tab2>.
You need to address these fields separately in target field symbol like below.
FIELD-SYMBOLS: <fs_item1>, <fs_item2>.
ASSIGN COMPONENT 'ITEM1' OF STRUCTURE <fs_tab1> TO <fs_item1>.
ASSIGN COMPONENT 'ITEM2' OF STRUCTURE <fs_tab1> TO <fs_item2>.
Anyhow there is no reason why you should use ASSIGN COMPONENT here. You now statically type of field symbol so you can address its components statically too
<fs_tab2>-item1 = <fs_tab1>-item1.
<fs_tab2>-item2 = <fs_tab1>-item2.
An alternative and I think good approach would be make use of nested typing
TYPES: BEGIN OF ty_common,
item1 TYPE char20,
item2 TYPE char20,
TYPES END OF ty_common.
TYPES BEGIN OF ty_tab1.
INCLUDE TYPES ty_common as common.
TYPES val1 type i,
TYPES val2 type i,
TYPES END OF ty_tab1.
TYPES ty_tab2 TYPE ty_common.
"now
READ TABLE it_tab2 assigning <fs_tab2> WITH KEY item1 = <fs_tab1>-item1
item2 = <fs_tab1>-item2.
IF sy-subrc EQ 0.
<fs_tab2> = <fs_tab1>-common.
endif.
More on nested typing in my blog [Do you really know everything about typing?|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/19958] [original link is broken] [original link is broken] [original link is broken];
Regards
Marcin
05-27-2011 8:46 AM
Hi marcin,
The following statement not working as got following error
<fs_tab2>-item1 = <fs_tab1>-item1.
<fs_tab2>-item2 = <fs_tab1>-item2.
A new value is to be assigned to the field "<fs_tab2>", although this field is
entirely or partly protected against changes.
05-27-2011 9:06 AM
Let me guess, you are addressing your table inside some subroutine which was passed by USING or in method passed by IMPORTING. The system will yell this is protected as was signed as not to be changed . Please ensure this is not the case here. then give us your feedback.
Regards
Marcin
05-27-2011 10:29 AM
You are right Marcin, the code is inside the subroutine but tables are not passed by using. These are declared as global ones.
Thanks
Prat
05-27-2011 11:06 AM
Hi Marcin,
Please advise how to pass contents of itab1 to itab2 in such scenario.
05-27-2011 11:16 AM
Taken from example dump
A new value is to be assigned to the field "...", although this field is
entirely or partly protected against changes.
The following are protected against changes:
- Character literals or numeric literals
- Constants (CONSTANTS)
- Parameters of the category IMPORTING REFERENCE for functions and
methods
- Untyped field symbols not yet assigned a field using ASSIGN
- TABLES parameters if the actual parameter is protected against changes
- USING reference parameters and CHANGING parameters for FORMs, if the
actual parameter is protected against changes and
- Accesses using field symbols if the field assigned using ASSIGN is
protected (or partially protected, e.g. key components of an internal
table with the type SORTED or HASHED TABLE) against changes
- Accesses using references, if the field bound to the reference is
protected (or partially protected) against changes
- External write accesses to READ-ONLY attributes,
- Content of a shared object area instance accessed using a shared lock
(ATTACH_FOR_READ).
You likely fall in one of these cases. Check each and if still unsure please share your code so we can reproduce the error.
Regards
Marcin
05-27-2011 7:54 AM
ASSIGN COMPONENT 'ITEM1' OF STRUCTURE <fs_tab1> TO <fs_tab2>.
What is the "type" of ITEM1?
item1 TYPE char20
What is the "static type" of <fs_tab2>?
<fs_tab2t> type ty_tab2
Need i say more? Search the net on how to correct this error