Ansicht
Dokumentation

ABAPSHEETS_STRUCTURES - SHEETS STRUCTURES

ABAPSHEETS_STRUCTURES - SHEETS STRUCTURES

Fill RESBD Structure from EBP Component Structure   Addresses (Business Address Services)  
This documentation is copyright by SAP AG.
SAP E-Book

Working with Structures

This cheat sheet provides information and tips on structures in a nutshell. Furthermore, it serves as a collection of syntax and code snippets in one place for your reference. For more details, refer to the respective topic in the ABAP Keyword Documentation.

Structures ...

  • are composed of a sequence of other data objects which are called components.
  • have components that can be of any type, that is, they can themselves be, for example, structures or internal tables.
  • summarize different pieces of information, that is, data objects, that belong together. A typical example is the following: an address has several components like name, street, city, and so on.
  • are often used to process data sequentially, such as the data stored in the rows and columns of a database table.
  • Most of the structures that are worked with in ABAP programs may be globally defined structures in the DDIC.

  • Why? Apart from globally defined structures in the DDIC, each database table or view constitutes a structured type. When a database table etc. is activated, a globally available structured type of the same name is created, too. Hence, in an ABAP program, a database table's name is typically used as type name to declare data objects, for example, structures or internal tables.

  • can be addressed as a whole. You can also address the individual components of structures.

See the topic Structures for more information.

Creating Structures and Structured Types

This cheat sheet focuses on locally defined structures and structured types.

The typical syntactical elements are BEGIN OF ... END OF ... are used in combination with TYPES to create a structured type and DATA to create a structure.

Creating structured types

The structures' components are listed between ... BEGIN OF ... and ... END OF ....

TYPES: BEGIN OF structured_type,
         comp1 TYPE ...,
         comp2 TYPE ...,
         comp3 TYPE ...,
         ...,
       END OF structured_type.

Alternatively, you could go with the following syntax, however, a chained statement with the colon : above is preferable due to better readability.

TYPES BEGIN OF structured_type.
TYPES comp1 TYPE ... .
TYPES comp2 TYPE ... .
TYPES comp3 TYPE ... .
... .
TYPES END OF structured_type.

As mentioned above, the components of structures can be of any type. They can be elementary and also complex, that is, a structure component can be a structure or internal table. Besides this, the components can be reference variables, too.

For the components, the additions TYPE and LIKE are possible. Setting default values with VALUE is - in contrast to the creation of structures using a DATA statement as shown further down - not possible in this case.

TYPES: BEGIN OF structured_type,
         comp1 TYPE i,
         comp2 TYPE c LENGTH 5,
         comp3 TYPE local_structured_type,
         comp4 TYPE itab,
         comp5 TYPE ddic_type,
         comp6 TYPE REF TO ...,
         comp7 like data_object,
         ...,
       END OF structured_type.

Creating structures

There are multiple ways to create structures in a program using a DATA statement:

  • Creating a structure either based on a local type or a globally available type from the DDIC:
DATA ls_from_local TYPE structured_type,
     ls_from_global TYPE sflight.
  • Creating a structure by directly specifying the components using ... BEGIN OF ... END OF .... The syntax is similar to the TYPES statement. Here, default values can be set with VALUE.
DATA: BEGIN OF structure,
        comp1 TYPE ...,
        comp2 TYPE ... VALUE ...,
        comp3 TYPE i VALUE 99,
        comp4 TYPE i VALUE IS INITIAL,
        comp5 TYPE local_structured_type,
        comp6 TYPE itab,
        comp7 TYPE ddic_type,
        comp8 TYPE REF TO ...,
        comp9 like data_object,
        ...,
      END OF structure.
Alternatively and similar to the TYPES statement, you could use the following syntax, however, a chained statement with the colon : as above is preferable due to better readability.
DATA BEGIN OF structure.
DATA comp1 TYPE ... .
DATA comp2 TYPE ... VALUE ... .
... .
DATA END OF structure.
  • Creating a structure by referring to local structured data object using LIKE.
