Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Assignments →  Setting References →  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'.
        ASSIGN struc-word INCREMENT 1 TO <word> RANGE struc.
      WHEN '2'.
        ASSIGN struc-word INCREMENT 1 TO <int>  RANGE struc.
      WHEN '3'.
        ASSIGN struc-word INCREMENT 2 TO <word> RANGE struc.
      WHEN '4'.
        ASSIGN struc-word INCREMENT 2 TO <int>  RANGE struc.
      WHEN '5'.
        ASSIGN struc-word INCREMENT 3 TO <word> RANGE struc.
      WHEN '6'.
        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 ASSIGN statement returns the value 0 in sy-subrc in both Unicode and non-Unicode systems. In Unicode systems, <word> is assigned the associated memory area of struc-int1 and struc-int2 which assumes type c. In non-Unicode systems, <word> is only assigned the memory area of struc-int1 which assumes type c.
  • The second ASSIGN statement terminates with a runtime error in both Unicode and non-Unicode systems since the data type of struc-word does not match the typing of <int>.
  • The third ASSIGN statement terminates with a runtime error in Unicode systems since the system tries to assign the component struc-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 of struc-int2 which assumes type c.
  • The fourth ASSIGN statement terminates with a runtime error in Unicode systems since the system tries to assign the component struc-stri to <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 of struc-word does not match the typing of <int>.
  • The fifth and the sixth ASSIGN statement both return the value 4 in sy-subrc in Unicode systems since the system tries to allocate memory area outside the structure struc specified after RANGE. In non-Unicode systems, a runtime error occurs since the system tries to assign the component struc-stri to <word> or <int>, and as the structure is deep and the typings of <word> and <int> are flat, no casting is possible.