ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Assignments → Assigning References → Setting Field Symbols → ASSIGN → ASSIGN - mem_area → ASSIGN - dynamic_dobj
Field Symbols, ASSIGN INCREMENT
The examples shows how the statement ASSIGN behaves when the addition INCREMENT is used.
Other versions: 7.31 | 7.40 | 7.54
Source Code
DATA: BEGIN OF struc,
word TYPE c LENGTH 4 VALUE 'abcd',
int1 TYPE i VALUE 111,
int2 TYPE i VALUE 222,
stri TYPE string VALUE `efgh`,
END OF struc.
FIELD-SYMBOLS: <word> LIKE struc-word,
<int> TYPE i.
CASE assign.
WHEN '1'. "-> sy-subrc 0
ASSIGN struc-word INCREMENT 1 TO <word> RANGE struc.
WHEN '2'. "-> Runtime error
ASSIGN struc-word INCREMENT 1 TO <int> RANGE struc.
WHEN '3'. "-> Runtime error
ASSIGN struc-word INCREMENT 2 TO <word> RANGE struc.
WHEN '4'. "-> Runtime error
ASSIGN struc-word INCREMENT 2 TO <int> RANGE struc.
WHEN '5'. "-> sy-subrc 4
ASSIGN struc-word INCREMENT 3 TO <word> RANGE struc.
WHEN '6'. "-> sy-subrc 4
ASSIGN struc-word INCREMENT 3 TO <int> RANGE struc.
ENDCASE.
WRITE: / 'sy-subrc:', sy-subrc.
IF <word> IS ASSIGNED OR <int> IS ASSIGNED.
WRITE / 'Field symbol is assigned'.
ENDIF.
Description
This example shows why you should use the addition INCREMENT in the statement
ASSIGN only if you want to access sequences of similar memory areas and that
the typing of the field symbol must match the specification made in
casting_spec. Any access which is not appropriate as shown in the example can produce the following behavior:
- The first
ASSIGNstatement returns the value 0 insy-subrcin both Unicode and non-Unicode systems. In Unicode systems,<word>is assigned the associated memory area ofstruc-int1andstruc-int2which assumes typec. In non-Unicode systems,<word>is only assigned the memory area ofstruc-int1which assumes typec.
- The second
ASSIGNstatement terminates with a runtime error in both Unicode and non-Unicode systems since the data type ofstruc-worddoes not match the typing of<int>.
- The third
ASSIGNstatement terminates with a runtime error in Unicode systems since the system tries to assign the componentstruc-strito <word>, and as the structure is deep and the typing of<word>is flat, no casting is possible. In non-Unicode systems,<word>is assigned the memory area ofstruc-int2which assumes typec.
- The fourth
ASSIGNstatement terminates with a runtime error in Unicode systems since the system tries to assign the componentstruc-strito <int>, and as the structure is deep and the typing of<int>is flat, no casting is possible. In non-Unicode systems, a runtime error occurs since the data type ofstruc-worddoes not match the typing of<int>.
- The fifth and the sixth
ASSIGNstatement both return the value 4 in sy-subrc in Unicode systems since the system tries to allocate memory area outside the structurestrucspecified afterRANGE. In non-Unicode systems, a runtime error occurs since the system tries to assign the componentstruc-strito<word>or<int>, and as the structure is deep and the typings of<word>and<int>are flat, no casting is possible.