Start of Content Area

Assigning Components of Structures to a Field Symbol  Locate the document in its SAP Library structure

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 that 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. If the assignment is successful, SY-SUBRC is set to 0. Otherwise, it is set to 4.

This statement is particularly important for addressing components of structured data objects dynamically. If you assign a data object to a field symbol either generically or using casting, or pass it generically (or using casting) 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.

Example

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.