DATA some_struc LIKE other_struc.

Variants of Structures

Depending on the component type, the structure can be a flat structure, a nested structure, or a deep structure.

Flat structure

Flat structures contain only elementary types that have a fixed length, that is, there are no internal tables, reference types or strings as components:

DATA: BEGIN OF structure,
        comp1 TYPE i,
        comp2 TYPE c LENGTH 15,
        comp3 TYPE p LENGTH 8 DECIMALS 2,
        ...,
      END OF structure.

Nesting does not play a role in this context. Even a nested structure is flat unless a substructure contains a deep component.

Nested structure

At least one component of a structure is a substructure, that is, it refers to another structure. The following example has multiple substructures.

DATA: BEGIN OF address_n,
        BEGIN OF name,
          title   TYPE string VALUE `Mr.`,
          prename TYPE string VALUE `Duncan`,
          surname TYPE string VALUE `Pea`,
        END OF name,
        BEGIN OF street,
          name   TYPE string VALUE `Vegetable Lane`,
          number TYPE string VALUE `11`,
        END OF street,
        BEGIN OF city,
          zipcode TYPE string VALUE `349875`,
          name    TYPE string VALUE `Botanica`,
        END OF city,
      END OF address_n.

Deep structure

A deep structure contains at least one internal table, reference type, or string as a component.

DATA: BEGIN OF address_d,
        name   TYPE string VALUE `Mr. Duncan Pea`,
        street TYPE string VALUE `Vegetable Lane 11`,
        city   TYPE string VALUE `349875 Botanica`,
        contact_details TYPE TABLE OF string WITH EMPTY KEY,
      END OF address_d.

Despite that fact that the following structure looks fairly simple, it is not a flat structure but a deep structure since it contains strings.

DATA: BEGIN OF address,
        name   TYPE string VALUE `Mr. Duncan Pea`,
        street TYPE string VALUE `Vegetable Lane 11`,
        city   TYPE string VALUE `349875 Botanica`,
      END OF address.

Working with Structures

Accessing Components of Structures

The structure as a whole and also the individual components can be addressed. To address the components, you use the structure component selector -:

structure-substructure-comp1 ...
address-name ...

Nested components can be addressed via chaining:

struct-substruc-comp1 ...
address_n-name-title ...

Notes

  • You can refer to structure components when creating new data types and data objects:
TYPES: lty_1 TYPE structured_type-comp1,
       lty_2 LIKE structure-comp1.

DATA: lv_1 TYPE structured_type-comp1,
      lv_2 LIKE structure-comp1.
  • ADT and the ABAP Editor provide code completion for structure components after the component selector.
  • When declaring a variable with reference to a structured type, the object component selector -> must be used: ... dref->comp ....

Filling Structures

Filling structure components using the component selector.

address-name     = `Mr. Duncan Pea`.
address-street   = `Vegetable Lane 11`.
address-city     = `349875 Botanica`.

Filling structure components using the VALUE operator. Value assignments by addressing the structure components individually can be very bulky. Hence, the use of the VALUE operator is very handy for the value assignment, especially for filling structure components at operand position. In the example below, the # sign is used before the parentheses which means that the type of the operand can be implicitly derived. This is not the case for the example further down in which a new structure is declared inline. Hence, the type must be explicitly specified before the parentheses.

address = VALUE #( name   = `Mr. Duncan Pea`
                   street = `Vegetable Lane 11`.
                   city   = `349875 Botanica` ).

Creating a new structure and filling the components with the VALUE operator. The new structure is declared inline.

TYPES address_type LIKE address.

DATA(add2) = VALUE address_type( name   = `Mrs. Jane Pea`
                                 street = `Vegetable Lane 11`.
                                 city   = `349875 Botanica` ).

Copy the content of a structure to another one that has the same type. You might also stumble upon MOVE ... TO ... statements that have the same effect, however, do not use the statement anymore since it is obsolete syntax.

