Field symbols

4,006 views
3,686 views

Published on

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
4,006
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
223
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Field symbols

  1. 1. 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 each field symbol before you can address the latter in programs. Field symbols are similar to dereferenced pointers in C (that is, pointers to which the content operator * is applied). However, the only real equivalent of pointers in ABAP, that is, variables that contain a memory address (reference) and that can be used without the contents operator, are reference variables in ABAP Objects. (For more information, see Data References). 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.
  2. 2. Defining Field Symbols Assigning Data Objects to Field Symbols Declaring 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 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.
  3. 3. 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 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.
  4. 4. 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. 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.
  5. 5. 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 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.
  6. 6. 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. Assigning Components of Structures to a Field Symbol 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. 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'.
  7. 7. 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. Casting Data Objects When you assign a data object to a field symbol you can cast to any data type. This means that any area of memory can be considered to have a assumed a given type. You can then address parts of fields symbolically without having to use offset/length addressing. Use the CASTING addition to the ASSIGN statement. This allows you to assign a data object to a field symbol where the type of the data object is incompatible with that of the field symbol. There are two types of CASTING: casting with an implicit type declaration and casting with an explicit type declaration. Casting with an Implicit Type Declaration Provided the field symbol is either fully typed or has one of the generic built-in ABAP types – C, N, P, or X – you can use the following statement: ASSIGN ... TO <FS> CASTING. When the system accesses the field symbol, the content of the assigned data object is interpreted as if it had the same type as the field symbol. The length and alignment of the data object must be compatible with the field symbol type. Otherwise the system returns a runtime error. If the type of either the field symbol or the data object is – or contains – a string, reference type, or internal table, the type and position of these components must match exactly. Otherwise, a runtime error occurs.
  8. 8. REPORT demo_field_symbols_casting. TYPES: BEGIN OF t_date, year(4) TYPE n, month(2) TYPE n, day(2) TYPE n, END OF t_date. FIELD-SYMBOLS <fs> TYPE t_date. ASSIGN sy-datum TO <fs> CASTING. WRITE / sy-datum. SKIP. WRITE: / <fs>-year , / <fs>-month, / <fs>-day. The output looks something like this: 1999/05/19 1999 05 19 In this example, the field symbol <fs>is fully typed using the local program type t_date. The SY-DATUM field can be treated like a structure as a result of the CASTING addition to the ASSIGN statement. This would not be possible without the CASTING addition, since SY-DATUM is incompatible with the field symbol type. Casting with an Explicit Type Declaration If the field symbol is neither fully typed nor generically typed, use the following form of the ASSIGN statement: ASSIGN ... TO <FS> CASTING TYPE <type>|LIKE <obj> [DECIMALS <d>]. When the system accesses the field symbol, the content of the assigned data object is interpreted as if it had the type declared in the statement. After the TYPE addition, you can declare the name of a data object enclosed in parentheses. The content of this data object indicates the data type at runtime. You cannot enter a type unless an explicit cast to the data object is logical. For example, there is no point casting a standard table to a sorted table, or an object reference to a data reference. Thus, you cannot declare table kinds (such as SORTED TABLE) or reference types (using REF TO) after the TYPE addition. If the data type is – or contains – a string, reference type, or internal table, these components must occur in the assigned data object. Otherwise, the system returns a syntax or runtime error. The data type declared in the TYPE or LIKE addition must be compatible with the generic type of the field symbol. The generic type can be left as it is or specialized. However, you cannot reset known technical attributes of the field symbol. For example, if the field symbol has the completely generic type ANY, you can declare another permitted generic type. If the field symbol has the generic type C, N, P, or X, you can only specify the length and the number of decimal places. You cannot make an explicit type declaration for a fully typed field symbol, since, if the field symbol is fully typed already, you cannot specialize it further. The system checks this statically if possible, or at runtime if not.
  9. 9. You should use the DECIMALS addition if the content of the assigned data object can be interpreted as a packed number. The field symbol is then given <d> decimal places. DECIMALS may only be used if no type was defined or if type P is defined. If no type is defined, type P is used. Moreover, you cannot use the LIKE and DECIMALS additions together. REPORT demo_field_symbols_casting_typ. TYPES: BEGIN OF t_date, year(4) TYPE n, month(2) TYPE n, day(2) TYPE n, END OF t_date. FIELD-SYMBOLS: <fs> TYPE ANY, <f> TYPE n. ASSIGN sy-datum TO <fs> CASTING TYPE t_date. WRITE / sy-datum. SKIP. DO. ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <f>. IF sy-subrc<> 0. EXIT. ENDIF. WRITE / <f>. ENDDO. The output looks something like this: 1999/05/19 1999 05 19 In this example, the field symbol <fs> is completely generic. A cast is performed on the field SY-DATUMto the local program type t_dateusing the CASTING addition to the ASSIGN statement. Field symbol <fs> can now be handled like a structure, but does not recognize any components. It therefore must be assigned to another field symbol <f> component by component. Obsolete Variants of the ASSIGN Statement The following additions to the ASSIGN statement are obsolete; you should no longer use them. They have been replaced by the above CASTING addition. Cast with built-in data types
  10. 10. To set the data type of a field symbol independently of the assigned object type (before the CASTING statement was introduced), you used the TYPE addition in the ASSIGN statement: ASSIGN ... TO <FS> TYPE <t>. However, you could only use the fixed-length elementary ABAP types (C, D, F, I, N, P, T, and X), ‘s’ for two-byte integers (with sign) and ‘b’ for one byte integers (without sign) as literals or variables for <t>. By contrast, the CASTING addition allows you declare any data type after the TYPE addition. There were situations where you could have used a TYPE addition: Typed field symbols The TYPE addition with a typed field symbol was useful whenever the assigned data object type is incompatible with the field symbol type, but you want to avoid triggering an error message. In this case, the specified type <t> must be compatible with the field symbol type. The field symbol then retains its data type, regardless of the assigned data object. You should now use casting with an implicit type declaration in this situation. Untyped field symbols If you use the TYPE addition, an untyped field symbol <FS> adopts the data type specified in <t> instead of the data type and output length of the data object assigned to it. If the TYPE addition is included in the ASSIGN statement and the field symbol is then used in the program, the content of the data object is interpreted as if it were a type <t> field. You should now use casting with an explicit type declaration in this situation. An error message occurs: if the data type declared is unknown; if the length of the data type declared is too short for the assigned field; or if the alignments of the data type and field symbol are incompatible. Example using the obsolete TYPE addition: DATA TXT(8) VALUE '19980606'. DATA mytype(1) VALUE 'X'. FIELD-SYMBOLS <fs>. ASSIGN txt TO <fs>. WRITE / <fs>. ASSIGN txt TO <fs> TYPE 'D'. WRITE / <fs>. ASSIGN TXT TO <FS> TYPE MYTYPE. WRITE / <fs>. Example using the CASTING addition:
  11. 11. DATA TXT(8) VALUE '19980606'. DATA mytype(1) VALUE 'X'. FIELD-SYMBOLS <fs>. ASSIGN txt TO <fs>. WRITE / <fs>. ASSIGN txt TO <fs> CASTING TYPE d. WRITE / <fs>. ASSIGN txt TO <fs> CASTING TYPE (mytype). WRITE / <fs>. In both cases, the system displays the following: 19980606 06061998 3139393830363036 In these examples, string TXT is assigned to field symbol <FS> three times: first without casting, then casting with type D and finally with casting with type X. The format of the second output line depends on the data in the user master record. The numbers in the last line are the hexadecimal codes of the characters in txt. They are platform-specific (in this case, ASCII). Casting decimal places To specify the number of decimal places in a field symbol when assigning it to a packed number of type P (before the CASTING statement was introduced), you used the DECIMAL addition in the ASSIGN statement: Syntax ASSIGN ... TO <FS> DECIMALS <d>. You can use the DECIMALS addition in any variant of the ASSIGN statement where the data object assigned to the field symbol has type P. In general, this leads to different numerical values for the field symbol and the assigned field. You can specify the number of decimal places <d> as a literal or a variable. If <d> is not between 0 and 14, or the data object assigned to the field symbol is not a type P field, a runtime error occurs. Example using the obsolete DECIMALS addition:
  12. 12. DATA: pack1 TYPE p DECIMALS 2 VALUE '400', pack2 TYPE p DECIMALS 2, pack3 TYPE p DECIMALS 2. FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i. WRITE: / 'PACK1', pack1. ASSIGN pack1 TO <f1> DECIMALS 1. WRITE: / '<F1> ', <f1>. pack2 = <f1>. WRITE: / 'PACK2', pack2. ASSIGN pack2 TO <f2> DECIMALS 4. WRITE: / '<F2> ', <f2>. pack3 = <f1> + <f2>. WRITE: / 'PACK3', pack3. <f2> = '1234.56789'. WRITE: / '<F2> ', <f2>. WRITE: / 'PACK2', pack2. Example using the CASTING addition: DATA: pack1 TYPE p DECIMALS 2 VALUE '400', pack2 TYPE p DECIMALS 2, pack3 TYPE p DECIMALS 2. FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i. WRITE: / 'PACK1', pack1. ASSIGN pack1 TO <f1> CASTING TYPE p DECIMALS 1. WRITE: / '<F1> ', <f1>. pack2 = <f1>. WRITE: / 'PACK2', pack2. ASSIGN pack2 TO <f2> CASTING TYPE p DECIMALS 4. WRITE: / '<F2> ', <f2>. pack3 = <f1> + <f2>. WRITE: / 'PACK3', pack3. <f2> = '1234.56789'. WRITE: / '<F2> ', <f2>. WRITE: / 'PACK2', pack2. In both cases, the list appears as follows:
  13. 13. PACK1 400.00 <F1> 4,000.0 PACK2 4,000,00 <F2> 40.0000 PACK3 4,040.00 <F2> 1.234,5679 PACK2 123,456.79 Each of the three type P fields has two decimal places. The field symbols <f1> and <f2> have one and four decimal places each. Note that the numeric values are different for the field symbols and for their assigned fields. Reference Variables Reference variables contain references. The actual contents of a reference variable, namely the value of a reference, is not visible in an ABAP program. ABAP contains data references and object references. Consequently, there are two kinds of data reference variables - data reference variables and object reference variables. You create the data type of a data reference variable using: TYPES <t_dref> TYPE REF TO DATA. You can create a data reference variable either by referring to the above data type or using: DATA <dref> TYPE REF TO DATA. Reference variables are handled in ABAP like other data objects with an elementary data type. This means that a reference variable can be defined as a component of a complex data object such as a structure or internal table as well as a single field. Reference variables are initial when you declare them. They do not point to an object. You cannot deference an initial reference variable. A data reference variable can point to a data object if you Use it to create a data object dynamically. Use it to get a reference to a data object. Assign an existing data reference to it from another data reference variable. You can assign references between data reference variables using the MOVE statement or the assignment operator (=), and also when you fill an internal table or pass interface parameters to a procedure. If you assign references in this way, the operands must be typed as data reference variables. You cannot assign to object reference variables or other variables. After the assignment, the reference in the target variable points to the same data object as the reference in the source variable.
  14. 14. Creating Data Objects Dynamically All of the data objects that you define in the declaration part of a program using statements such as DATA are created statically, and already exist when you start the program. To create a data object dynamically during a program, you need a data reference variable and the following statement: CREATE DATA <dref> TYPE <type>|LIKE <obj>. This statement creates a data object in the internal session of the current ABAP program. After the statement, the data reference in the data reference variable <dref> points to the object. The data object that you create does not have its own name. You can only address it using a data reference variable. To access the contents of the data object, you must dereference the data reference. You must specify the data type of the object using the TYPE or LIKE addition. In the TYPE addition, you can specify the data type dynamically as the contents of a field (this is not possible with other uses of TYPE). CREATE DATA <dref> TYPE (<name>). Here, <name> is the name of a field that contains the name of the required data type. Getting References to Data Objects The following statements allow you to place a data reference to an existing data object in a reference variable: GET REFERENCE OF <obj> INTO <dref>. <obj> can be a statically-declared data object, or a field symbol pointing to any object (including a dynamic object). If you specify a field symbol to which an object has not yet been assigned, a runtime error occurs. If you place a reference to a local field in a procedure in a global reference variable, the reference will become invalid when you leave the procedure. You cannot then deference the reference variable. Dereferencing Data References To access the contents of the data object to which a data reference is pointing, you must dereference it. ASSIGN <dref>->* TO <FS> [CASTING ...]. This statement assigns the data object to which the data reference in the reference variable <dref> to the field symbol<FS>. If the assignment is successful, SY-SUBRC is set to zero. If the field symbol is fully generic, it adopts the data type of the data object. If the field symbol is partially or fully typed, the system checks the data types for compatibility. Casting is also possible for the assigned data object. If the data reference in <dref> is initial or invalid, you cannot dereference it. The field symbol remains unchanged, and SY-SUBRC is set to 4.
  15. 15. If you create a data object dynamically, the only way to access its contents is to use dereferencing. Data References: Example TYPES: BEGIN OF t_struct, col1 TYPE i, col2 TYPE i, END OF t_struct. DATA: dref1 TYPE REF TO data, dref2 TYPE REF TO data. FIELD-SYMBOLS: <fs1> TYPE t_struct, <fs2> TYPE i. CREATE DATA dref1 TYPE t_struct. ASSIGN dref1->* TO <fs1>. <fs1>-col1 = 1. <fs1>-col2 = 2. dref2 = dref1. ASSIGN dref2->* TO <fs2> CASTING. WRITE / <fs2>. GET REFERENCE OF <fs1>-col2 INTO dref2. ASSIGN dref2->* TO <fs2>. WRITE / <fs2>. The output is: 1 2 This example declares two data reference variables dref1 and dref2. dref1 is used to create a structured dynamic data object. This is dereferenced with the field symbol <fs1>, and values are then assigned to it. dref1 is assigned to dref2. dref2 then points to the structured data object. When dref2 is dereferenced, it is cast to the elementary type i of field symbol <fs2>. Its first component is assigned to the field symbol. GET REFERENCE is then used to get a reference to the second component not the structured data object in dref2. It is dereferenced without casting. Basics Internal table processing is essential part of any ABAP program. Generally, we use the explicit work area to process the internal table like appending & modifying records. We can reduce the time and improve the performance of the program by using the field- symbols.
  16. 16. When we use the LOOP construct with the explicit work area, system need to engage the resources to put the required record in the work area, process it and move it back to the table if the needed. This additional processing time could be saved by using the field- symbol. By using the field-symbols we can save this additional time and improve the performance. Field-symbols are similar to dereferenced pointers in C. While using the field-symbol, system uses the same memory allocated to that particular field in the record instead of moving it to work area and processing. More on field-symbols can be found at: Field-Symbols on SAP Help. This code snippet shows how to use the field-symbols to process the loop with time measurement. Demo to measure performance Check out this demo application to measure the performance while using Field-Symbols and workares. *&---------------------------------------------------------------------* *& Illustrate the performance gain by using the field-symbols *& over header areas *&---------------------------------------------------------------------* * REPORT ztest_np_loop_fs. * DATA: i_bsegTYPE STANDARD TABLE OF bseg, wa_bsegLIKE LINE OF i_bseg. * DATA: lv_flagTYPE flag, lv_sta_timeTYPE timestampl, lv_end_timeTYPE timestampl, lv_diff_w TYPE p DECIMALS 5, lv_diff_f LIKE lv_diff_w, lv_save LIKE lv_diff_w. * FIELD-SYMBOLS: <fs_bseg>LIKE LINE OF i_bseg. * * data selection = 10,000 records SELECT * FROM bsegINTO TABLE i_bsegUP TO 100 ROWS. * * Begin - Processing with Work area GET TIME STAMP FIELD lv_sta_time. LOOP AT i_bsegINTO wa_bseg. IF lv_flag= 'X'. wa_bseg-sgtxt= 'TEST'. MODIFY i_bsegFROM wa_bseg. ENDIF. CLEAR wa_bseg. IF lv_flagIS INITIAL.
  17. 17. lv_flag= 'X'. ENDIF. ENDLOOP. GET TIME STAMP FIELD lv_end_time. lv_diff_w= lv_end_time- lv_sta_time. WRITE: /(15) 'Work area', lv_diff_w. * End - Processing with Work Area * CLEAR: lv_flag, lv_sta_time, lv_end_time. * Begin - Processing with Field-Symbols GET TIME STAMP FIELD lv_sta_time. LOOP AT i_bsegASSIGNING <fs_bseg>. IF lv_flag= 'X'. <fs_bseg>-sgtxt= 'TEST'. ENDIF. IF lv_flagIS INITIAL. lv_flag= 'X'. ENDIF. ENDLOOP. GET TIME STAMP FIELD lv_end_time. lv_diff_f= lv_end_time- lv_sta_time. WRITE: /(15) 'Field-Symbol', lv_diff_f. * End - Processing with Work Area * * Net time saving lv_save= lv_diff_w- lv_diff_f. WRITE: /(15) 'Total Save', lv_save. WRITE: / 'Done'. Advertisement Some statistics: In this performance measurement, time taken by the work area to process is considered as the 100%. By using the field-symbols, we can definitely improve the performance.

×