Entering content frameInternal table objects Locate the document in its SAP Library structure

Internal tables are dynamic variable data objects. Like all variables, you declare them using the DATA statement. You can also declare static internal tables in procedures using the STATICS statement, and static internal tables in classes using the CLASS-DATA statement. This description is restricted to the DATA statement. However, it applies equally to the STATICS and CLASS-DATA statements.

Reference to Declared Internal Table Types

Like all other data objects, you can declare internal table objects using the LIKE or TYPE addition of the DATA statement.

DATA <itab> TYPE <type>|LIKE <obj> [WITH HEADER LINE].

Here, the LIKE addition refers to an existing table object in the same program. The TYPE addition can refer to an internal type in the program declared using the TYPES statement, or a table type in the ABAP Dictionary.

You must ensure that you only refer to tables that are fully typed. Referring to generic table types (ANY TABLE, INDEX TABLE) or not specifying the key fully is not allowed (for exceptions, refer to Special Features of Standard Tables).

The optional addition WITH HEADER line declares an extra data object with the same name and line type as the internal table. This data object is known as the header line of the internal table. You use it as a work area when working with the internal table (see Using the Header Line as a Work Area). When you use internal tables with header lines, you must remember that the header line and the body of the table have the same name. If you have an internal table with header line and you want to address the body of the table, you must indicate this by placing brackets after the table name (<itab>[]). Otherwise, ABAP interprets the name as the name of the header line and not of the body of the table. You can avoid this potential confusion by using internal tables without header lines. In particular, internal tables nested in structures or other internal tables must not have a header line, since this can lead to ambiguous expressions.

 

Example

TYPES VECTOR TYPE SORTED TABLE OF I WITH UNIQUE KEY TABLE LINE.

DATA: ITAB TYPE VECTOR,
      JTAB LIKE ITAB WITH HEADER LINE.

* MOVE ITAB TO JTAB.   <-  Syntax error!

MOVE ITAB TO JTAB[].

The table object ITAB is created with reference to the table type VECTOR. The table object JTAB has the same data type as ITAB. JTAB also has a header line. In the first MOVE statement, JTAB addresses the header line. Since this has the data type I, and the table type of ITAB cannot be converted into an elementary type, the MOVE statement causes a syntax error. The second MOVE statement is correct, since both operands are table objects.

Declaring New Internal Tables

You can use the DATA statement to construct new internal tables as well as using the LIKE or TYPE addition to refer to existing types or objects. The table type that you construct does not exist in its own right; instead, it is only an attribute of the table object. You can refer to it using the LIKE addition, but not using TYPE. The syntax for constructing a table object in the DATA statement is similar to that for defining a table type in the TYPES statement.

DATA <itab> TYPE|LIKE <tabkind> OF <linetype> WITH <key>
            [INITIAL SIZE <n>]
            [WITH HEADER LINE].

As when you define a table type , the type constructor

<tabkind> OF <linetype> WITH <key>

defines the table type <tabkind>, the line type <linekind>, and the key <key> of the internal table <itab>. Since the technical attributes of data objects are always fully specified, the table must be fully specified in the DATA statement. You cannot create generic table types (ANY TABLE, INDEX TABLE), only fully-typed tables (STANDARD TABLE, SORTED TABLE, HASHED TABLE). You must also specify the key and whether it is to be unique (for exceptions, refer to Special Features of Standard Tables).

As in the TYPES statement, you can, if you wish, allocate an initial amount of memory to the internal table using the INITIAL SIZE addition. You can create an internal table with a header line using the WITH HEADER LINE addition. The header line is created under the same conditions as apply when you refer to an existing table type.

Example

DATA ITAB TYPE HASHED TABLE OF SPFLI
WITH UNIQUE KEY CARRID CONNID.

The table object ITAB has the type hashed table, a line type corresponding to the flat structure SPFLI from the ABAP Dictionary, and a unique key with the key fields CARRID and CONNID. The internal table ITAB can be regarded as an internal template for the database table SPFLI. It is therefore particularly suitable for working with data from this database table as long as you only access it using the key.

 

 

 

Leaving content frame