
Internal tables are dynamic data objects containing any number of rows with a common row type. Internal tables are particularly well suited for editing mass data in business applications. As well as the row type, the data type of an internal table is also defined by its table category and a table key.
Row Type
The row type can be a non-generic data type from ABAP Dictionary or global class or interface, a non-generic type in the local program, or a predefined ABAP type. Any types can be used as row types, such as elementary types, structured types, table types, and reference types.
Table Category
The table category defines how individual rows can be accessed:
Standard tables are managed using a row index. New rows can either be appended to end of the table or inserted at specific positions. Individual rows are accessed using the table key or the row index.
Sorted tables are also managed using a row index, but are sorted by the table key in ascending order. New rows can be inserted or appended only if they are sorted. The rows are accessed in the same way as in standard tables.
Hashed tables are managed using a hash algorithm. The rows are accessed using the table key only. Index operations are not possible.
As well as the table categories STANDARD TABLE, SORTED TABLE, and HASHED TABLE described above, two other generic table categories exist that can be used when typing formal parameters and field symbols. The generic table category ANY TABLE covers all table categories; the table category INDEX TABLE only covers those where index operations are possible (namely standard tables and sorted tables).
Table Key
Internal tables have a primary table key and can also have secondary table keys. If the row types are structured, the table key can consist of a list of structure components. Alternatively, the pseudo component table_line can be used to specify the entire table row as a key. Another option is to specify the standard key DEFAULT KEY. This key contains all components that are not numeric and are not themselves table types. The primary table key of a standard table can also be empty. Table keys in standard keys are always non-unique, which means multiple rows in the same table can have the same key. Hashed tables, on the other hand, always have a unique key. In sorted tables, this property can be defined using the additions UNIQUE and NON-UNIQUE.
Editing Internal Tables
Internal tables can be edited, for example, using the statements READ TABLE, LOOP AT, INSERT INTO TABLE, MODIFY TABLE, and DELETE TABLE.
Example
Declares an internal table itab with a structured row type. Fills the table by inserting rows, reads the rows sequentially in a loop, and assigns the fifth row to a field symbol declared inline.
DATA: BEGIN OF struc,
col1 TYPE i,
col2 TYPE i,
END OF struc,
itab LIKE STANDARD TABLE OF struc.
DO 10 TIMES.
struc-col1 = sy-index.
struc-col2 = sy-index ** 2.
INSERT struc INTO TABLE itab.
ENDDO.
LOOP AT itab INTO struc.
WRITE: / struc-col1, struc-col2.
ENDLOOP.
READ TABLE itab INDEX 5 ASSIGNING FIELD-SYMBOL(<line>).
WRITE: / <line>-col1, <line>-col2.