Defining Dictionary Table Types

Objectives

After completing this lesson, you will be able to:

  • Define a dictionary table type
  • Define a nested table type

Dictionary Table Types

Creating a dictionary table type

Play the video to learn how to create a dictionary table type.

Dictionary table types

The editor for dictionary objects of type Table Type is form-based and not code-based as the editors are for structures and database tables. The editor consists of the following four sections:

Row Type

This frame contains the origin and name of the row type. When you create a new table type, it uses predefined dictionary type CHAR with a length of 1.

Initialization and Access

This frame contains the definition of the table access category (standard/sorted/hashed). By default, the access category is set to Standard Table.

The initial number of rows can be used to give the ABAP runtime a hint about the size of the initial memory requirement. With the default value 0, the ABAP runtime allocates the initial memory area fully by itself. Note that in most cases it is best to keep the default value 0 unchanged.

Key Overview

This frame provides an overview of the keys. Choose the key for which you want to see details.

Every table type contains the definition of a primary key. Secondary keys are optional. The table type in the example does not contain any secondary keys.

Key Details

This frame displays details for the selected key. This may be for the key category, that is, whether the key is unique or non-unique, and the key definition. In the example, the primary key is non-unique and consists of the entire row type.

When we compare the definition of a dictionary table type and a table type that is defined with ABAP statement TYPES, you can easily identify the different parts:

  • The row type is specified after the addition OF.

    Note

    The syntax of the TYPES statement only allows one word here. When using an ABAP built-in type that requires a length (incomplete ABAP type), the row type has to be defined in a separate TYPES statement.
  • The table access category is specified after the addition TYPE.
  • The primary key definition is specified after the addition WITH. Key definition value Row Type, corresponds to table_line in the ABAP syntax.

Options for Defining the Row Type

When you define a table type you have several options to specify the row type:

Dictionary Type
This is the most common row type category. The row type is specified though another dictionary object, most often a structure. If the row type is a data element, the internal table will have one column only that does not have a name to it.
Predefined Type
Technically, using a predefined type as row type is identical to using a data element. What is missing is the additional semantics of a data element.
Reference to
With one of these options the row type is a reference type. The internal table will have one nameless column containing a list of references, either to data objects or to instances of classes.
Range Table on
With one of these options the table type defines the type of a ranges table. Although you specify only a predefined type or a data element, the ranges table will have four columns: OPTION, SIGN, LOW, and HIGH. For more information on the usage of ranges tables refer to the ABAP documentation.

When you define the row type of a new table type, you can search for existing dictionary types. You will find data elements, structures, but also database tables and table types. Place the cursor in the input field Type Name and press Ctrl + Space, or choose Browse.

Hint

If the specified dictionary type exists, a left-click on Type Name navigates to the definition.

Caution

Even though table types are offered here it is not meaningful to use them as row types. The resulting "Table of Tables" is difficult to handle because there is no easy way to identify the rows of the outer table. Use structures with table-like components instead. We will discuss those nested tables later.

Access Category and Keys

The table category is a technical property of the table type that specifies the storage and primary access method. The default value is Standard Table. Other values are:

Sorted Table
The content of the internal table is sorted by its primary key at any time. The ABAP runtime optimizes access using binary searches where possible. Restrictions apply for write accesses.
Hashed Table
The content of the internal table is administrated using a hash table. The ABAP runtime optimizes access using a hash algorithm where possible. Restrictions apply for read and write accesses.
Index Table and Not Specified
With these options the table type is not completely specified. Such generic types cannot be used to type data objects. Their usage is restricted to parameters and field symbols. To work with generic data types requires dynamic programming techniques that we do not cover in this ABAP core skills training. For details refer to the ABAP documentation.

For defining the primary key of a table type the following options exist:

Standard Key

The standard key (also known as default key) consists of all non-numeric components of the row type.

Row Type

With this option the primary key consists of all components of the row type. If the row type is unstructured, the one column is the key column.

Not Specified

With this option no primary key is defined. The table type is generic and cannot be used for ABAP data objects.

Key Components

The primary key consists of the explicitly specified components. When you choose this option, the editor displays a new section below the Primary Key Details section. In this section you enter must then enter the key components.

