Wiki‎ > ‎Abap Overview‎ > ‎

Assigning Data Objects to Field Symbols

To assign a data object to a field symbol, use the ASSIGN statement. The ASSIGN statement has several variants and parameters.

Static ASSIGN

If you already know the name of the field that you want to assign to the field symbol when you write a program, use the static ASSIGN statement:

ASSIGN <f> TO <FS>.

When you assign the data object, the system checks whether the technical attributes of the data object <f> correspond to any type specifications for the field symbol <FS>. The field symbol adopts any generic attributes of <f> that are not contained in its own type specification. Following the assignment, it points to <f> in memory.

FIELD-SYMBOLS: <F1>, <F2> TYPE I.

DATA: TEXT(20)  TYPE C VALUE 'Hello, how are you?',
      NUM       TYPE I VALUE 5,
      BEGIN OF LINE1,
        COL1 TYPE F VALUE '1.1e+10',
        COL2 TYPE I VALUE '1234',
      END OF LINE1,
      LINE2 LIKE LINE1.

ASSIGN TEXT TO <F1>.
ASSIGN NUM TO <F2>.
DESCRIBE FIELD <F1> LENGTH <F2>.
WRITE: / <F1>, 'has length', NUM.

ASSIGN LINE1 TO <F1>.
ASSIGN LINE2-COL2 TO <F2>.
MOVE <F1> TO LINE2.
ASSIGN 'LINE2-COL2 =' TO <F1>.
WRITE: / <F1>, <F2>.

The output is:

Hello, how are you? has length 20

LINE-COL2 = 1,234

The example declares two field symbols <F1> and <F2>. <F2> has the type I, which means that only type I fields may be assigned to it. <F1> and <F2> both point to different fields during the program.

Static ASSIGN with Offset Specification

In a static assign statement, you can use positive offset and length specifications to assign a part of a field to a field symbol.

ASSIGN <f>[+<o>][(<l>)] TO <FS>.

When you assign parts of fields to a field symbol, the following special conditions apply:

  • The offset <o> and length <l> can be variables.
  • The system does not check whether the selected part lies inside the field <f>. Both offset <o> and length <l> can be larger than the length of <f>. You can address memory beyond the boundary of <f>, but not beyond the data areas for field symbols.
  • If you do not specify the length <l>, the system automatically uses the length of the field <f>. If <o> is greater than zero, <FS> always points to an area beyond the limits of <f>.
  • If <o> is smaller than the length of <f>, you can enter an asterisk (*) for <l> to prevent <FS> from referring to an address beyond the limits of <f>.

FIELD-SYMBOLS <FS>.

DATA: BEGIN OF LINE,
        STRING1(10) VALUE '0123456789',
        STRING2(10) VALUE 'abcdefghij',
      END OF LINE.

WRITE / LINE-STRING1+5.

ASSIGN LINE-STRING1+5 TO <FS>.
WRITE / <FS>.

ASSIGN LINE-STRING1+5(*) TO <FS>.
WRITE / <FS>.

The output is:

56789

56789abcde

56789

In this example, you can see the difference between an offset specification in a WRITE statement and an offset specification in an ASSIGN statement. With WRITE, the output is truncated at the end of LINE-STRING1. Specifying an offset greater than 9 would lead to an error during the syntax check. In the first ASSIGN statement, the memory area of length 10 beginning with offset 5 in LINE-STRING1 is assigned to the field symbol <FS>. The output is meaningful because the memory area behind LINE-STRING1 belongs to LINE-STRING2. In the second ASSIGN statement, the length specification * prevents the system from addressing the memory area after LINE-STRING1.

FIELD-SYMBOLS <FS>.

DATA: BEGIN OF LINE,
        A VALUE '1', B VALUE '2', C VALUE '3', D VALUE '4',
        E VALUE '5', F VALUE '6', G VALUE '7', H VALUE '8',
      END OF LINE,
      OFF TYPE I,
      LEN TYPE I VALUE 2.

DO 2 TIMES.
  OFF = SY-INDEX * 3.
  ASSIGN LINE-A+OFF(LEN) TO <FS>.
  <FS> = 'XX'.
ENDDO.

DO 8 TIMES.
  OFF = SY-INDEX - 1.
  ASSIGN LINE-A+OFF(1) TO <FS>.
  WRITE <FS>.
ENDDO.

The output is:

1 2 3 X X 6 X X

The example show how field symbols can make it easier to access and manipulate regular structures. Note, however, that this flexible method of manipulating memory contents beyond field limits also has its dangers and may lead to runtime errors.

Dynamic ASSIGN

If you do not know the name of the field that you want to assign to the field symbol when you write a program, you can use a dynamic ASSIGN statement:

ASSIGN (<f>) TO <FS>.

This statement assigns the field whose name is contained in the field <f> to the field symbol <FS>. You cannot use offset and length in a dynamic ASSIGN.