For value assignments, generally bear in mind that there are special conversion and comparison rules that apply to assignments involving structures.

address = add2.
"MOVE add2 TO address.

Copy content of a structure to another one that has a different type. You can use statements with MOVE-CORRESPONDING and the CORRESPONDING operator. Both are used to assign identically named components of structures to each other. This means that the example above dealing with structures having the same type also works with the syntax below.

Note the rules mentioned above and consider the result of the value assignment when using either of the two options with MOVE-CORRESPONDING and the CORRESPONDING operator. See more information and syntax variants in the ABAP Keyword Documentation: MOVE-CORRESPONDING for structures and CORRESPONDING. In the examples below, the focus is only on flat structures.

"Moves identically named components; content in other components
"of the targets structure are kept.
MOVE-CORRESPONDING struc TO diff_struc.

"Initializes target structure; moves identically named components
diff_struc = CORRESPONDING #( struc ).

"Same effect as the first MOVE-CORRESPONDING statement;
"addition BASE keeps existing content
diff_struc = CORRESPONDING #( BASE ( diff_struc ) struc ).

Copy content of a deep structure to another deep structure that has a different type. Here too, you can use statements with MOVE-CORRESPONDING and the CORRESPONDING operator. However, in the context of deep structures, more syntax variants are available. The focus of the following examples is on internal tables as structure components. See the demonstration example for more details on all variants and that also considers the assignment rules and nested structures.

Bear in mind that there are special conversion and comparison rules that apply to assignments involving structures and that affect the value assignments in the internal table components.

"Nonidentical elementary component types are kept in target
"structure which is true for the below MOVE-CORRESPONDING statements;
"existing internal table content is replaced by content of
"the source table irrespective of identically named components

MOVE-CORRESPONDING deep_struc TO diff_deep_struc.

"Existing internal table content is replaced but the value
"assignment happens for identically named components only.

MOVE-CORRESPONDING deep_struc TO diff_deep_struc
    EXPANDING NESTED TABLES.

"Existing internal table content is kept; table content of the source
"structure are added but the value assignment happens like the first
"MOVE-CORRESPONDING statement without further syntax additions.

MOVE-CORRESPONDING deep_struc TO diff_deep_struc
    KEEPING TARGET LINES.

"Existing internal table content is kept; table content of the source
"structure are added; the value assignment happens like the statement.
"MOVE-CORRESPONDING ... EXPANDING NESTED TABLES.

MOVE-CORRESPONDING deep_struc TO diff_deep_struc
    EXPANDING NESTED TABLES KEEPING TARGET LINES.

"Target structure is initialized; the value assignment for an internal
"table happens irrespective of identically named components.

diff_deep_struc = CORRESPONDING #( deep_struc ).

"Target structure is initialized; the value assignment for an internal
"table happens for identically named components only.

diff_deep_struc = CORRESPONDING #( DEEP deep_struc ).

"Nonidentical elementary component types are kept in target structure;
"internal table content is replaced; there, the value assignment
"happens like using the CORRESPONDING operator without addition.

diff_deep_struc = CORRESPONDING #( BASE ( diff_struc ) deep_struc ).

"Nonidentical elementary component types are kept in target structure;
"internal table content is replaced; there, the value assignment
"happens like using the CORRESPONDING operator with the addition DEEP.

diff_deep_struc = CORRESPONDING #( DEEP BASE ( diff_struc )
                                   deep_struc ).

"Nonidentical elementary component types are kept in target structure;
"internal table content is kept, too, and table content of the
"source structure are added; there, the value assignment
"happens like using the CORRESPONDING operator without addition.

diff_deep_struc = CORRESPONDING #( APPENDING BASE ( diff_struc )
                                   deep_struc ).

"Nonidentical elementary component types are kept in target structure;
"internal table content is kept, too, and table content of the
"source structure are added; there, the value assignment
"happens like using the CORRESPONDING operator with the addition DEEP.