Note

Press Ctrl + Space to choose from the components of the row type.

Note

Remember that the primary key of internal tables is not necessarily unique. For standard tables and sorted tables, non-unique primary keys are also supported. For standard tables it is actually the only option. This is opposed to database tables where the primary key is always a unique key.

Secondary key definition

Play the video to learn about secondary key definition.

How to Define a Dictionary Table Type

Play the video to see how to define a Dictionary Table Type.

Nested Table Types

You can use a table type to type the component of a structure type. In the example, component addresses is typed with table type zs4d430T_addresses. The ABAP code on the right-hand side illustrates how you define exactly the same structure type in ABAP.

Note

A structure with a table-like component is often referred to as a deep structure. The opposite is a flat structure. This has to do with the memory layout of such a data object in ABAP. For details on deep and flat structures, refer to the ABAP documentation.

When you use a structure with a table-like component as row type of another table type, this table type becomes nested. In the example, structure ZS4D430S_PERSON_DEEP is used as row type of table type ZS4D430T_NESTED. Column addresses of the resulting internal table contains an internal table in each row.

Note

In each row of the outer table, the inner table can have a different number of rows.

Define and Use Dictionary Table Types

You need some table types that you can access in several global classes. You decide to define table types in the ABAP Dictionary.

Hint

Before you define table types in the ABAP Dictionary, you should investigate the option to define public table types in one of the global classes or in a global interface.

Template:

  • /LRN/CL_S4D430_TYT_TABLE_TYPE (Global Class)

Solution:

  • /LRN/CL_S4D430_TYS_TABLE_TYPE (Global Class)
  • /LRN/T_ADDRESSES (Table Type)
  • /LRN/S_PERSON_DEEP (Structure Type)
  • /LRN/T_PERSONS (Table Type)

Task 1: Define a Table Type

