Using Field Symbols to Process Internal Tables

Objective

After completing this lesson, you will be able to Process internal tables using field symbols.

Field Symbols for Access to 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: Field Symbols for Access to 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
    Copy code
    Switch to dark mode
    12345678910111213
    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' ).
  3. Switch to the Local Types tab and copy the following code snippet into the editor:
    Code Snippet
    Copy code
    Switch to dark mode
    12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
    CLASS lcl_demo DEFINITION. PUBLIC SECTION. METHODS use_work_area. METHODS use_field_symbol. PRIVATE SECTION. 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. ENDCLASS. CLASS lcl_demo IMPLEMENTATION. METHOD use_field_symbol. DATA flights TYPE t_flights. SELECT FROM /dmo/flight FIELDS * INTO TABLE @flights. loop_field_symbol( CHANGING c_flights = flights ). ENDMETHOD. METHOD use_work_area. DATA flights TYPE t_flights. SELECT FROM /dmo/flight FIELDS * INTO TABLE @flights. loop_work_area( CHANGING c_flights = flights ). ENDMETHOD. METHOD loop_field_symbol. LOOP AT c_flights ASSIGNING FIELD-SYMBOL(). -seats_occupied += 1. ENDLOOP. ENDMETHOD. METHOD loop_work_area. LOOP AT c_flights INTO DATA(flight). flight-seats_occupied += 1. MODIFY c_flights FROM flight. ENDLOOP. ENDMETHOD. ENDCLASS.
  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( ).

Use Field Symbols in Loops

You realize, that in method GET_FLIGHTS_BY_CARRIER, the comprehension only accesses three out of nine components of the work area. To improve performance, you assign a field symbol instead of copying the complete table row to the work area.

Template:

  • /LRN/CL_S4D401_ITS_PROCESSING (Global Class)

Solution:

  • /LRN/CL_S4D401_ITS_FSYM (Global Class)

Task 1: Copy Template (Optional)

Copy the template class. If you finished the previous exercise, you can skip this task and continue editing your class ZCL_##_SOLUTION.

Steps

  1. Copy class /LRN/CL_S4D401_ITS_PROCESSING to a class in your own package (suggested name: ZCL_##_SOLUTION, where ## stands for your group number).

    1. In the Project Explorer view, right-click class /LRN/CL_S4D401_ITS_PROCESSING to open the content menu.

    2. From the context menu, choose Duplicate ....

    3. Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_SOLUTION, where ## stands for your group number.

    4. Adjust the description and choose Next.

    5. Confirm the transport request and choose Finish.

  2. Activate the copy.

    1. Press Ctrl + F3 to activate the class.

Task 2: Use Field Symbol

Change the implementation of method GET_FLIGHTS_BY_CARRIER in local class LCL_PASSENGER_FLIGHT. In the comprehension, replace the work area with a field symbol.

Steps

  1. In local class LCL_PASSENGER_FLIGHT, navigate to the implementation of method GET_FLIGHTS_BY_CARRIER.

    1. Perform this step as in previous exercises.

  2. Adjust the VALUE statement so that in every iteration a field symbol is assigned to the current row of the internal table (suggested name for the field symbol: <flight>).

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123
      * r_result = VALUE #( * FOR flight IN flights_buffer
  3. Adjust the code inside the VALUE expression. Replace any usage of work area (flight) with a usage of the field symbol (<flight>).

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678
      * ( NEW lcl_passenger_flight( * i_carrier_id = flight-carrier_id * i_connection_id = flight-connection_id * i_flight_date = flight-flight_date * ) * )
  4. Activate and test your global class as console app.

    1. Press Ctrl + F3.

    2. Press F9.

Log in to track your progress & complete quizzes