Using Field Symbols To Process Internal Tables

After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Process internal tables using field symbols

Using Field Symbols To Access Internal Tables

You have already learned how to iterate over an internal table using a structure as a work area. When you use this technique, the system copies data from the internal table into the work area. You then change the contents of the work area before copying them back into the internal table using the MODIFY statement.

If you use this technique to process a large internal table, you risk causing performance problems because of the costs of copying the data back and forth. You can reduce these costs by replacing the work area with a field symbol.

Replacing a Work Area With a Field Symbol

A field symbol is a pointer. A pointer is a data object that knows the memory address of a different object and allows you to manipulate that object. In the case of internal tables, the pointer allows you to address a line of an internal table without first copying it into a work area. Since you work directly with the table line and not with a work area, you do not have to copy your changes back into the internal table.

To define a field symbol, you use the FIELD-SYMBOLS statement and assign a name to the field symbol. The name must be included in angled brackets. You also assign a type to the field symbol. If you want to use a field symbol to process an internal table, you define the field symbol with the line type of the internal table, just as you would with a work area.

To use the field symbol in a loop over an internal table, you use the ASSIGNING addition in the LOOP statement. In each loop pass, the pointer now points to the corresponding line of the internal table and you can change its contents directly. As the field symbol has the line type of the internal table, you can treat it as you would a corresponding structure. After the name of the field symbol, you can type a dash, followed by the field that you want to change.

Since you are working directly with the internal table and not with a structure, there is no need for the MODIFY statement. By avoiding copying data, you can improve the performance of the loop considerably.

Runtime Comparison

Here you can see the result of a runtime analysis in which an internal table is updated using a MODIFY statement and another is updated using a field symbol. In this case, the field symbol takes 40% less time than the modify statement. As a rule, you can expect to save 25–40% of the runtime when you update an internal table using a field symbol instead of a work area. The performance gain for read operations is smaller.

Try It Out: Using Field Symbols To Access Internal Tables

  1. Create a new class that implements the interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    METHOD if_oo_adt_classrun~main.
    * Execute this class using Profile As->ABAP Application
    * In the analysis, look at the comparative runtimes of
    * the methods loop_work_area( ) and loop_field_symbol( )
    data(flights) = new lcl_demo( ).
    flights->use_work_area( ).
    flights->use_field_symbol( ).
    out->write( 'Done' ).
    Copy code
  3. Switch to the Local Types tab and copy the following code snippet into the editor:
    Code snippet
    CLASS lcl_demo DEFINITION.
    METHODS use_work_area.
    METHODS use_field_symbol.
    TYPES t_flights TYPE STANDARD TABLE OF /dmo/flight WITH NON-UNIQUE KEY carrier_id connection_id flight_date.
    METHODS loop_field_symbol CHANGING c_flights TYPE t_Flights.
    METHODS loop_Work_area CHANGING c_flights TYPE t_flights.
    METHOD use_field_symbol.
    DATA flights TYPE t_flights.
    SELECT FROM /dmo/flight FIELDS * INTO TABLE @flights.
    loop_field_symbol( CHANGING c_flights = flights ).
    METHOD use_work_area.
    DATA flights TYPE t_flights.
    SELECT FROM /dmo/flight FIELDS * INTO TABLE @flights.
    loop_work_area( CHANGING c_flights = flights ).
    METHOD loop_field_symbol.
    -seats_occupied += 1.
    METHOD loop_work_area.
    LOOP AT c_flights INTO DATA(flight).
    flight-seats_occupied += 1.
    MODIFY c_flights FROM flight.
    Copy code
  4. Select Ctrl + F3 to activate the class.
  5. Right-click in the Editor and choose Profile As...ABAP Application (Console).
  6. In the Details, uncheck the checkbox SQL database access, and choose Finish.
  7. Switch to the ABAP Profiler perspective and double-click on the trace item (you may need to refresh the display first) .
  8. Choose Hit List.
  9. Compare the runtimes of the methods loop_Work_area( ) and loop_field_symbol( ).

Save progress to your learning plan by logging in or creating an account