Wiki‎ > ‎Glossary‎ > ‎

Field Symbols

Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for a field, but point to its contents. A field symbol cam point to any data object. The data object to which a field symbol points is assigned to it after it has been declared in the program.

Whenever you address a field symbol in a program, you are addressing the field that is assigned to the field symbol. After successful assignment, there is no difference in ABAP whether you reference the field symbol or the field itself. You must assign a field to a field symbol before you can address it in a program.

All operations programmed with field symbols are applied to the field assigned to it. For example, a MOVE statement between two field symbols moves the contents of the field assigned to the first field symbol to the field assigned to the second field symbol. The field symbols themselves point to the same fields after the MOVE statement as they did before.

You can create field symbols either without or with type specifications. If you do not specify a type, the field symbol inherits all of the technical attributes of the field assigned to it. If you do specify a type, the system checks the compatibility of the field symbol and the field you are assigning to it during the ASSIGN statement.

Field symbols provide greater flexibility when you address data objects:

  • If you want to process sections of fields, you can specify the offset and length of the field dynamically.
  • You can assign one field symbol to another, which allows you to address parts of fields.
  • Assignments to field symbols may extend beyond field boundaries. This allows you to address regular sequences of fields in memory efficiently.
  • You can also force a field symbol to take different technical attributes from those of the field assigned to it.

The flexibility of field symbols provides elegant solutions to certain problems. On the other hand, it does mean that errors can easily occur. Since fields are not assigned to field symbols until runtime, the effectiveness of syntax and security checks is very limited for operations involving field symbols. This can lead to runtime errors or incorrect data assignments.

While runtime errors indicate an obvious problem, incorrect data assignments are dangerous because they can be very difficult to detect. For this reason, you should only use field symbols if you cannot achieve the same result using other ABAP statements.

For example, you may want to process part of a string where the offset and length depend on the contents of the field. You could use field symbols in this case. However, since the MOVE statement also supports variable offset and length specifications, you should use it instead. The MOVE statement (with your own auxiliary variables if required) is much safer than using field symbols, since it cannot address memory beyond the boundary of a field. However, field symbols may improve performance in some cases.

Defining Field Symbols

Defining Field Symbols 

To declare a field symbol, use the statement

FIELD-SYMBOLS <FS> [<type>|STRUCTURE <s> DEFAULT <wa>].

For field symbols, the angle brackets are part of the syntax. They identify field symbols in the program code.

If you do not specify any additions, the field symbol <FS> can have data objects of any time assigned to it. When you assign a data object, the field symbol inherits its technical attributes. The data type of the assigned data object becomes the actual data type of the field symbol.

Note that although it possible to assign reference variables and structured data objects to untyped field symbols, the static field symbol is only a pointer to the field in memory, and does not have the complex type attributes of a reference or structured field until runtime. You can only use the field symbol to address the whole field (for example, in a MOVE statement). Specific statements such as CREATE OBJECT <FS> or LOOP AT <FS> are not possible.

Typing Field Symbols

The <type> addition allows you to specify the type of a field symbol. When you assign a data object to a field symbol, the system checks whether the type of the data object you are trying to assign is compatible with that of the field symbol. If the types are not compatible or convertible, the system reacts with a syntax or runtime error. You can only assign variables to a field symbol if their type is compatible with that of the field symbol (see Defining the Type of a Field Symbol). In this case, the field symbol retains its original type, regardless of the type of the data object assigned to it.

You specify the type of a field symbol using the same semantics as for formal parameters in procedures. For <type> you can enter either TYPE <t> or LIKE <f>. You can specify the type either generically or in full. If you specify a generic type, the type of the field symbol is either partially specified or not specified at all. Any attributes that are not specified are inherited from the corresponding data object in the ASSIGN statement. If you specify the type fully, all of the technical attributes of the field symbol are determined when you define it. You can then only assign data objects to it that have exactly the same data type.

Generic Type Specification

The following types allow you more freedom when using actual parameters. The data object only needs to have the selection of attributes specified.

Type specification

Check for data object

No type specification

TYPE ANY

All types of data object are accepted. The field symbol adopts all of the attributes of the data object.

TYPE C, N, P, or X

Only data objects with type C, N, P, or X are accepted. The field symbol adopts the field length and DECIMALS specification (type P) of the data object.

TYPE TABLE

The system checks whether the data object is a standard internal table. This is a shortened form of TYPE STANDARD TABLE (see below).

TYPE ANY TABLE

The system checks whether the data object is an internal table. The field symbol inherits all of the attributes (line type, table type, key) from the data object.

TYPE INDEX TABLE

The system checks whether the data object is an index table (standard or sorted table). The field symbol inherits all of the attributes (line type, table type, key) from the data object.

TYPE STANDARD TABLE

