Professional Documents
Culture Documents
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: o o o o 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
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.
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 TYPE D, F, I, or T TYPE <type> Technical attributes of the field symbol The field symbol has the technical attributes of the predefined elementary 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 The field symbol is a reference variable (ABAP Objects) for the class or interface <cif> 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 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.
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.