Get Started with ABAP Programming on SAP BTP

Declaring a Structured Data Object

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

After completing this lesson, you will be able to:

  • Declare structured data object

Structured Data Objects

Up until now, you have been using simple variables, each of which can store a single piece of information. Here, for example, there are two variables, one for the departure airport, the other for the arrival airport.

When you read a record from the database you need to hold all of this information together. The two variables in the example are completely independent of one another, and are therefore not suitable for storing different pieces of information that belong together.

In ABAP, the solution is to use a structured variable, or a structure, for short.

A structure is a variable ABAP data object with a name and a structured type.

In the example, data object connection_full is such a structure. It is subdivided into eight components, each of which also has a name and a type. You can address both the structure as a whole and the individual components. Importantly, you can use each component in exactly the same way that you would use a standalone simple variable.

There are various possibilities to declare a structure. You can define structured types with keyword TYPES or use a repository object of type Structure. The definitions of views and database tables can also serve as structured types. The example uses CDS View /DMO/I_Connection as a structured type.

In the debugger perspective, there are two ways to analyze the structure and content of a structured variable:

How To Analyze Structured Data Objects Using The Debug Perspective

Structured Data Types

A global structure type is a repository object that can be used as data type anywhere in the system. In the example, structured type SYMSG is used to declare variable message.

When you press the F2 key to display the details of this data type you can see that this is a structure type consisting of seven components. You can also see the names, technical types and descriptions of the components.

When you press the F3 key to navigate to the definition of the type, a new view opens with the ADT editor for global structured types.

The definition of a global structure type is very similar to the definition of a database table. The main part of the definition consists of a DEFINE STRUCTURE statement with the name of the structure type. This is followed by the list of structure components in a pair of curly brackets ( { , } ); each component with a component type. Component types are often described by data elements, but it is also possible to use structure types as component types. Structures with structured components are referred to as Nested Structures.

Additional code lines before the DEFINE STRUCTURE statement specify additional properties of the structure type, among them a label.

You can also define structured types in an ABAP program using the TYPES statement. The structure definition begins with the statement TYPES BEGIN OF <structure type name> and ends with TYPES END OF <structure type name>. In between, you name each component and specify its type in an additional TYPES statement.

A compact form uses keyword TYPES only once, followed by a colon( : ). The BEGIN OF addition, the END OF addition and the component definitions in between are then separated by commas.

This is referred to as chain statement.

Note
In the past, chain statements were used a lot in ABAP. Nowadays they are only recommended to combine statements that belong closely together.

In this example, a chain statement TYPES: is used to define local structured type st_connection which consists of the three components airport_from_id, airport_to_id, and carrier_name. Each component is typed with a data element beginning with /dmo/.

Local structured type st_connection is then used in a DATA statement to type structured variable connection.

In this example, variable connection has a nested structured type. Type st_nested defines 4 components. The first three are typed with data elements, therefore they are simple components. The fourth component message, however, is typed with a structured type. Therefore it is a structured component. This makes variable connection nested structure.

ABAP supports not only structured variables but also structured constants. To define a structured constant, use BEGIN OF and END OF as part of a CONSTANTS statement. The example shows a structured constant that is defined in the public section of global class CL_ABAP_BEHV. All four components are typed with data element SECKEYNAME. Remember that addition VALUE is mandatory when defining constants.

Try It Out: Structured Data 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
    
       * Example 1 : Motivation for Structured Variables
    **********************************************************************
    
        DATA connection_full TYPE /DMO/I_Connection.
    
        SELECT SINGLE
         FROM /dmo/I_Connection
       FIELDS AirlineID, ConnectionID, DepartureAirport, DestinationAirport,
              DepartureTime, ArrivalTime, Distance, DistanceUnit
        WHERE AirlineId    = 'LH'
          AND ConnectionId = '0400'
         INTO @connection_full.
    
        out->write(  `--------------------------------------` ).
        out->write(  `Example 1: CDS View as Structured Type` ).
        out->write( connection_full ).
    
    * Example 2: Global Structured Type
    **********************************************************************
    
        DATA message TYPE symsg.
    
        out->write(  `---------------------------------` ).
        out->write(  `Example 2: Global Structured Type` ).
        out->write( message ).
    
    * Example 3 : Local Structured Type
    **********************************************************************
    
        TYPES: BEGIN OF st_connection,
                 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.
    
        DATA connection TYPE st_connection.
    
        SELECT SINGLE
          FROM /DMO/I_Connection
        FIELDS DepartureAirport, DestinationAirport, \_Airline-Name
         WHERE AirlineID = 'LH'
           AND ConnectionID = '0400'
          INTO @connection.
    
        out->write(  `---------------------------------------` ).
        out->write(  `Example 3: Global Local Structured Type` ).
        out->write( connection ).
    
    * Example 4 : Nested Structured Type
    **********************************************************************
    
        TYPES: BEGIN OF st_nested,
                 airport_from_id TYPE /dmo/airport_from_id,
                 airport_to_id   TYPE /dmo/airport_to_id,
                 message         TYPE symsg,
                 carrier_name    TYPE /dmo/carrier_name,
               END OF st_nested.
    
        DATA connection_nested TYPE st_nested.
    
        out->write(  `---------------------------------` ).
        out->write(  `Example 4: Nested Structured Type` ).
        out->write( connection_nested ).
    
    Copy code
  3. Press CTRL + F3 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.

Save progress to your learning plan by logging in or creating an account