The system checks whether the data object is a standard internal table. The field symbol inherits all of the remaining attributes (line type, key) from the data object.

TYPE SORTED TABLE

The system checks whether the actual parameter is a sorted internal table. The field symbol inherits all of the remaining attributes (line type, key) from the data object.

TYPE HASHED TABLE

The system checks whether the actual parameter is a hashed internal table. The field symbol inherits all of the remaining attributes (line type, key) from the data object.

If you specify a type generically, remember that the attributes inherited by the field symbol from the program are not statically recognizable in the program. You can, at most, address them dynamically.

TYPES: BEGIN OF LINE,
COL1,
COL2,
END OF LINE.

DATA: WA TYPE LINE,
ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1,
KEY(4) VALUE 'COL1'.

FIELD-SYMBOLS <FS> TYPE ANY TABLE.

ASSIGN ITAB TO <FS>.

READ TABLE <FS> WITH TABLE KEY (KEY) = 'X' INTO WA.

The internal table ITAB is assigned to the generic field symbol <FS>, after which it is possible to address the table key of the field symbol dynamically. However, the static address

READ TABLE <FS> WITH TABLE KEY COL1 = 'X' INTO WA.

is syntactically not possible, since the formal parameter P does not adopt the key of table ITAB until runtime. In the program, the type specification ANY TABLE only indicates that <FS> is a table. If the type had been ANY (or no type had been specified at all), even the specific internal table statement READ TABLE <FS> would not have been possible.

If you adopt a structured type generically (a structure, or a table with structured line type), the individual components cannot be addressed in the program either statically or dynamically. In this case, you would have to work with further field symbols and the method of assigning structures component by component.

Specifying the Type Fully

When you use the following types, the technical attributes of the field symbols are fully specified. The technical attributes of the data objects must correspond to those of the field symbol.

Type specification

Technical attributes of the field symbol

TYPE D, F, I, or T

The field symbol has the technical attributes of the predefined elementary type

TYPE <type>

The field symbol has the type <type>. This is a data type defined within the program using the TYPES statement, or a type from the ABAP Dictionary

TYPE REF TO <cif>

The field symbol is a reference variable (ABAP Objects) for the class or interface <cif>

TYPE LINE OF <itab>

The field symbol has the same type as a line of the internal table <itab> defined using a TYPES statement or defined in the ABAP Dictionary

LIKE <f>

The field symbol has the same type as an internal data object <f> or structure, or a database table from the ABAP Dictionary

When you use a field symbol that is fully typed, you can address its attributes statically in the program, since they are recognized in the source code. If you fully specify the type of a field symbol as a reference or structured data object, you can address it as you would the data object itself once you have assigned an object to it. So, for example, you could address the components of a structure, loop through an internal table, or create an object with reference to a field symbol.

DATA: BEGIN OF LINE,
COL1,
COL2 VALUE 'X',
END OF LINE.

FIELD-SYMBOLS <FS> LIKE LINE.

ASSIGN LINE TO <FS>.

MOVE <FS>-COL2 TO <FS>-COL1.

The field symbol <FS> is fully typed as a structure, and you can address its components in the program.

Attaching a Structure to a Field Symbol

The STRUCTURE addition forces a structured view of the data objects that you assign to a field symbol.

FIELD-SYMBOLS <FS> STRUCTURE <s> DEFAULT <f>.

The structure <s> is either a structured local data object in the program, or a flat structure from the ABAP Dictionary. <f> is a data object that must be assigned to the field symbol as a starting field. However, this assignment can be changed later using the ASSIGN statement.

When you assign a data object to the field symbol, the system only checks whether it is at least as long as the structure. You can address the individual components of the field symbol. It has the same technical attributes as the structure <s>.

If <s> contains components with type I or F, you should remember the possible effects of alignment. When you assign a data object to a field symbol with a structure, the data object must have the same alignment, otherwise a runtime error may result. In such cases, you are recommended to assign such data objects only to structured field symbols which retain the same structure as the field symbol at least over the length of the structure.


DATA: WA(10) VALUE '0123456789'.

DATA: BEGIN OF LINE1,
COL1(3),
COL2(2),
COL3(5),
END OF LINE1.

DATA: BEGIN OF LINE2,
COL1(2),
COL2 LIKE SY-DATUM,
END OF LINE2.

FIELD-SYMBOLS: <F1> STRUCTURE LINE1 DEFAULT WA,
<F2> STRUCTURE LINE2 DEFAULT WA.

WRITE: / <F1>-COL1, <F1>-COL2, <F1>-COL3,
/ <F2>-COL1, <F2>-COL2.

The output is:

012 34 56789
01 2345/67/89

This example declares two field symbols to which different structures are attached. The string WA is then assigned to each of them. The output shows that the field symbols assign the strings component by component according to the type of the component.

 

 

 

Comments