Module 5: Internal Tables & Work Areas
Internal tables are the heart of ABAP programming. Almost every ABAP application uses internal tables to store, process, and manipulate data fetched from the database or received from interfaces.
1. What Are Internal Tables?
An internal table is a temporary data structure stored in application server memory.
Why Internal Tables Are Important
- Store database records
- Perform calculations and transformations
- Transfer data between programs
- Improve performance by minimizing DB access
Internal tables exist only during program runtime and are deleted automatically afterward.
2. Work Areas
A work area is a single-row structure used to process one record at a time.
Usage
-
Reading one row from an internal table
-
Modifying a single record
-
Temporary data holder
Traditional Work Area
DATA: gs_employee TYPE ty_employee.
Inline Work Area
READ TABLE gt_employee INTO DATA(gs_employee).
Prefer inline work areas when scope is limited.
3. Types of Internal Tables
ABAP provides three main internal table types.
3.1 Standard Tables
-
Index-based access
-
Allows duplicate entries
-
Slow key-based access
DATA gt_std TYPE STANDARD TABLE OF i.
Use When
a. Sequential processing
b. Small datasets
c. Order matters
3.2 Sorted Tables
-
Automatically sorted by key
-
No duplicate keys (by default)
-
Faster read than standard tables
DATA gt_sorted TYPE SORTED TABLE OF i
WITH UNIQUE KEY table_line.
Use When
a. Frequent READ operations
b. Sorted data is required
3.3 Hashed Tables
-
Hash-based access
-
No index
-
Fastest key-based access
-
Unique keys mandatory
DATA gt_hashed TYPE HASHED TABLE OF i
WITH UNIQUE KEY table_line.
Use When
a. Large datasets
b. Frequent key-based access
Standard < Sorted < Hashed (for key access)
4. Table Keys
4.1 Primary Key
-
Defined during table declaration
-
Used for fast access
DATA gt_emp TYPE SORTED TABLE OF ty_employee
WITH UNIQUE KEY emp_id.
4.2 Secondary Keys (Advanced)
-
Improve read performance
-
Optional
Define keys consciously. Wrong table type = poor performance.
5. Core Operations on Internal Tables
5.1 READ TABLE
Reads a single row from an internal table.
READ TABLE gt_emp INTO DATA(gs_emp)
WITH KEY emp_id = 1001.
Check result:
IF sy-subrc = 0.
WRITE 'Record found'.
ENDIF.
Ignoring sy-subrc leads to runtime errors.
5.2 LOOP AT
Iterates over internal table entries.
LOOP AT gt_emp INTO DATA(gs_emp).
WRITE gs_emp-name.
ENDLOOP.
With condition:
LOOP AT gt_emp INTO DATA(gs_emp)
WHERE dept = 'IT'.
WRITE gs_emp-name.
ENDLOOP.
5.3 MODIFY
Updates existing entries.
MODIFY gt_emp FROM gs_emp.
With key:
MODIFY gt_emp FROM gs_emp
TRANSPORTING salary
WHERE emp_id = 1001.
5.4 DELETE
Deletes rows from internal tables.
DELETE gt_emp WHERE dept = 'HR'.
Delete current loop row:
DELETE gt_emp INDEX sy-tabix.
Deleting inside loops must be handled carefully to avoid logical errors.
6. Performance Considerations
6.1 Choose the Right Table Type
| Scenario | Recommended Table |
|---|---|
| Sequential processing | Standard |
| Sorted access | Sorted |
| Fast key lookup | Hashed |
6.2 Avoid Nested Loops
❌ Bad Practice
LOOP AT gt_a INTO DATA(ls_a).
LOOP AT gt_b INTO DATA(ls_b).
" heavy logic
ENDLOOP.
ENDLOOP.
✅ Better Approach
-
Use hashed tables
-
Use READ with key
6.3 Avoid Database Access Inside Loops
❌ Bad Practice
LOOP AT gt_emp INTO DATA(ls_emp).
SELECT SINGLE * FROM zemp
WHERE emp_id = ls_emp-emp_id.
ENDLOOP.
✅ Good Practice
SELECT * FROM zemp INTO TABLE @DATA(gt_db).
LOOP AT gt_emp INTO DATA(ls_emp).
READ TABLE gt_db INTO DATA(ls_db)
WITH KEY emp_id = ls_emp-emp_id.
ENDLOOP.
Database access inside loops is one of the biggest performance killers in ABAP.
7. Common Beginner Mistakes
-
Using STANDARD tables for key-based access
-
Not defining keys
-
Ignoring sy-subrc
-
Unnecessary work areas
-
Nested loops with large datasets
8. Summary
-
Internal tables are core ABAP data structures
-
Choose the correct table type
-
Define keys properly
-
Use READ, LOOP, MODIFY, DELETE carefully
-
Optimize for performance early
9. Practice Exercises
-
Create a sorted table with employee data.
-
Read a record using a key.
-
Update salary using MODIFY.
-
Delete records based on condition.
-
Compare performance of STANDARD vs HASHED table.
10. What’s Next?
➡️ Module 6: Open SQL Basics
If you master internal tables, performance tuning becomes intuitive.