Field Symbols New | Data Type | Variable (Computer Science)

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

Defining Field Symbols
To declare a field symbol, use the statement FIELD-SYMBOLS <FS> [<type>|STRUCTURE <s> DEFAULT <wa>].

You specify the type of a field symbol using the same semantics as for formal parameters in procedures. The system checks whether the data object is an internal table. The field symbol adopts all of the attributes of the data object. N. Only data objects with type C. The system checks whether the actual parameter is a hashed internal TYPE TABLE TYPE ANY TABLE TYPE INDEX TABLE TYPE STANDARD TABLE TYPE SORTED TABLE TYPE HASHED TABLE . the type of the field symbol is either partially specified or not specified at all. In this case. regardless of the type of the data object assigned to it.For field symbols. and does not have the complex type attributes of a reference or structured field until runtime. The system checks whether the data object is a standard internal table. P. 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). the field symbol <FS> can have data objects of any time assigned to it. The field symbol adopts the field length and DECIMALS specification (type P) of the data object. The system checks whether the data object is an index table (standard or sorted table). Any attributes that are not specified are inherited from the corresponding data object in the ASSIGN statement. The field symbol inherits all of the remaining attributes (line type. The field symbol inherits all of the attributes (line type. Generic Type Specification The following types allow you more freedom when using actual parameters. This is a shortened form of TYPE STANDARD TABLE (see below). The field symbol inherits all of the remaining attributes (line type. They identify field symbols in the program code. When you assign a data object to a field symbol. If you specify the type fully. The field symbol inherits all of the attributes (line type. The data object only needs to have the selection of attributes specified. You can then only assign data objects to it that have exactly the same data type. Note that although it possible to assign reference variables and structured data objects to untyped field symbols. the field symbol inherits its technical attributes. For <type> you can enter either TYPE <t> or LIKE <f>. table type. or X are accepted. N. key) from the data object. P. When you assign a data object. the angle brackets are part of the syntax. table type. If the types are not compatible or convertible. You can specify the type either generically or in full. the field symbol retains its original type. the system reacts with a syntax or runtime error. The data type of the assigned data object becomes the actual data type of the field symbol. key) from the data object. in a MOVE statement). If you specify a generic type. all of the technical attributes of the field symbol are determined when you define it. the static field symbol is only a pointer to the field in memory. the system checks whether the type of the data object you are trying to assign is compatible with that of the field symbol. Specific statements such as CREATE OBJECT <FS> or LOOP AT <FS> are not possible. or X Check for data object All types of data object are accepted. key) from the data object. If you do not specify any additions. The system checks whether the actual parameter is a sorted internal table. Type specification No type specification TYPE ANY TYPE C. Typing Field Symbols The <type> addition allows you to specify the type of a field symbol. key) from the data object. The system checks whether the data object is a standard internal table. You can only use the field symbol to address the whole field (for example.

COL2. In this case. or a database table from the ABAP Dictionary TYPE REF TO <cif> TYPE LINE OF <itab> LIKE <f> When you use a field symbol that is fully typed. In the program. at most. you would have to work with further field symbols and the method of assigning structures component by component. ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1. This is a data type defined within the program using the TYPES statement. or a table with structured line type). KEY(4) VALUE 'COL1'. FIELD-SYMBOLS <FS> TYPE ANY TABLE. since they are recognized in the source code. 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. READ TABLE <FS> WITH TABLE KEY (KEY) = 'X' INTO WA. 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>. address them dynamically. key) from the data object. Specifying the Type Fully When you use the following types. the type specification ANY TABLE only indicates that <FS> is a table. END OF LINE. after which it is possible to address the table key of the field symbol dynamically. The field symbol inherits all of the remaining attributes (line type. even the specific internal table statement READ TABLE <FS> would not have been possible. The internal table ITAB is assigned to the generic field symbol <FS>.table. since the formal parameter P does not adopt the key of table ITAB until runtime. DATA: WA TYPE LINE. COL1. you can address its attributes statically in the program. I. If you fully specify the type of a field symbol as a reference or structured . is syntactically not possible. Type specification TYPE D. If you specify a type generically. You can. the technical attributes of the field symbols are fully specified. ASSIGN ITAB TO <FS>. If the type had been ANY (or no type had been specified at all). remember that the attributes inherited by the field symbol from the program are not statically recognizable in the program. the static address READ TABLE <FS> WITH TABLE KEY COL1 = 'X' INTO WA. If you adopt a structured type generically (a structure. TYPES: BEGIN OF LINE. The technical attributes of the data objects must correspond to those of the field symbol. However. the individual components cannot be addressed in the program either statically or dynamically. F.

COL1. otherwise a runtime error may result. you can address it as you would the data object itself once you have assigned an object to it. 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. In such cases. loop through an internal table. So. you should remember the possible effects of alignment. the data object must have the same alignment. or create an object with reference to a field symbol. ASSIGN LINE TO <FS>. COL2 VALUE 'X'. When you assign a data object to a field symbol with a structure. COL1(2). FIELD-SYMBOLS <FS> STRUCTURE <s> DEFAULT <f>. and you can address its components in the program. END OF LINE2. for example. the system only checks whether it is at least as long as the structure. It has the same technical attributes as the structure <s>. DATA: BEGIN OF LINE1. END OF LINE1. The structure <s> is either a structured local data object in the program. DATA: BEGIN OF LINE2. If <s> contains components with type I or F. When you assign a data object to the field symbol. 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. COL2(2).data object. The field symbol <FS> is fully typed as a structure. MOVE <FS>-COL2 TO <FS>-COL1. you could address the components of a structure. However. COL2 LIKE SY-DATUM. COL1(3). FIELD-SYMBOLS: <F1> STRUCTURE LINE1 DEFAULT WA. or a flat structure from the ABAP Dictionary. DATA: WA(10) VALUE '0123456789'. COL3(5). . END OF LINE. <f> is a data object that must be assigned to the field symbol as a starting field. FIELD-SYMBOLS <FS> LIKE LINE. this assignment can be changed later using the ASSIGN statement. You can address the individual components of the field symbol. <F2> STRUCTURE LINE2 DEFAULT WA. DATA: BEGIN OF LINE.

<F2>-COL2. <F1>-COL2. / <F2>-COL1. The output is: 012 34 56789 01 2345/67/89 This example declares two field symbols to which different structures are attached. <F1>-COL3. The output shows that the field symbols assign the strings component by component according to the type of the component.WRITE: / <F1>-COL1. The string WA is then assigned to each of them. .

Sign up to vote on this title
UsefulNot useful