Working with Internal Tables: The Power of Data Management

August 26, 2024

Working with Internal Tables: The Power of Data Management

This lesson introduces internal tables in ABAP. You will learn how to create, manipulate, and process internal tables, which are essential for handling large datasets in your applications.

What are Internal Tables?

Internal tables are a powerful feature in ABAP that allows you to store and manage data in a structured way. They can be thought of as temporary data storage areas in memory, where you can hold multiple records of the same structure. Internal tables are particularly useful when dealing with large datasets that need to be processed in your ABAP programs.

Creating Internal Tables

To create an internal table, you first need to define its structure using the DATA statement. Here is an example:

DATA: lt_employees TYPE TABLE OF ty_employee.

In this example, lt_employees is an internal table defined to hold records of type ty_employee. You would need to define the structure ty_employee beforehand, like so:

TYPES: BEGIN OF ty_employee,
         id TYPE i,
         name TYPE string,
         position TYPE string,
       END OF ty_employee.

Populating Internal Tables

Once you have defined your internal table, you can populate it with data. You can use the APPEND statement to add records to the table:

DATA: ls_employee TYPE ty_employee.

ls_employee-id = 1.
ls_employee-name = 'John Doe'.
ls_employee-position = 'Developer'.

APPEND ls_employee TO lt_employees.

You can repeat this process to add as many records as you need.

Reading from Internal Tables

To read data from an internal table, you can use a loop. Here’s how you can loop through the records of an internal table:

LOOP AT lt_employees INTO ls_employee.
  WRITE: / ls_employee-id, ls_employee-name, ls_employee-position.
ENDLOOP.

This loop will output the ID, name, and position of each employee stored in the lt_employees internal table.

Modifying Internal Tables

ABAP allows you to modify records in internal tables as well. You can use the MODIFY statement to change existing records:

READ TABLE lt_employees INTO ls_employee WITH KEY id = 1.
IF sy-subrc = 0.
  ls_employee-position = 'Senior Developer'.
  MODIFY lt_employees FROM ls_employee.
ENDIF.

In this example, we read the employee with ID 1 and change their position to ‘Senior Developer’.

Deleting from Internal Tables

If you need to remove records from an internal table, you can use the DELETE statement:

DELETE lt_employees WHERE id = 1.

This command will delete the employee record with ID 1 from the lt_employees internal table.

Sorting and Filtering Internal Tables

ABAP provides built-in functions to sort and filter internal tables. For example, you can sort an internal table using the SORT statement:

SORT lt_employees BY name.

You can also filter records using the DELETE ADJACENT DUPLICATES statement to remove duplicates:

DELETE ADJACENT DUPLICATES FROM lt_employees COMPARING name.

Conclusion

Internal tables are a fundamental aspect of data management in ABAP. They allow you to efficiently handle and manipulate large datasets within your applications. In this lesson, you learned how to create, populate, read, modify, and delete records from internal tables, as well as how to sort and filter them. Mastering internal tables will significantly enhance your ability to develop robust ABAP applications.