Entering content frameDeclaring Field Symbols Locate the document in its SAP Library structure

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 type 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: it is possible to assign reference variables and structured data objects to untyped field symbols. However, 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. If however, you want to assign the type of the field symbol to the data object by means of casting, you must do so explicitly using the ASSIGN statement. The system then treats the assigned data object as if it had the same type as the field symbol.

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.

You should always specify a type for each field symbol. If you cannot avoid defining a generic field symbol, make this clear by using an appropriate generic type declaration.

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.

Typing

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.

Example

TYPES: BEGIN OF line,
         col1 TYPE c,
         col2 TYPE c,
       END OF line.

DATA: wa TYPE line,
      itab TYPE HASHED TABLE OF line WITH UNIQUE KEY col1,
      key(4) TYPE c 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 not possible syntactically, since the field symbol 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.

Typing

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>|DATA

The field symbol is a reference variable for the class or interface <cif>, or for a data object.

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.

Example

REPORT demo_field_symbols_type .

DATA: BEGIN OF line,
         col1(1) TYPE c,
         col2(1) TYPE c 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 that 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 advised 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.

The STRUCTURE is obsolete; you should no longer use it. Field symbols defined using the STRUCTURE addition are a mixture of typed field symbols and a utility for casting to either local or ABAP Dictionary data types. If you want to define the type of a field symbol, include the TYPE addition in a FIELD-SYMBOLS statement. If you want to use casting, include the CASTING addition in an ASSIGN statement.

Example

Example using the obsolete STRUCTURE addition:

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.

Example using the correct syntax (TYPE and CASTING):

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> LIKE line1.
ASSIGN wa TO <f1> CASTING.

FIELD-SYMBOLS: <f2> LIKE line2.
ASSIGN wa TO <f2> CASTING.

WRITE: / <f1>-col1, <F1>-col2, <F1>-col3,
       / <f2>-col1, <F2>-col2.

In both cases, the list appears as follows:

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 components.

 

 

Leaving content frame