At runtime, the system searches for the corresponding data object in the following order:

  1. If the ASSIGN statement is in a procedure, the system searches first in its local data.
  2. If it cannot find the object in the local data (or if the ASSIGN statement is not in a procedure), it then looks in the local data of the program.
  3. If the field does not exist in the global data of the program, the system looks in the table work areas declared with the TABLES statement in the main program of the current program group. A program group consists of a main program and all of the programs that are loaded into the same internal session as a result of other program calls.

If the search is successful and a field can be assigned to the field symbol, SY-SUBRC is set to 0. Otherwise, it is set to 4, and the field symbol remains unchanged. For security reasons, you should always check the value of SY-SUBRC after a dynamic ASSIGN to prevent the field symbol pointing to the wrong area.

Searching for the field in this way slows down the program. You should therefore only use the dynamic ASSIGN statement when absolutely necessary. If you know when you create the program that you want to assign a table work area to the field symbol, you can also use the following variant of the dynamic ASSIGN statement:

ASSIGN TABLE FIELD (<f>) TO <FS>.

The system then only searches within the table work areas in the main program of the current program group for the data object that is to be assigned to the field symbol.

Suppose we have three programs. The main program:

PROGRAM DEMO.
TABLES SBOOK.
SBOOK-FLDATE = SY-DATUM.
PERFORM FORM1(MYFORMS1).

and two other programs:

PROGRAM MYFORMS1.
FORM FORM1.
  PERFORM FORM2(MYFORMS2).
ENDFORM.

and

PROGRAM MYFORMS2.
FORM FORM2.
  DATA NAME(20) VALUE 'SBOOK-FLDATE'.
  FIELD-SYMBOLS <FS>.
  ASSIGN (NAME) TO <FS>.
  IF SY-SUBRC EQ 0.
    WRITE / <FS>.
  ENDIF.
ENDFORM.

The output is:

02.06.1998

The program group in the internal session now consists of the programs DEMO, MYFORMS1 and MYFORMS2. The field symbol <FS> is defined in MYFORMS2. After the dynamic ASSIGN statement, it points to the component FLDATE of the table work area SBOOK declared in the main program DEMO.

TABLES SBOOK.

DATA: NAME1(20) VALUE 'SBOOK-FLDATE',
NAME2(20) VALUE 'NAME1'.

FIELD-SYMBOLS <FS>.

ASSIGN TABLE FIELD (NAME1) TO <FS>.
WRITE: / 'SY-SUBRC:', SY-SUBRC.

ASSIGN TABLE FIELD (NAME2) TO <FS>.
WRITE: / 'SY-SUBRC:', SY-SUBRC.

The output is:

SY-SUBRC:      0

SY-SUBRC:      4

In the first ASSIGN statement, the system finds the component FLDATE of the table work area SBOOK and SY-SUBRC is set to 0. In the second ASSIGN statement, the system does not find the field NAME1 because it is declared by the DATA statement and not by the TABLES statement. In this case, SY-SUBRC is set to 4.

Assigning Field Symbols

Instead of using the names of data objects, you can also assign field symbols to field symbols in all variants of the ASSIGN statement.

ASSIGN <FS1>[+<o>][(<l>)] TO <FS2>.

in a static ASSIGN and:

ASSIGN [TABLE FIELD] (<f>) TO <FS2>.

in a dynamic ASSIGN, where the field <f> contains the name of a field symbol <FS1>. <FS1> and <FS2> may be identical.

DATA: BEGIN OF S,
        A VALUE '1', B VALUE '2', C VALUE '3', D VALUE '4',
        E VALUE '5', F VALUE '6', G VALUE '7', H VALUE '8',
      END OF S.

DATA OFF TYPE I.

FIELD-SYMBOLS <FS>.

ASSIGN S-A TO <FS>.

DO 4 TIMES.
  OFF = SY-INDEX - 1.
  ASSIGN <FS>+OFF(1) TO <FS>.
  WRITE <FS>.
ENDDO.

The output is:

1 2 4 7

The program declares a structure with eight components S-A to S-H, and fills them with the digits 1 to 8. These character strings are stored regularly in memory. The component S-A is assigned initially to the field symbol <FS>. The statements in the DO loop have the following effect:

Loop pass 1:

<FS> points to S-A, OFF is zero, and S-A is assigned to <FS>

Loop pass 2:

<FS> points to S-A, OFF is one, and S-B is assigned to <FS>

Loop pass 3:

<FS> points to S-B, OFF is two, and S-D is assigned to <FS>

Loop pass 4:

<FS> points to S-D, OFF is three, and S-G is assigned to <FS>

 

  

Assigning Components of Structures to a Field Symbol 

Casting

For a structured data object <s>, you can use the statement

ASSIGN COMPONENT <comp> OF STRUCTURE <s> TO <FS>.

to assign one of its components <comp> to the field symbol <FS>. You can specify the component <comp> either as a literal or a variable. If <comp> is of type C or a structure which has no internal tables as components, it specifies the name of the component. If <comp> has any other elementary data type, it is converted to type I and specifies the number of the component. In the assignment is successful, SY-SUBRC is set to 0. Otherwise, it returns 4.

