Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Declarations →  Declaration Statements →  Data Types and Data Objects →  Declaring Data Types →  TYPES →  TYPES - TABLE OF 

Internal Tables, Nested Tables

This example demonstrates how nested internal tables are declared and used.

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.
    FIELD-SYMBOLS <fs> LIKE company.

    DATA(out) = cl_demo_output=>new( ).

<span class="blue">* Filling Internal Tables</span>
    company_tab = VALUE #(
      ( name   = 'Racing Bikes Inc.'
        addresses = VALUE #( ( street = 'Fifth Avenue'
                               city   = 'New York' )
                             ( street = 'Second Street'
                               city   = 'Boston' ) ) )
      ( name   = 'Chocolatiers Suisse'
        addresses = VALUE #( ( street = 'Avenue des Forets'
                               city   = 'Geneve' )
                             ( street = 'Kleine Bachgasse'
                               city   = 'Basel' )
                             ( street = 'Piazza di Lago'
                              city   = 'Lugano' ) ) ) ).
<span class="blue">* Reading Internal Tables</span>
    READ TABLE company_tab
         WITH TABLE KEY name = 'Racing Bikes Inc.'
         ASSIGNING <fs>.
    out->write( |{ <fs>-name }| ).
    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).
      out->write( output ).
    ENDLOOP.
<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.
      out->write( |{ company-name }| ).
      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).
        out->write( output ).
      ENDLOOP.
    ENDLOOP.
<span class="blue">* text output</span>
    out->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 the 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.

The internal table is filled using the value operator VALUE.

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. The content of the row is assigned to the structure address. Since addresses is an index table, sy-tabix can be evaluated.

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 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. The entries of the inner standard table, however, are sorted using the SORT command.