DATA - Defining an Internal Table

Variants:

Variants:

1. DATA itab TYPE itabtype [WITH HEADER LINE].

2. DATA itab  {TYPE tabkind OF linetype| LIKE
   tabkind OF lineobj}

  WITH [UNIQUE|NON-UNIQUE] keydef
  [INITIAL SIZE n] [WITH HEADER LINE].

3. DATA itab {TYPE TABLE OF linetype|LIKE TABLE OF lineobj}.

4. DATA itab {TYPE RANGE OF type|DATA itab LIKE RANGE OF f}.

5. DATA itab [TYPE linetype|LIKE lineobj] OCCURS n [WITH HEADER LINE].

6. DATA: BEGIN OF itab OCCURS n,           ...
        END   OF itab [VALID BETWEEN f1 AND f2].

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas. See New Naming Conventions und Cannot Use LIKE Reference to Dictionary Types.

Effect

Defines an internal table.

You fill and edit internal tables using the following statements: INSERT, APPEND, READ TABLE, LOOP, SORT.

The OCCURS and INITIAL SIZE additions (hereafter called the OCCURS value), determine the initial number of table lines created. The table is enlarged by the system when necessary. For more details about this, see performance notes for internal tables. The OCCURS value has no semantic meaning apart from an exception with APPEND SORTED BY. If you do not specify the INITIAL SIZE, the system sets the OCCURS value to 0 by default.

If you specify WITH HEADER LINE, the table is created with a header line - a field with the same name as the table. Its type is the same as the line type of the internal table.

This addition is not allowed in an ABAP Objects context. See Cannot Use Tables with a Header.

Variant 1

DATA itab TYPE itabtype [WITH HEADER LINE].


Effect

itabtype must be an internal table type, defined using TYPES. The system creates an internal table with the specified type.

The type declaration for the table object must be complete. The only exception to this rule is that, if you declare a generic type for a standard table (where only the key definition can be missing), the system completes it using the standard key.

Example

Creating a hashed table by referring to an existing table type.

TYPES: BEGIN OF STRUC, NAME(10), AGE TYPE I, END OF STRUC,
       HTAB TYPE HASHED TABLE OF STRUC WITH UNIQUE KEY NAME.
DATA : PERSONS TYPE HTAB.

Variant 2

DATA itab {TYPE tabkind OF linetype|LIKE tabkind OF lineobj}
          WITH [UNIQUE|NON-UNIQUE] keydef

          [INITIAL SIZE n] [WITH HEADER LINE].


Effect

The system creates an internal table with table type tabkind. Since there is no generic field definition, you cannot use the table types ANY TABLE or SORTED TABLE.

The construction of the table lines is defined by linetype (if you are using a TYPE reference) or by the type of the referred object lineobj (if you are using a LIKE reference). If you specify the line type, you can also use REF TO to refer to a reference type.

The same rules apply to UNIQUE and NON-UNIQUE as apply to the TYPES definition. You may only omit this specification with standard tables.

If you do not specify an INITIAL SIZE, the system assumes a default value of INITIAL SIZE 0.

Variant 3

DATA itab {TYPE TABLE OF linetype|LIKE TABLE OF lineobj}.


Effect

This variant is a shortened form of the internal table definition. It corresponds to

   DATA itab {TYPE STANDARD TABLE OF linetype|
              LIKE STANDARD TABLE OF lineobj} WITH DEFAULT KEY.


or the old definition (see variant 4) with

   DATA itab {TYPE linetype|LIKE lineobj} OCCURS 0.


Variant 4

DATA itab {TYPE RANGE OF type|DATA itab LIKE RANGE OF f}.

Extras:

1. ... INITIAL SIZE n

2. ... WITH HEADER LINE


Effect

Defines an internal table itab of the table type STANDARD. The line type is a structure made up of the following:

  SIGN(1)   TYPE C
  OPTION(2) TYPE C
  LOW       TYPE type bzw. LIKE f
  HIGH      TYPE type bzw. LIKE f

Thus, the structure of the table corresponds to a RANGES table and can be used in the same way. Unlike tables that were created using the RANGES statement, a table defined using TYPE|LIKE RANGE OF has no header. You can declare an appropriate work area using the TYPE|LIKE LINE OF itab addition of the TYPES statement.


Addition 1

...INITIAL SIZE n

Effect

INITIAL SIZE specifies how many table lines are to be created initially. The number of table lines can then be increased as needed. For more information, see Performance: Notes for Internal Tables. The value of INITIAL SIZE has no semantic significance except in the case of APPEND SORTED BY. If INITIAL SIZE is not declared, its value is set to 0.

Addition 2

... WITH HEADER LINE

This addition is not allowed in an ABAP Objects context.

See Cannot Use Tables with a Header Line.



Effect
WITH HEADER LINE creates a header line with the table - that is, a field with the same name, whose type corresponds to the line type of the table.

Variant 5

DATA itab [TYPE linetype|LIKE lineobj] OCCURS n
                                       [WITH HEADER LINE].



This variant is not allowed in an ABAP Objects context.

See Cannot Use OCCURS with Declarative Statements.



Effect
This variant exists for reasons of compatibility with Release 3.x. If this declaration is missing, the type C with a length of 1 is used. Otherwise the variant is synonymous with

   DATA itab {TYPE STANDARD TABLE OF linetype|
              LIKE STANDARD TABLE OF lineobj}
             INITIAL SIZE n [WITH HEADER LINE].

Example

TYPES: BEGIN OF LINE_TYPE,
         NAME(20) TYPE C,
         AGE      TYPE I,
       END   OF LINE_TYPE.
DATA:  PERSONS    TYPE LINE_TYPE OCCURS 20,
       PERSONS_WA TYPE LINE_TYPE.

PERSONS_WA-NAME = 'Michael'.  PERSONS_WA-AGE  = 25.
APPEND PERSONS_WA TO PERSONS.
PERSONS_WA-NAME = 'Gabriela'. PERSONS_WA-AGE  = 22.
APPEND PERSONS_WA TO PERSONS.

The internal table PERSONS now contains two entries:

Variant 6

DATA: BEGIN OF itab OCCURS n,
        ...

      END   OF itab [VALID BETWEEN f1 AND f2].


This variant is not allowed in an ABAP Objects context.

See Cannot Use OCCURS with Declarative Statements.

Effect

Defines an internal table itab with table type STANDARD and a header line. The line type consists of the fields between "BEGIN OF itab OCCURS n" and "END OF itab".

You can use the addition VALID BETWEEN f1 AND f2 to specify that subfields f1 and f2 of the internal table itab contain the validity range. This addition is only relevant in conjunction with the PROVIDE statement.

Example

DATA: BEGIN OF PERSONS OCCURS 20,
        NAME(20),
        AGE TYPE I,
      END   OF PERSONS.
PERSONS-NAME = 'Michael'.
PERSONS-AGE  = 25.
APPEND PERSONS.
PERSONS-NAME = 'Gabriela'.
PERSONS-AGE  = 22.
APPEND PERSONS.

The internal table now has two entries. It also has a header line (work area), which you use to work with the table itself.

Additional help

Internal Table Objects