We always get confused about the type of internal tables. So ABAP knows the three table types
STANDARD,SORTED, andHASHEDtable. Each of these table types differs in the way they are declared, accessed, and changed during runtime.
Lets understand this with the help of an example
For the following declaration we use the local type ty_sales_order_item which has fields from sales order table and it’s decleration will look something like this
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
now if we have to declare an standard table it will look like this
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
Hashed as well as sorted both has unique and non unique keys ( unique means only one key will be there, non-unique multiple entries for the key
key must be specified, Example see how data decleartion needs to be done
DATA : it_mat type sorted table of ty_mat with unique key matnr
DATA : it_mat type sorted table of ty_mat with non-unique key matnr
example:-
Here duplicate entries are not inserted
*&---------------------------------------------------------------------*
*& Report ZSOTED_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZSOTED_TABLE.
TYPES : begin of ty_tab,
am TYPE i,
cou TYPE i,
END OF ty_tab.
DATA : it_tab TYPE SORTED TABLE OF ty_tab WITH UNIQUE KEY cou,
wa_tab TYPE ty_tab.
do 3 TIMES.
wa_tab-am = sy-index.
wa_tab-cou = sy-index + 1.
INSERT wa_tab INTO TABLE it_tab.
ENDDO.
wa_tab-am = sy-index.
wa_tab-cou = '3'.
INSERT wa_tab INTO TABLE it_tab.
loop at it_tab INTO wa_tab.
WRITE : / wa_tab-am,
wa_tab-cou.
ENDLOOP.
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