diff_deep_struc = CORRESPONDING #( DEEP APPENDING BASE ( diff_struc )
                                   deep_struc ).

Clearing Structures

You can reset individual components to their initial value and clear the complete structure using the keyword CLEAR.

CLEAR structure-component.
CLEAR structure.

Structures in Use in the Context of Tables

Structures are primarily used to process data from tables. In this context, structures often assume the role of a work area.

Read line from a database table into a structure that has a matching type. In the example below, the matching type is realized by a structure that is declared inline. Apart from that, since database tables are flat, the target structure must also be flat. In below example, the addition SINGLE reads only a single row into the structure. The first entry that is found according to the WHERE condition is returned.

See more details on SELECT statements in the ABAP Keyword Documentation. The ABAP cheat sheet ABAP SQL: Working with Persisted Data in Database Tables also provides information and more code snippets when using SELECT statements.

SELECT SINGLE FROM spfli
    FIELDS *
    WHERE carrid = 'LH'
    INTO @DATA(ls_spfli).

Read a line from a database table into a structure that has a different type.

SELECT SINGLE FROM spfli
    FIELDS *
    WHERE carrid = 'AA'
    INTO CORRESPONDING FIELDS OF @ls_spfli_diff.

Read a line from an internal table into a structure ...

... using a SELECT statement. Note the specified alias name and that ABAP variables like internal tables must be escaped using @. The addition INTO CORRESPONDING FIELDS OF also applies here.

SELECT SINGLE FROM @itab AS itab_alias
    FIELDS *
    WHERE ...
    INTO @DATA(ls_struc).
    "INTO CORRESPONDING FIELDS OF @struc.

... using a READ TABLE statement. The code snippet below shows the reading of one line into a work area and a field symbol, both representing structured data objects. Here, the reading of a line is based on the line number by specifying INDEX.

READ TABLE itab INTO DATA(wa) INDEX 2.
READ TABLE itab ASSIGNING FIELD-SYMBOL(<fs>) INDEX 3.

... using a table expression. The code snippet below shows the reading of one line into a structure that is declared inline. The index is specified in square brackets.

DATA(ls_table_exp) = itab[ 3 ].

See more syntax variants and code snippets for READ TABLE statements and statements with table expressions in the ABAP cheat sheet Working with Internal Tables.

Sequentially read a line from ...

... a database table into a structure. A SELECT loop can be specified by using the syntax SELECT ... ENDSELECT.. In the simple example below, the line that is found and returned in a structure, which is declared inline, can be further processed.

A SELECT loop might also have an internal table as data source.

SELECT FROM spfli
    FIELDS *
    WHERE carrid = 'AZ'
    INTO @DATA(ls_sel_loop).
      IF sy-subrc = 0.
        ...
      ENDIF.
    ENDSELECT.

... an internal table into a structure using a LOOP AT statement. There are numerous options for specifying the condition on which the loop is based. See the ABAP Keyword Documentation and the cheat sheet on internal tables for more information and code snippets. The example below covers two options of reading lines sequentially into a field symbol and a work area.

LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs_loop>) FROM 1 TO 5.
      (<fs_loop>)-comp1 = ...
      ...
    ENDLOOP.

LOOP AT itab INTO DATA(wa) FROM 1 TO 5.
      wa-comp1 = 'XY'.
    ENDLOOP.

Inserting an individual row from a structure into a database table using statements with INSERT. The statements below can be considered as alternatives. The third statement demonstrates that the structure might also be created and filled directly instead of inserting a line from an existing structure. Note that a line with a certain key should not be inserted into the database table if a line with the same key already exists there.

INSERT INTO dbtab VALUES @structure.

INSERT dbtab FROM @structure.

