SAP ABAP Internal Tables
SAP ABAP internal tables are temporary table-like data objects used inside an ABAP program. They store multiple rows of data in the application server memory while the program is running. The data is not saved permanently in the SAP database unless the program explicitly writes it back to a database table.
Internal tables are used when an ABAP program needs to collect, process, filter, sort, or display several records. For example, a program may read sales order data from a database table into an internal table, process it in memory, and then show the result in an ALV report or pass it to another routine.
Scenario : The same SAP database is accessed by all departments in an organization and each other department uses the same information for different purposes and utilize according their requirements and change of data is not required by the other departments. So the copy of the database table should be fetched into the program and to maintain copy of database table.
In this situation, an internal table gives the program a working copy of the selected database records. The program can sort, read, summarize, or format this copy without changing the original database table.
Types of Internal Tables in SAP ABAP
The three main types of internal tables in SAP ABAP are based on how table rows are stored and accessed.
- It is a default type of internal table.
- When the records are searching in standard internal tables, the searching time varies based on position of the record in the Internal Tables.
- It uses “Linear Search” algorithm for searching records in SAP system.
A standard internal table is suitable when records are mainly processed in the order in which they are inserted, or when the table is small enough that sequential access is acceptable. If you frequently search a standard table by key, you may sort it first and then use a binary search, or use a sorted or hashed internal table instead.
- Sorted internal tables enables in arrangement the records automatically in a sorted order by using key field.
- It uses “Binary Search” algorithm for searching records in SAP system.
A sorted internal table is always kept in ascending order by its table key. It is useful when the program often reads records by key and also needs the data in sorted order. Sorted tables can have a unique or non-unique key, depending on whether duplicate key values are allowed.
- Hashed internal tables are special type of internal table and search the record by specifying the key.
- Hashed internal tables users “Hashing Algorithm” to search the records
- Hashed tables wont allow Append, Insert Command while adding records. The table should be filled with database directly.
A hashed internal table is optimized for key-based access. It must have a unique key, and rows are accessed by that key rather than by a numeric index. In current ABAP programming, rows are commonly added to hashed internal tables using INSERT ... INTO TABLE. Since a hashed table has no meaningful row index, index-based operations such as appending to the end of a sequence are not the right model for this table type.
Choosing Between Standard, Sorted, and Hashed Internal Tables
| Internal table type | Best used when | Key behavior | Common access style |
|---|---|---|---|
| Standard table | Rows are processed sequentially or the table is small | Default key or explicitly defined key | Looping, appending, occasional reads |
| Sorted table | Rows must remain sorted by key | Unique or non-unique key | Key reads and ordered processing |
| Hashed table | Fast exact lookup by a unique key is required | Unique key only | Direct key access |
Use a standard table for simple lists, a sorted table when order and key access both matter, and a hashed table when the main operation is finding a single row by a unique key.
Features of SAP ABAP Internal Tables
- The data is stored record by record in memory and each line has the same structure.
- Internal tables are used for storing and formation the data from a database table within a program.
- An internal table contains rows with the same row type.
- The row type can be based on a database table, a structure, or a locally declared type.
- The table is available only during program execution unless its data is saved elsewhere.
- Rows can be added, read, modified, deleted, sorted, and looped through.
- The table category affects search behavior, permitted operations, and performance.
ABAP Internal Table Declaration Syntax
Syntax used for declaring the Internal table
DATA <ITAB> TYPE <DATATYPE>
In practical ABAP programs, the internal table declaration usually includes the row type and the table category. The following examples show common declarations.
TYPES: BEGIN OF ty_employee,
emp_id TYPE i,
emp_name TYPE string,
dept TYPE string,
END OF ty_employee.
DATA lt_employees TYPE STANDARD TABLE OF ty_employee WITH EMPTY KEY.
Here, ty_employee defines the structure of one row, and lt_employees is an internal table that can store many rows of that structure.
Declaring Standard, Sorted, and Hashed Tables in ABAP
The table category can be written explicitly in the declaration. This makes the program easier to read and avoids confusion about how the internal table should be accessed.
DATA lt_standard TYPE STANDARD TABLE OF ty_employee WITH EMPTY KEY.
DATA lt_sorted TYPE SORTED TABLE OF ty_employee
WITH UNIQUE KEY emp_id.
DATA lt_hashed TYPE HASHED TABLE OF ty_employee
WITH UNIQUE KEY emp_id.
In these declarations, lt_standard behaves as a normal sequential table, lt_sorted keeps rows sorted by emp_id, and lt_hashed allows direct lookup by the unique employee ID.
Adding Rows to an ABAP Internal Table
Rows can be inserted into an internal table using a work area or inline value construction. Modern ABAP commonly uses VALUE expressions for clearer examples.
APPEND VALUE ty_employee(
emp_id = 101
emp_name = 'Anil'
dept = 'Sales'
) TO lt_standard.
INSERT VALUE ty_employee(
emp_id = 102
emp_name = 'Meena'
dept = 'Finance'
) INTO TABLE lt_sorted.
INSERT VALUE ty_employee(
emp_id = 103
emp_name = 'Ravi'
dept = 'IT'
) INTO TABLE lt_hashed.
APPEND is normally used with standard tables. INSERT ... INTO TABLE is suitable when the target table category manages row position or key uniqueness, such as sorted and hashed internal tables.
Reading Data from an ABAP Internal Table
Internal tables can be read by index or by key, depending on the table type. Key-based reading is usually clearer when the program is looking for a specific business object, such as a customer number, material number, or employee ID.
READ TABLE lt_standard INTO DATA(ls_employee)
WITH KEY emp_id = 101.
IF sy-subrc = 0.
WRITE: / ls_employee-emp_name, ls_employee-dept.
ENDIF.
When READ TABLE finds a matching row, sy-subrc is set to 0. If no row is found, sy-subrc is set to a non-zero value.
Looping Through Rows in an Internal Table
LOOP AT is used to process each row of an internal table. This is common in reports, data validation, calculations, and output formatting.
LOOP AT lt_standard INTO DATA(ls_row).
WRITE: / ls_row-emp_id, ls_row-emp_name, ls_row-dept.
ENDLOOP.
For large tables, avoid unnecessary loops when a direct key read is enough. The right internal table type can reduce repeated searching and make the program easier to maintain.
Modifying and Deleting Rows in ABAP Internal Tables
ABAP provides statements to change or remove rows from an internal table. The key should identify the row clearly, especially when the table has many records.
READ TABLE lt_standard INTO DATA(ls_change)
WITH KEY emp_id = 101.
IF sy-subrc = 0.
ls_change-dept = 'Marketing'.
MODIFY lt_standard FROM ls_change INDEX sy-tabix.
ENDIF.
DELETE lt_standard WHERE dept = 'Finance'.
Use MODIFY when an existing row needs to be changed, and DELETE when one or more rows should be removed from the internal table. These operations affect only the internal table in memory unless further database update logic is written.
Internal Table Keys in SAP ABAP Programs
A table key defines the fields used to identify or order rows in an internal table. The key is important because it controls how sorted and hashed tables behave and how records are searched.
- Unique key: does not allow duplicate key values.
- Non-unique key: allows more than one row with the same key value.
- Empty key: commonly used for standard tables when no meaningful default key is needed.
Define keys carefully. A customer table may use customer number as a unique key, while a line-item table may allow many rows for the same document number and therefore need a non-unique key.
Internal Table Work Areas and Inline Declarations
Older ABAP programs often use a separate work area for reading or changing rows. Newer ABAP code may use inline declarations such as DATA(ls_row) directly inside statements.
DATA ls_employee TYPE ty_employee.
READ TABLE lt_standard INTO ls_employee
WITH KEY emp_id = 101.
READ TABLE lt_standard INTO DATA(ls_inline_employee)
WITH KEY emp_id = 102.
Both styles are valid. Inline declarations make examples shorter, while explicit work areas may be easier for beginners when learning how row data is moved in and out of internal tables.
Common Mistakes with SAP ABAP Internal Tables
- Using a standard table for repeated key lookups when a sorted or hashed table would be better.
- Forgetting to check
sy-subrcafterREAD TABLE. - Assuming that changes in an internal table automatically update the database.
- Using index-based logic on table types where key-based access is the intended approach.
- Declaring a table without thinking about whether duplicate key values should be allowed.
SAP ABAP Internal Table Example Program
The following example creates an internal table, adds employee records, reads one employee by key, and displays all rows.
REPORT zdemo_internal_table.
TYPES: BEGIN OF ty_employee,
emp_id TYPE i,
emp_name TYPE string,
dept TYPE string,
END OF ty_employee.
DATA lt_employees TYPE STANDARD TABLE OF ty_employee WITH EMPTY KEY.
APPEND VALUE ty_employee( emp_id = 101 emp_name = 'Anil' dept = 'Sales' ) TO lt_employees.
APPEND VALUE ty_employee( emp_id = 102 emp_name = 'Meena' dept = 'Finance' ) TO lt_employees.
APPEND VALUE ty_employee( emp_id = 103 emp_name = 'Ravi' dept = 'IT' ) TO lt_employees.
READ TABLE lt_employees INTO DATA(ls_employee)
WITH KEY emp_id = 102.
IF sy-subrc = 0.
WRITE: / 'Employee found:', ls_employee-emp_name.
ENDIF.
LOOP AT lt_employees INTO DATA(ls_row).
WRITE: / ls_row-emp_id, ls_row-emp_name, ls_row-dept.
ENDLOOP.
This example uses a standard table because the data set is small and the program mainly demonstrates simple storage and reading. In a real program with frequent employee ID lookups, a hashed table with a unique key on emp_id may be a better choice.
FAQ on SAP ABAP Internal Tables
What is an internal table in SAP ABAP?
An internal table in SAP ABAP is a temporary table-like object that stores multiple rows of data in application server memory while an ABAP program is running.
Which internal table type is fastest for key-based search?
A hashed internal table is usually the best choice for exact key-based search when the key is unique. A sorted table is useful when both ordered processing and key reads are needed.
Can an ABAP internal table store data permanently?
No. An internal table stores data temporarily during program execution. To save data permanently, the program must write the data to a database table or another persistent storage object.
What is the difference between a standard table and a sorted table in ABAP?
A standard table stores rows in the order they are inserted unless the program sorts it. A sorted table automatically keeps rows sorted by its declared table key.
When should I use a hashed internal table in ABAP?
Use a hashed internal table when each row can be identified by a unique key and the program frequently reads rows by that exact key.
Editorial QA Checklist for SAP ABAP Internal Tables
- Confirm that the article clearly explains internal tables as temporary in-memory data objects.
- Check that standard, sorted, and hashed internal tables are described with correct access behavior.
- Verify that examples use appropriate ABAP statements such as
APPEND,INSERT ... INTO TABLE,READ TABLE,LOOP AT,MODIFY, andDELETE. - Ensure that key concepts such as unique key, non-unique key, and empty key are not confused.
- Review whether the table-type selection guidance matches the access pattern described in the tutorial.
TutorialKart.com