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 STATICSstatement, and static internal tables in classes using the CLASS-DATAstatement.
This description is restricted to the DATAstatement. However, it applies equally to the STATICS and CLASS-DATA statements.
Like all other data objects, you can declare internal tables 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 WITH HEADER LINE addition is obsolete; you should no longer use it. Also see the keyword documentation.
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.
TYPES vector TYPE SORTED TABLE OF i WITH UNIQUE KEY table_line.
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.
You can use the DATA statement to construct new internal tables as well as using the LIKE or TYPEaddition 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 TYPESstatement.
DATA itab TYPE|LIKE tabkind OF
linetype WITH key
[INITIAL SIZE n]
[WITH HEADER LINE].
As illustrated when you define a table type yourself, the type constructor
tabkind OF linetype WITH key
defines the table type tabkind, the line type linetype, 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 DATAstatement. You cannot create generic table types (ANY TABLE, INDEX TABLE), only fully-typed tables (STANDARD TABLE and 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 SIZEaddition. 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.
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.