We always get confused about the type of internal tables. So ABAP knows the three table types STANDARD, SORTED, and HASHED table. Each of these table types differs in the way they are declared, accessed, and changed during runtime.
For the following declaration examples we use the local type ty_sales_order_item:
TYPES: BEGIN OF ty_sales_order_item,
vbeln TYPE vbeln,
posnr TYPE posnr,
matnr TYPE matnr,
deleted TYPE boolean,
END OF ty_sales_order_item.
Standard Table
Accessed by referring to the table index (which is the quickest access method). Access time for standard table increases linearly with respect to the number of table entries. New rows are appended to the table. Individual rows are accessed by reading the table at a specified index value.
- Unsorted but can be sorted with
SORT - Access via table index and table key
- Default key because the table is a standard table
DATA: sales_order_items TYPE STANDARD TABLE OF ty_sales_order_item WITH DEFAULT KEY.
Sorted Table
Most appropriate where the table is to be filled in sorted order. Sorted tables are filled using the INSERT statement. Inserted entries are sorted according to a sort sequence defined by the table key. Illegal entries are recognized as soon as you try to insert them into the table. Response time is logarithmically proportional to the number of table entries (since the system always uses a binary search). Sorted tables are particularly useful if you wish to process row by row using a LOOP.
- Sorted according to the defined table key
- Access via table index and table key
- Fast key access thanks to binary search
- Primary Key: table_line
DATA: sales_order_items TYPE SORTED TABLE OF string WITH UNIQUE KEY table_line.
Hashed Table
Most appropriate when access to rows is by a key. (Cannot access hashed tables via the table index)response time remains constant regardless of the number of rows in the table.
- Access only via a unique key, no index
- Each key value may only appear once, otherwise an exception is raised
- Fast if all key fields are included
- Primary Key: vbeln
DATA: sales_order_items TYPE HASHED TABLE OF ty_sales_order_item WITH UNIQUE KEY vbeln.
Defining keys for an internal table can play a significant role i.e. it determines how you entries will be processes and in which way, below is the recommendation from SAP
https://github.com/SAP/code-pal-for-abap/blob/master/docs/checks/avoid-default-key.md
Generic Key Operations
| KEY | Single record processing | Multiple record processing |
|---|---|---|
| Read | READ TABLE i tab FROM viaREAD TABLE flab . WITH KEY ky = v1 … kn = v„. | LOOP AT flab WHERE cond. |
| Insert | INSERT wa INTO TABLE itab. | INSERT LINES OF flab.; INTO TABLE itab2. |
| Change | MODIFY TABLE itab FROM wa. | MODIFY itab .. TRANSPORTING , WHERE cond. |
| Delete | DELETE TABLE itab FROM wa.DELETE TABLE -tah WITH KEY /.-/ = v1 kn = vn. | DELETE itab WHERE cond. |
Data Transfer & Internal Tables
Filling line by line
- Append, Collect, Insert
Copying the contents of another table
- Append, insert, move
Reading line by line
- Loop, read, search
Determining the attributes of an internal table
- Describe