INSERT dbtab FROM @( VALUE #( comp1 = ... comp2 = ... ) ).

Updating an individual row from a structure in a database table using statements with UPDATE. Note that with this syntax, the whole line and all components are changed.

UPDATE dbtab FROM @structure.

UPDATE dbtab FROM @( VALUE #( comp1 = ... comp2 = ... ) ).

If you want to update a database table row from a structure by specifying components to be changed without overwriting other components, you might use choose the following way. First, read the intended line from the database table into a structure. Then, use the VALUE operator with the addition BASE and specify the components to be changed.

SELECT SINGLE * FROM dbtab WHERE ... INTO @DATA(wa).
UPDATE dbtab FROM @( VALUE #( BASE wa comp2 = ... comp4 = ... ) ).

Updating or creating an individual row in a database table from a structure using statements with MODIFY. If a line in the database table already exists with the same keys as specified in the structure, the line is updated. If a line does not exist with the keys specified in the structure, a new line is created in the database table.

MODIFY dbtab FROM @structure.

MODIFY dbtab FROM @( VALUE #( comp1 = ... comp2 = ... ) ).

Adding rows to and updating individual rows in an internal table from a structure using statements with INSERT, APPEND, and MODIFY. Note that all statements, including INSERT and MODIFY, are ABAP statements in this context, not statements.

Both INSERT and APPEND add one line (or more) to an internal table. While APPEND adds at the bottom of the internal table, INSERT can be used to add lines at a specific position in tables. If you go without specifying the position, then the lines are added at the bottom of the table, too. However, when using INSERT, sy-tabix is not set as compared to APPEND.

MODIFY changes the content of an internal table entry.

Statements using the VALUE operator for directly creating and filling the structures are possible, too.

INSERT structure INTO TABLE itab.

APPEND structure TO itab.

MODIFY TABLE itab FROM structure.

Excursion: Including Structures

Although their use is not recommended according to the ABAP programming guidelines, you might stumble upon statements with INCLUDE TYPE and INCLUDE STRUCTURE in the context of local structures. Structured data objects and types that are created with ... BEGIN OF... END OF ... can include the said syntax to integrate components of another structure, no matter if it is a locally defined or global structure, without the need to create substructures. INCLUDE TYPE can be used to include a structured type. INCLUDE STRUCTURE can be used to include a structure.

  • They are not additions to ... BEGIN OF... END OF ... but individual ABAP statements.
  • If a chained statement is used for the structure declaration with the colon, the inclusion of other structures with these statements interrupts the chained statement, that is, the components of the included structures are included as direct components of the superstructure.
  • Using the optional addition AS and the specification of a name, the included components can be addressed by this common name as if the components were actually components of a substructure.
  • Using the optional addition RENAMING WITH SUFFIX and the specification of a name, the included components are given a suffix name to avoid naming conflicts with other components.

The example below shows how structured types and data objects are included in another structure. First, three structured types as well as a structured data object based on one of those types are created. Then, the types and the structure are included in the structured type address_type. The demonstration program visualizes a structure that includes other structures this way.

TYPES:
      BEGIN OF name_type,
        title   TYPE string,
        prename TYPE string,
        surname TYPE string,
      END OF name_type,
      BEGIN OF street_type,
        name   TYPE string,
        number TYPE string,
      END OF street_type,
      BEGIN OF city_type,
        zipcode TYPE string,
        name    TYPE string,
      END OF city_type.

DATA city_struc TYPE city_type.

TYPES BEGIN OF address_type.
      INCLUDE TYPE name_type AS name.
      INCLUDE TYPE street_type AS street RENAMING WITH SUFFIX _street.
      INCLUDE STRUCTURE city_struc AS city RENAMING WITH SUFFIX _city.
TYPES END OF address_type.

Demonstration Program

The example Working with Structures demonstrates the syntactical options mentioned above and beyond in one program.






CL_GUI_FRONTEND_SERVICES - Frontend Services   rdisp/max_wprun_time - Maximum work process run time  
This documentation is copyright by SAP AG.

Length: 35703 Date: 20240509 Time: 195942     sap01-206 ( 338 ms )