Types of Internal tables in SAP ABAP

  In SAP ABAP, internal tables are often referred to as "temporary tables" because they exist only during the runtime of a program and are used to store and manipulate data temporarily. Once the program execution is completed, the internal tables are automatically released from memory. This temporary nature makes internal tables a powerful tool for efficiently processing and organizing data within a program.


Here's an example code snippet to illustrate the usage of internal tables as temporary tables:

REPORT ZINTERNAL_TABLE_EXAMPLE. *-- Define a structure for the internal table TYPES: BEGIN OF ty_employee, employee_id TYPE i, employee_name TYPE string, salary TYPE p DECIMALS 2, END OF ty_employee. *-- Declare an internal table DATA: lt_employee TYPE TABLE OF ty_employee, wa_employee TYPE ty_employee. *-- Populate the internal table with sample data wa_employee-employee_id = 1001. wa_employee-employee_name = 'John Doe'. wa_employee-salary = 50000. APPEND wa_employee TO lt_employee. wa_employee-employee_id = 1002. wa_employee-employee_name = 'Jane Smith'. wa_employee-salary = 60000. APPEND wa_employee TO lt_employee. wa_employee-employee_id = 1003. wa_employee-employee_name = 'Bob Johnson'. wa_employee-salary = 55000. APPEND wa_employee TO lt_employee. *-- Display the original content of the internal table WRITE: / 'Original Content of Internal Table:', / 'Employee ID', 'Employee Name', 'Salary'. LOOP AT lt_employee INTO wa_employee. WRITE: / wa_employee-employee_id, wa_employee-employee_name, wa_employee-salary. ENDLOOP. *-- Modify data in the internal table LOOP AT lt_employee INTO wa_employee. wa_employee-salary = wa_employee-salary * 1.1. " Increase salary by 10% MODIFY lt_employee FROM wa_employee INDEX sy-tabix. ENDLOOP. *-- Display the modified content of the internal table WRITE: / 'Modified Content of Internal Table:', / 'Employee ID', 'Employee Name', 'Salary'. LOOP AT lt_employee INTO wa_employee. WRITE: / wa_employee-employee_id, wa_employee-employee_name, wa_employee-salary. ENDLOOP. *-- Temporary tables are automatically released when the program ends

In this example:
  1. We define a structure ty_employee representing the data structure for each employee.
  2. We declare an internal table lt_employee based on this structure.
  3. Sample data is appended to the internal table, and the original content is displayed.
  4. The program then modifies the salary of each employee in the internal table.
  5. The modified content of the internal table is displayed.
  6. At the end of the program, the internal table is automatically released from memory as it is a temporary structure.

This example demonstrates how internal tables serve as temporary containers for data manipulation within an ABAP program. The data in these tables is only relevant during the execution of the program and is not persisted beyond its scope.

In SAP ABAP, there are three main types of internal tables: standard tables, sorted tables, and hashed tables. Each type has its own characteristics and use cases. Here's an overview of each:

  1. Standard Tables:

    • Description: Standard tables are the most common type of internal table. They are unsorted and allow for linear searches. The order of the entries is the same as the order in which they were inserted.
    • Declaration:

  2. DATA: lt_standard TYPE TABLE OF <data_type>.

Sorted Tables:

  • Description: Sorted tables store data in a sorted order based on one or more key fields. This allows for efficient binary searches using the READ TABLE statement. However, maintaining the sorted order incurs some overhead during insertions and modifications.
  • Declaration:

DATA: lt_sorted TYPE TABLE OF <data_type> WITH UNIQUE KEY <key_fields>.

Hashed Tables:

  • Description: Hashed tables use a hash algorithm to directly access data based on a key field. This results in very fast access times, making them suitable for scenarios where quick access to specific entries is crucial. However, they are not suitable for operations that require sequential access.
  • Declaration:

  • DATA: lt_hashed TYPE HASHED TABLE OF <data_type> WITH UNIQUE KEY <key_fields>.

Here's a brief example illustrating the declaration of these internal table types:

TYPES: BEGIN OF ty_employee, employee_id TYPE i, employee_name TYPE string, salary TYPE p DECIMALS 2, END OF ty_employee. DATA: lt_standard TYPE TABLE OF ty_employee, lt_sorted TYPE TABLE OF ty_employee WITH UNIQUE KEY employee_id, lt_hashed TYPE HASHED TABLE OF ty_employee WITH UNIQUE KEY employee_id.

In this example, ty_employee is a structure representing the data for each employee. Three internal tables (lt_standard, lt_sorted, and lt_hashed) are declared based on this structure, each representing a different type of internal table.

It's important to choose the appropriate type of internal table based on the specific requirements of your program to achieve optimal performance. Standard tables are suitable for general-purpose storage, sorted tables for frequent searches, and hashed tables for direct access scenarios.


Post a Comment

0 Comments

Total Pageviews