Declaring a Complex Internal Table

Objectives
After completing this lesson, you will be able to:

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.

Note
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:

Note

In this lesson, we will concentrate on standard tables. More information on sorted and hashed tables can be found in the ABAP documentation.

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.
Note
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

Here you will 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:

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
                                                     airport_to_id.
    
        " sorted hashed with unique explicit key
        DATA connections_4  TYPE HASHED TABLE OF st_connection
                                 WITH UNIQUE KEY carrier_id
                                                 connection_id.
    
    * Example 3 : Local Table Type
    **********************************************************************
    
        TYPES tt_connections TYPE SORTED TABLE OF st_connection
                                  WITH UNIQUE KEY carrier_id
                                                  connection_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:` ).
    
    Copy code
  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