Entering content frame

Structured Data Types Locate the document in its SAP Library structure

To construct a new structured data type struc_typein a program, you use several TYPES statements:

TYPES BEGIN OF struc_type.
  {TYPES dtype ...} | {INCLUDE {TYPE|STRUCTURE} ...}.
TYPES END OF struc_type.

The two TYPES statements with the additions BEGIN OF und END OFmay enclose as many TYPESstatements as required, including additional complete structure definitions, as well as the statements INCLUDE TYPE and INCLUDE STRUCTURE. This way you can create nested structures.

The individual components of a structured data type are addressed in the program with the structure component selector (-) between the structure name and component name as follows: struc_type-dtype.

For more information, refer to the keyword documentation.


TYPES: spfli_type TYPE spfli,

       surname(20) TYPE c,

       BEGIN OF address,
             name       TYPE surname,
             street(30) TYPE c,
             city       TYPE spfli_type-cityfrom,
       END OF address,

       town TYPE address-city.

This example shows the definition of two structure types in a program - spfli_type and address. The structure of the data type spfli_type is taken from the database table SPFLI in the ABAP Dictionary. The components of spfli_type are the same as the columns of SPFLI. The individual data types of the components are the ABAP equivalents of the data types of the columns of the database table. The structure type address is newly defined. The component address-name takes the data type of the previously defined type surname, the component address-street is newly defined, address-city takes the data type of column cityfrom of the structure type spfli_type.


TYPES: BEGIN OF struct1,
         col1 TYPE i,
         BEGIN OF struct2,
           col1 TYPE i,
           col2 TYPE i,
         END OF struct2,
       END OF struct1.

TYPES mytype TYPE struct1-struct2-col2.

The example shows how you can construct a nested structure type struct1 with a complex component struct2 by nesting TYPES BEGIN OF ... TYPES END OF blocks, and how you can address the inner components.


* Local types in program
* referring to predefined ABAP types:

TYPES: surname(20)  TYPE c,
       street(30)   TYPE c,
       zip_code(10) TYPE n,
       city(30)     TYPE c,
       phone(20)    TYPE n,
       date         LIKE sy-datum.

* Local structure in program
* referring to the above types

TYPES: BEGIN of address,
         name TYPE surname,
         code TYPE zip_code,
         town TYPE city,
         str  TYPE street,
       END OF address.

* Local nested structure in program
* referring to the above types

TYPES: BEGIN of phone_list,
         adr TYPE address,
         tel TYPE phone,
       END OF phone_list.

This example shows how to create complex data types from simple type definitions. After a set of simple data types are created with ABAP predefined types, a structured type address is defined using the data types defined earlier. Finally, a nested structure type, phone_list , is created, whose first component has the type address.


REPORT demo_structure.

         title      TYPE c
         first_name TYPE c
         last_name  TYPE c
       END OF name.

         client         TYPE name,
         number         TYPE i,
       END OF mylist.

DATA list TYPE mylist.

list-client-title = 'Lord'.
list-client-first_name = 'Howard'.
list-client-last_name = 'Mac Duff'.
list-number = 1.

WRITE list-client-title.
WRITE list-client-first_name.
WRITE list-client-last_name.
WRITE / 'Number'.
WRITE list-number.

This program produces the following output on the screen:

Lord  Howard      Mac Duff
Number           1

The local data types name and mylistare defined as structures using the TYPESstatement. The structure namecontains the three components title, first_name and last_name with reference to the built-in ABAP type c. The structure mylist contains the two components client and number. client is a substructure as it refers to the structure name. number is elementary with the built-in type i. A data object list is declared with reference to the data type mylist. Values are assigned to the components and their contents are then displayed on the screen.


The syntax for directly declaring a variable as a structure is the same as you would use to define a structured data type using the TYPES statement:

  {DATA comp ...} | {INCLUDE {TYPE|STRUCTURE} ...}.
DATA END OF struc.

The individual components of a structure are addressed in the program with the structure component selector (-) between the structure name and component name as follows: struc_comp.

For more information, refer to the keyword documentation.


DATA: BEGIN OF address,
         name     TYPE c LENGTH 20,
         street   TYPE c LENGTH 20,
         number   TYPE p,
         postcode TYPE n LENGTH 5,
         city     TYPE c LENGTH 20,
      END OF address.

This example defines a structure called address. The components can be addressed using address-name, address-street, and so on.



Leaving content frame