Declaring a Complex Internal Table


After completing this lesson, you will be able to:

  • Declare a complex internal table

Properties of Complex Internal Tables

The internal tables we have used so far had a scalar data type as their row type. In the example shown in the figures here, the row type of internal table numbers is the ABAP built-in type I.

We refer to these kinds of internal tables as simple internal tables.

We speak of a complex internal table if the row type is a structured data type.

While a simple internal table has only one nameless column, a complex internal table consists of several columns, each of them with the name and type of the corresponding component of the structured row type. In the example, the row type of internal table connection is a structured type with five components: carrier_id, connection_id, airport_from_id, airport_to_id, and carrier_name. Consequently, internal table connections has five columns with those names.

The columns in the examples in the figures, Reminder: Simple Internal Tables and Internal Tables with Structured Row Type, all have scalar types. More generally, a column of an internal table could also be of structured type or even have a table type. In the latter case, we talk of a nested internal table.

Up to now, we have addressed the rows of an internal table by their position. This is called an index access.

With the named columns of a complex internal table, key access becomes more important. Key access means addressing a row of the internal table by looking for particular values in particular columns. The columns in which you search can be any columns of the internal table.

Index access to an internal table is always very fast, even if the internal table contains many rows. Key access, however, can become very slow if the table contains a lot of rows. Choosing the right access type for the internal table can improve the performance of a key access.

Every internal table has one of three access types. The access type determines how data is stored in the table and, based on that, how the system reads the table to retrieve the data.

The different types of tables are as follows:

Standard Table
In a standard table, the contents are not stored in a particular sort order. By default, new records are appended to the end of the table. In order to retrieve data by key, the system must read it sequentially, which can lead to long retrieval times if the table is very large. The simple internal tables we used so far were standard tables.
Sorted Table
In a sorted table, the contents of the table are always sorted according to the key fields in ascending order. When you insert a new record into the table, the system ensures that it is placed at the correct position. Since the data is always sorted, the system can retrieve records more efficiently than from a standard table (as long as you follow particular rules).
Hashed Table
Hashed tables are managed using a special hash algorithm. This ensures that the system can retrieve records very quickly even if the table is extremely large. However, this performance gain only works in very particular cases.

Every internal table has a key. In standard tables, the key does not play a particularly significant role. For sorted and hashed tables, the key is very important as it determines the way in which the data will be managed in the table. Crucially, sorted and hashed tables are only faster for key access that addresses all or at least a subset of the key fields.

A further attribute of the table key is its uniqueness. You will sometimes want to allow duplicate entries in an internal table, and sometimes you will want to ensure that the key is unique. Here, the following rules apply:

  • Duplicates are always allowed in standard tables
  • Duplicates are never allowed in hashed tables
  • For a sorted table, you choose in the definition whether the key is to be unique or non-unique.
Internal tables may also have secondary keys. Secondary keys are a way of improving the performance of key accesses to internal tables that use different combinations of fields. You will find more information about secondary keys in the ABAP syntax documentation.

Complex Table Types

Watch this video to see some examples for the declaration of complex internal tables.

Furthermore, it is good programming style to define the data type first and then to create a variable that refers to the type.

Instead of specifying the access type and key of an internal table in the DATA statement, you should use a named table type. If you need the table type only locally in a method or in connection with a given class, you can define it using the TYPES statement.

The example defines a structured type st_connection, first. With this structured type as row type it then defines table type tt_connections. Finally, the declaration of internal table connections_5 refers to the table type.

If you need the table type globally, you can use a global table type.

A global table type is a repository object that can be used as data type anywhere in the system. ADT provides a dedicated editor for this kind of repository object. The tool consists of the following frames:

The following video shows how to define table types locally.

Try It Out: Complex Table Types

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method, if_oo_adt_classrun~main( ):
    Code snippet
        TYPES: BEGIN OF st_connection,
                 carrier_id      TYPE /dmo/carrier_id,
                 connection_id   TYPE /dmo/connection_id,
                 airport_from_id TYPE /dmo/airport_from_id,
                 airport_to_id   TYPE /dmo/airport_to_id,
                 carrier_name    TYPE /dmo/carrier_name,
               END OF st_connection.
    * Example 1 : Simple and Complex Internal Table
        " simple table (scalar row type)
        DATA numbers TYPE TABLE OF i.
        " complex table (structured row type)
        DATA connections TYPE TABLE OF st_connection.
        out->write(  `--------------------------------------------` ).
        out->write(  `Example 1: Simple and Complex Internal Table` ).
        out->write( data = numbers
                    name = `Simple Table NUMBERS:`).
        out->write( data = connections
                    name = `Complex Table CONNECTIONS:`).
    * Example 2 : Complex Internal Tables
        " standard table with non-unique standard key (short form)
        DATA connections_1 TYPE TABLE OF st_connection.
        " standard table with non-unique standard key (explicit form)
        DATA connections_2 TYPE STANDARD TABLE OF st_connection
                                WITH NON-UNIQUE DEFAULT KEY.
        " sorted table with non-unique explicit key
        DATA connections_3  TYPE SORTED TABLE OF st_connection
                                 WITH NON-UNIQUE KEY airport_from_id
        " sorted hashed with unique explicit key
        DATA connections_4  TYPE HASHED TABLE OF st_connection
                                 WITH UNIQUE KEY carrier_id
    * Example 3 : Local Table Type
        TYPES tt_connections TYPE SORTED TABLE OF st_connection
                                  WITH UNIQUE KEY carrier_id
        DATA connections_5 TYPE tt_connections.
    * Example 4 : Global Table Type
        DATA flights  TYPE /dmo/t_flight.
        out->write(  `------------------------------------------` ).
        out->write(  `Example 4: Global Table TYpe /DMO/T_FLIGHT` ).
        out->write(  data = flights
                     name = `Internal Table FLIGHTS:` ).
  3. Press CTRL + F3 on your keyboard to activate the class and F9 to execute the console app.
  4. Analyze the console output. Debug the program, play around with the source code to get familiar with the concepts.

Log in to track your progress & complete quizzes