Create a copy of global class /LRN/CL_S4D430_TYT_TABLE_TYPE (suggested name: ZCL_##_TABLE_TYPE, where ## is your group number). Replace public table type tt_addresses with a dictionary table type (suggested name: Z##T_ADDRESSES).

Steps

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

    1. In the Project Explorer view, right-click class /LRN/CL_S4D430_TYT_TABLE_TYPE to open the context 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_##_TABLE_TYPE, where ## stands for your group number.

    4. Adjust the description and choose Next.

    5. Confirm the transport request and choose Finish.

  2. In the PUBLIC SECTION of the class definition, analyze the definition of type tt_addresses.

    1. Perform this step as before.

  3. Create a new dictionary object of type Table Type (suggested name: Z##T_ADDRESSES).

    1. In the Project Explorer view, expand your package and open the context menu on subnode Dictionary.

    2. Choose NewTable Type.

    3. Confirm the package, enter the name Z##T_ADDRESSES and the description Addresses, then choose Next.

    4. Assign the new object to a transport request and choose Finish.

  4. Specify your structure type Z##S_ADDRESS as row type.

    Note

    If you have not finished the previous exercise, you can use the structure type /LRN/S_ADDRESS, instead.
    1. In the Row Type section, change the Category from Predefined Type to Dictionary.

    2. In the Type Name field, enter /LRN/S_ADDRESS or Z##S_ADDRESS.

  5. Specify the same table kind and key definition as in table type tt_addresses.

    1. In the Initialization and Access section, change the table kind from Standard Table to Sorted Table.

    2. In the Key Overview section, select <Primary Key>.

    3. In the Primary Key Details section, change the Key Definition from Standard Key to Key Components.

    4. In the Key Components section, use code completion to enter COUNTRY and CITY as key fields.

  6. Activate the table type.

    1. Press Ctrl + F3 to activate the development object.

  7. Return to the source ode of your global class. In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace tt_addresses with your new dictionary structure type. Make sure there are no syntax errors.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678910111213141516171819
      * Task 1 ********************************************************************** * DATA addresses TYPE tt_addresses. DATA addresses TYPE z##t_addresses. addresses = VALUE #( ( street = 'Dietmar-Hopp-Allee 16' postal_code = '69190' city = 'Walldorf' country = 'DE' ) ( street = '3999 West Chester Pike' postal_code = '19073' city = 'Newtown Square, PA' country = 'US' ) ).

Task 2: Define a Deep Structure

In your global class, remove or comment public structure type st_person_deep and replace it with a dictionary structure (suggested name: Z##S_PERSON_DEEP). For the addresses, re-use the table type you already created.

Steps

  1. Create a new dictionary object of type Structure.

    1. In the Project Explorer view, expand your package and open the context menu on subnode Dictionary.

    2. Choose NewStructure.

    3. Confirm the package, enter the name Z##S_PERSON_DEEP and the description Person (Name and Several Addresses), then choose Next.

    4. Assign the new object to a transport request and choose Finish.

  2. Remove the placeholder component_to_be_Changed : abap.string(0);from the component list of the structure and replace it with a component for the first_name (suggested name:first_name), the last name (suggested name: last_name).

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567
      define structure z##s_person_deep { first_name : /dmo/first_name; last_name : /dmo/last_name; }
  3. Add another component for the addresses of the person (suggested name: addresses). As type, use the table type you created in the previous task.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678
      define structure z##s_person_deep { first_name : /dmo/first_name; last_name : /dmo/last_name; addresses : z##t_addresses; }
  4. Activate the structure type.

    1. Press Ctrl + F3 to activate the development object.

  5. Return to the source code of your global class. In the PUBLIC SECTION of the class definition, remove or comment the definition of type st_person_deep.

    1. Perform this step as before.

  6. In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace st_person_deep with your new dictionary structure type. Make sure there are no syntax errors.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678910
      * Task 2 ********************************************************************** * DATA person TYPE st_person_deep. DATA person TYPE /lrn/s_person_deep. person-first_name = 'Dictionary'. person-last_name = 'ABAP'. person-addresses = addresses.

Task 3: Define a Nested Table Type

In your global class, remove or comment public table type tt_persons and replace it with a dictionary table type (suggested name: Z##T_PERSONS). As its row type, use the deep structure type you created in the previous task.

Steps

  1. Create a new dictionary object of type Table Type.

    1. In the Project Explorer view, expand your package and open the context menu on subnode Dictionary.

    2. Choose NewTable Type.

    3. Confirm the package, enter the name Z##T_PERSONS and the description Persons (With Name and Addresses), then choose Next.

    4. Assign the new object to a transport request and choose Finish.

  2. Specify your structure type Z##S_PERSON_DEEP as the row type.

    1. In the Row Type section, change the Category from Predefined Type to Dictionary.

    2. In the Type Name field, enter Z##S_PERSON_DEEP.

  3. Specify the same table kind and key definition as in table type tt_persons.

    1. In the Initialization and Access section, change the table kind from Standard table to Hashed Table.

    2. In the Key Overview section, select <Primary Key>.

    3. In the Primary Key Details section, change the Key Definition from Standard Key to Key Components and the Key Category from Non-Unique to Unique.

    4. In the Key Components section, specify the key fields LAST_NAME and FIRST_NAME.

  4. Activate the table type.

    1. Press Ctrl + F3 to activate the development object.

  5. Return to the source ode of your global class. In the PUBLIC SECTION of the class definition, remove or comment the definition of type tt_persons .

    1. Perform this step as before.

  6. In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace tt_persons with your new dictionary table type. Make sure there are no syntax errors.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123456789101112131415161718192021222324252627282930313233
      * Task 3 ********************************************************************** * DATA persons TYPE tt_persons. DATA persons TYPE /lrn/t_persons. persons = VALUE #( ( person ) ( first_name = 'CDS' last_name = 'ABAP' addresses = VALUE #( ( street = 'SAP-Allee 29' postal_code = '68789' city = 'St.Leon-Rot' country = 'DE' ) ( street = '35 rue d''Alsace' postal_code = '92300' city = 'Levallois-Perret' country = 'FR' ) ( street = 'Bedfont Road' postal_code = 'TW14 8HD' city = 'Feltham' country = 'GB' ) ) ) ).
  7. Activate and debug the class as a console app. Analyze the content of the data objects addresses, person, and persons.

    1. Press Ctrl + F3 to activate the development object.

    2. Press F9 to execute the class as console app.

Log in to track your progress & complete quizzes