Entering content frameExamples of Data Types and Objects Locate the document in its SAP Library structure

This section contains examples of elementary and aggregated data types and objects as often used in ABAP programs.

Example

This example shows how to declare elementary data objects with reference to predefined ABAP types.

PROGRAM demo_elementary_data_objects.

DATA text1(20) TYPE c.
DATA text2     TYPE string.
DATA number    TYPE i.

text1 = 'The number'.
number = 100.
text2 = 'is an integer.'.

WRITE: text1, number, text2.

This program produces the following output on the screen:

The number              100 is an integer.

In this example, the data objects TEXT1, TEXT2, and NUMBER are declared with the DATA statement. The technical attributes are determined by referring to the predefined ABAP types C, STRING, and I. Values from unnamed literals are assigned to the data objects. The contents of the named data objects are displayed on the list.

Example

This example shows how to declare local elementary data types within a program.

REPORT demo_types_statement.

TYPES mytext(10) TYPE c.
TYPES myamount   TYPE p DECIMALS 2.

DATA text        TYPE mytext.
DATA amount      TYPE myamount.

text = ' 4 / 3 = '.
amount = 4 / 3 .

WRITE: text, amount.

This program produces the following output on the screen:

4 / 3 =                1.33

The program uses the TYPES statement to create the local data types MYTEXT and MYAMOUNT. The technical attributes are defined with reference to predefined ABAP types. Then, the data objects TEXT and AMOUNT are declared with the DATA statement. Their data types are defined with reference to MYTEXT and MYAMOUNT. Values are assigned to the data objects and the contents of the data objects are displayed on the screen.

Example

This example shows how to declare structures.

REPORT demo_structure.

TYPES: BEGIN OF name,
         title(5)       TYPE c,
         first_name(10) TYPE c,
         last_name(10)  TYPE c,
       END OF name.

TYPES: BEGIN OF mylist,
         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 MYLIST are defined as structures using the TYPES statement. The structure NAME contains the three components TITLE, FIRST_NAME and LAST_NAME, which refer to the predefined ABAP type C. The structure MYLIST contains the components CLIENT and NUMBER. CLIENT is a substructure that refers to the structure NAME. NUMBER is an elementary field with the predefined type I. A data object LIST is defined with reference to the data type MYLIST. Values are assigned to the components and their contents are then displayed on the screen.

Example

This example shows how to define an internal table.

PROGRAM demo_internal_table.

TYPES: BEGIN OF mytext,
         number TYPE i,
         name(10) TYPE c,
       END OF mytext.

TYPES mytab TYPE STANDARD TABLE OF mytext WITH DEFAULT KEY.

DATA text TYPE mytext.
DATA itab TYPE mytab.

text-number = 1. text-name = 'John'.
APPEND text TO itab.

text-number = 2. text-name = 'Paul'.
APPEND text TO itab.

text-number = 3. text-name = 'Ringo'.
APPEND text TO itab.

text-number = 4. text-name = 'George'.
APPEND text TO itab.

LOOP AT itab INTO text.
  WRITE: / text-number,text-name.
ENDLOOP.

This program produces the following output on the screen:

1  John

2  Paul

3  Ringo

4  George

In this example, first a data type MYTEX is defined as a structure. Then, a data type MYTAB is defined as an internal table with the line type MYTEXT. The data objects TEXT and ITAB are declared with reference to the internal data types MYTEXT and MYTAB. This lines of the internal table ITAB are generated dynamically with the APPEND statement. The contents of the internal table ITAB are written to the list using the structure TEXT. See also: Internal Tables

 

 

Leaving content frame