Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Declarative statemnts →  Data Types and Data Objects →  Declaring Data Types →  TYPES →  TYPES - TABLE OF 

Internal Tables, Nested Tables

The example demonstrates the declaration and use of nested internal tables.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_nested_internal_tables.

CLASS table_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
  PRIVATE SECTION.
    TYPES:
      BEGIN OF t_address,
        street TYPE c LENGTH 20,
        city   TYPE c LENGTH 20,
      END OF t_address,
      t_address_tab TYPE STANDARD TABLE
                    OF t_address
                    WITH NON-UNIQUE KEY city.
    CLASS-DATA:
      BEGIN OF company,
        name       TYPE c LENGTH 20,
        addresses  TYPE t_address_tab,
      END OF company,
      company_tab LIKE HASHED TABLE
                  OF   company
                  WITH UNIQUE KEY name,
      company_sorted_tab LIKE SORTED TABLE
                         OF   company
                         WITH UNIQUE KEY name.
ENDCLASS.

CLASS table_demo IMPLEMENTATION.
  METHOD main.
    DATA: address TYPE t_address,
          idx     TYPE sy-tabix,
          output  TYPE c LENGTH 80,
          TEXT    TYPE REF TO cl_demo_text.
    FIELD-SYMBOLS <fs> LIKE company.
<span class="blue">* Prepare text output</span>
    text = cl_demo_text=>get_handle( ).
<span class="blue">* Filling Internal Tables</span>
    company-name   = 'Racing Bikes Inc.'.
    address-street = 'Fifth Avenue'.
    address-city   = 'New York'.
    APPEND address TO company-addresses.
    address-street = 'Second Street'.
    address-city   = 'Boston'.
    APPEND address TO company-addresses.
    INSERT company INTO TABLE company_tab.
    CLEAR company.
    company-name   = 'Chocolatiers Suisse'.
    address-street = 'Avenue des Forets'.
    address-city   = 'Geneve'.
    APPEND address TO company-addresses.
    address-street = 'Kleine Bachgasse'.
    address-city   = 'Basel'.
    APPEND address TO company-addresses.
    address-street = 'Piazza di Lago'.
    address-city   = 'Lugano'.
    APPEND address TO company-addresses.
    INSERT company INTO TABLE company_tab.
<span class="blue">* Reading Internal Tables</span>
    READ TABLE company_tab
         WITH TABLE KEY name = 'Racing Bikes Inc.'
         ASSIGNING <fs>.
    WRITE <fs>-name TO output.
    text->add_line( output ).
    LOOP AT <fs>-addresses INTO address.
      CLEAR output.
      WRITE: sy-tabix       TO output+4(4),
             address-street TO output+8(20),
             address-city   TO output+28(20).
      text->add_line( output ).
    ENDLOOP.
    text->add_line( space ).
<span class="blue">* Modifying Internal Tables</span>
    address-street = 'Rue des Montagnes'.
    address-city   = 'Geneve'.
    READ TABLE company_tab
         WITH TABLE KEY name = 'Chocolatiers Suisse'
         INTO company.
    READ TABLE company-addresses TRANSPORTING NO FIELDS
               WITH TABLE KEY city = address-city.
    idx = sy-tabix.
    MODIFY company-addresses FROM address INDEX idx.
    MODIFY TABLE company_tab FROM company.
<span class="blue">* Moving and sorting Internal Tables</span>
    company_sorted_tab = company_tab.
    LOOP AT company_sorted_tab INTO company.
      WRITE company-name TO output.
      text->add_line( output ).
      SORT company-addresses.
      LOOP AT company-addresses INTO address.
        CLEAR output.
        WRITE: sy-tabix       TO output+4(4),
               address-street TO output+8(20),
               address-city   TO output+28(20).
        text->add_line( output ).
      ENDLOOP.
    ENDLOOP.
<span class="blue">* text output</span>
    text->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  table_demo=>main( ).

Description

The example shows the declaration of two internal tables t_address_tab and company_tab, where t_address_tab is contained in company_tab. For this purpose t_address_tab is defined as table type. The component addresses of the company structure is declared with this type. The data type of company is used as the row type for the tables company_tab and company_sorted_tab.

Using APPEND, the address structure is appended row by row onto the standard table company-addresses. However, in the hashed table company_tab you must use INSERT to insert company.

A row from company_tab is assigned to the field symbol <fs> using READ and its table-like component addresses is processed in a LOOP, whereby the content of the row is assigned to the structure address. Since addresses is an index table, sy-tabix can be analyzed.

To change the content of the component street of a row of the inner table addresses, the index of the relevant row is determined using a READ statement and it is then used in a MODIFY. To make the changes effective in the corresponding row of the outer hashed table as well, the key access of the MODIFY statement is used.

Finally, the hashed table is assigned a sorted table with the same row type. In this way, the entries are automatically sorted by the table key name. However, the entries of the inner standard table are sorted using the SORT command.

The wrapper CL_DEMO_TEXT of the Text Edit Control is used for output.