Types of Internal tables in SAP:
In SAP, we have 3 most commonly used internal tables. They are Standard internal tables, Sorted and Hashed internal tables. Let’s explore these internal tables creation.
Prerequisites:
- You should be having good knowledge on what is internal table in SAP ABAP ?
Let’s understand different types of internal tables now.
Standard internal table :
- Usage: Standard table of / table of.
- No specific key; Allows duplicate entries.
- linear access- linear search algorithm.
WITH UNIQUE KEY –not allowed since standard tables always non unique rows.
WITH NON-UNIQUE KEY – specifying doesn’t make sense, Since system always used this internally with specification of standard table.
Sorted internal table :
- Usage: Sorted table of.
- Automatically sorted by the key and fast access for sorted operations. Binary search algorithm
- It does not allow duplicates for the key field(s).
You need to specify either of these.
WITH UNIQUE KEY – table is sorted by keys specified after this keyword
WITH NON-UNIQUE KEY – table is sorted by keys specified after this keyword
Hashed internal table :
- Usage: Hashed table of
- You must always specify the UNIQUE option when you create a hashed table.
- Duplicates are not allowed
- Fast access via a unique key – hashed algorithm
WITH UNIQUE KEY – table is sorted by keys specified after this keyword.
WITH NON-UNIQUE KEY – can’t use and system raises syntax error.
Deep understanding of notation: WITH KEY
When you specify WITH KEY specification while declaring an internal table, the components specified after the key word WITH KEY acts as a primary key. It helps in faster search using the key field. We can also create secondary keys on an internal table based on the requirement which helps for searching easy with the key components
Lets differentiate different WITH KEY notations and usage :
- WITH KEY c1 c2 …. Cn :
- Forms a primary key with the components after WITH KEY
- Ex : WITH KEY c1 – means internal table has primary key c1
- WITH KEY TABLE_LINE :
- Forms a primary key with the elementary line type. we should use TABLE_LINE When row type of internal table is elementary line type like c, p, n etc.
- WITH DEFAULT KEY:
- If you have a structured line type, the primary key is built from all columns of the internal table that have a character-type type (c, d, t, n, x, String, XString).
- SORTED KEY:
- Table is sorted on the key specified after this syntax. A sorted key is primary key field for an sorted internal table. Can be used as a secondary table key for all types of internal tables. When entries are read, it follows binary search algorithm for search.
- HASHED KEY:
- Table is sorted on the key specified after this syntax. A Hashed key is primary key field for an hashed internal table. Can be used as a secondary table key for all types of internal tables. When entries are read, it follows Hashed algorithm for search.
In next page, you can see the syntax for these internal table declarations with examples. Also how the primary keys and secondary keys used in READ TABLE statements.