This statement is particularly important for addressing components of structured data objects dynamically. If you assign a data object to a field symbol generically, or pass it generically to the parameter interface of a procedure, you cannot address its components either statically or dynamically. Instead, you must use the above statement. This allows indirect access either using the component name or its index number.

DATA: BEGIN OF LINE,
        COL1 TYPE I VALUE '11',
        COL2 TYPE I VALUE '22',
        COL3 TYPE I VALUE '33',
      END OF LINE.

DATA COMP(5) VALUE 'COL3'.

FIELD-SYMBOLS: <F1>, <F2>, <F3>.

ASSIGN LINE TO <F1>.
ASSIGN COMP TO <F2>.

DO 3 TIMES.
  ASSIGN COMPONENT SY-INDEX OF STRUCTURE <F1> TO <F3>.
  WRITE <F3>.
ENDDO.

ASSIGN COMPONENT <F2> OF STRUCTURE <F1> TO <F3>.
WRITE / <F3>.

The output is:

11         22         33

33

The field symbol <F1> points to the structure LINE, <F2> points to the field COMP. In the DO loop, the components of LINE are specified by their numbers and assigned one by one to <F3>. After the loop, the component COL3 of LINE is specified by its name and assigned to <F3>. Note that ASSIGN COMPONENT is the only possible method of addressing the components of <F1>. Expressions such as <F1>-COL1 are syntactically incorrect.

Defining the Data Type of a Field Symbol 

To set the data type of a field symbol independently of that of the objects that you assign to it, use the TYPE addition in the ASSIGN statement:

ASSIGN ..... TO <FS> TYPE <t>.

You can use the TYPE addition in all variants of the ASSIGN statement. At present, you can only use the elementary ABAP types (C, D, F, I, N, P, T, and X), ‘s’ for two-byte integers (with sign) and ‘b’ for one byte integers (without sign) as literals of variables for <t>.

There are two possible cases:

  • Untyped field symbols

If you use the TYPE addition, an untyped field symbol <FS> adopts the data type specified in <t> instead of the data type and output length of the data object assigned to it. If the field symbol is used after the assignment in the program, the assigned data object is not converted to the specified type <t>. However, the contents of the data object are interpreted as though they belonged to a field of type <t>. When you specify the type of a field symbol, you force a particular view on the data objects assigned to it.

  • Typed field symbols

Using the TYPE option with a typed field symbol makes sense if the data type of the data object to be assigned is incompatible with the typing of the field symbol, but you want to avoid the resulting error message. In this case, the specified type <t> and the typing of the field symbol must be compatible. The field symbol then retains its data type, regardless of the assigned data object.

A runtime error occurs if

  • The specified data type is unknown,
  • The length of the specified data type is incompatible with the type of the assigned field,
  • there is an alignment error.

DATA TXT(8) VALUE '19980606'.

DATA MYTYPE(1) VALUE 'X'.

FIELD-SYMBOLS <FS>.

ASSIGN TXT TO <FS>.
WRITE / <FS>.

ASSIGN TXT TO <FS> TYPE 'D'.
WRITE / <FS>.

ASSIGN TXT TO <FS> TYPE MYTYPE.
WRITE / <FS>.

The output is:

19980606

06061998

3139393830363036

In this example, the character string TXT is assigned to <FS> three times. In the first assignment, the type is not specified. The second time, the type is specified as D, and finally as X. The format of the second output line depends on the settings of the current user in their user master record. The numbers in the last line are the hexadecimal codes of the characters in TXT. They are platform-specific (in this case, ASCII).

 

 

Data Areas for Field Symbols

You can only assign data objects from the data areas of the ABAP program to a filed symbol. When you assign a data object to a field symbol, the system checks at runtime to ensure that no data is lost due to the field symbol addressing memory outside the data area.

The data areas of an ABAP program are:

  • The table memory area for internal tables. The size of this storage area depends on the number of table lines which is not fixed, but determined dynamically at runtime.
  • The DATA storage area for other data objects. The size of this storage area is fixed during the data declaration.

Field symbols cannot point to addresses outside these areas. If you assign data objects to a field symbol and point to addresses outside these areas, a runtime error occurs.

Certain system information, such as the control blocks of internal tables, is also stored in the DATA storage area. Therefore, despite the runtime checks, you may unintentionally overwrite some of these fields and cause subsequent errors (for example, destruction of the table header).

PROGRAM DEMO.

DATA: TEXT1(10), TEXT2(10), TEXT3(5).

FIELD-SYMBOLS <FS>.

DO 100 TIMES.
  ASSIGN TEXT1+SY-INDEX(1) TO <FS>.
ENDDO.

After starting DEMO, a runtime error occurs. The short dump message begins as follows:

The DATA memory area is at least 25 bytes wide (it can be expanded due to alignment of the data areas in memory). In one of the loop passes, the program tries to access an address outside this area. The termination message also contains the contents of the field SY-INDEX, which may be different from case to case. Up to the 24th loop pass, no error occurs. If you replace TEXT1 with TEXT2 in the ASSIGN statement, the error occurs ten loop passes earlier.

 

Comments