✒️ABAP Las tablas internas
ABAP Las tablas internas
Internal Tables
Internal tables are data objects that allow storing large amounts of records with the same structure in memory, from 0 to n records. The maximum limit is related to the limits specified in the SAP installation. They exist only at runtime. In other languages, this concept of internal table is known as an array, vector, list, or collection. Typical uses:
- Temporarily storing data from database tables for future processing.
- Structuring and formatting data to be displayed as program output.
- Formatting data for use by other services.
Internal Tables Declaration
To work with an internal table, we need a structure or work area or header of the internal table, which has the same format as the internal table and allows us to:
- Read the content of a record
- Traverse each record.
- Add a record
- Modify the content of a record.
- This structure can only store 1 record of data.
Ways to declare an internal table:
- 1. The most efficient way is to use TYPES, where we first declare a data type (ty_suppliers) and then declare the internal table (ti_suppliers) of that data type using the statement TYPE STANDARD TABLE OF. Finally, we declare the structure or work area (wa_suppliers) that allows us to work with the internal table using the statement LIKE LINE OF.
TYPES: BEGIN OF ty_suppliers,
dni(8) TYPE c,
name(30) TYPE c,
END OF ty_suppliers.
DATA: ti_suppliers TYPE STANDARD TABLE OF ty_suppliers,
wa_suppliers LIKE LINE OF ti_suppliers.
- 2. A second way to declare the internal table (ti_suppliers) is by using the OCCURS 0 statement, and to declare the structure or work area (wa_suppliers), we use the LIKE LINE OF statement.
DATA: BEGIN OF ti_suppliers OCCURS 0,
dni(8) TYPE c,
name(30) TYPE c,
END OF ti_suppliers.
DATA: wa_suppliers LIKE LINE OF ti_suppliers.
- 3. A third way to declare an internal table is to first declare the structure (wa_suppliers) and then declare the internal table (ti_suppliers) of the same data type as the previously declared structure.
DATA: BEGIN OF wa_suppliers,
dni(8) TYPE c,
name(30) TYPE c,
END OF wa_suppliers.
DATA: ti_suppliers LIKE STANDARD TABLE OF wa_suppliers.
- 4. A fourth way to declare the internal table (TI_STRUCTURE) is to declare it of the same type as an existing database table in the ABAP data dictionary.
"LFA1 is a master table of suppliers
DATA: lt_supplier_data TYPE TABLE OF lfa1,
ls_supplier_data LIKE LINE OF lt_supplier_data.
SELECT * FROM lfa1 INTO TABLE lt_supplier_data.
LOOP AT lt_supplier_data INTO ls_supplier_data.
" Your processing logic here, for example:
WRITE:/ ls_supplier_data-lifnr, ls_supplier_data-name1, ls_supplier_data-city.
ENDLOOP.
- 5. Declaring the structure or work area or header of an internal table using the same internal table as the header.
DATA: ti_suppliers LIKE TABLE OF lfa1 WITH HEADER LINE.
Adding Content to Internal Tables
- Perform a select and store the result in the internal table.
- Use INSERT to insert records into the internal table.
- Add records with APPEND TO.
Types of Internal Tables
- AnyTables: Please note that the ANY TABLE type should be used cautiously, as it sacrifices the benefits of strong typing. It seems there is a limitation in the ABAP runtime, and the ANY TABLE type might not be supported in certain scenarios.
DATA: lt_any TYPE ANY TABLE,
ls_any TYPE string.
APPEND 'Item 1' TO lt_any.
APPEND 'Item 2' TO lt_any.
LOOP AT lt_any INTO ls_any.
WRITE:/ ls_any.
ENDLOOP.
- IndexTable
TYPES: BEGIN OF ty_index,
table_key TYPE string,
value TYPE string,
END OF ty_index.
DATA: lt_index TYPE TABLE OF ty_index WITH NON-UNIQUE KEY table_key,
ls_index TYPE ty_index.
APPEND VALUE #( table_key = 'Key1' value = 'Item 1' ) TO lt_index.
APPEND VALUE #( table_key = 'Key2' value = 'Item 2' ) TO lt_index.
READ TABLE lt_index INTO ls_index WITH KEY table_key = 'Key1'.
IF sy-subrc EQ 0.
WRITE:/ 'Item found:', ls_index-value.
ELSE.
WRITE:/ 'Item not found.'.
ENDIF.
- Standard Table (Most used)
DATA: lt_standard TYPE TABLE OF string,
ls_standard TYPE string.
APPEND 'Item 1' TO lt_standard.
APPEND 'Item 2' TO lt_standard.
LOOP AT lt_standard INTO ls_standard.
WRITE:/ ls_standard.
ENDLOOP.
- Sorted Table
TYPES: BEGIN OF ty_sorted,
table_key TYPE string,
value TYPE string,
END OF ty_sorted.
DATA: lt_sorted TYPE TABLE OF ty_sorted WITH NON-UNIQUE SORTED KEY table_key COMPONENTS value.
APPEND VALUE #( table_key = 'Key2' value = 'Item 2' ) TO lt_sorted.
APPEND VALUE #( table_key = 'Key1' value = 'Item 1' ) TO lt_sorted.
LOOP AT lt_sorted INTO DATA(ls_sorted).
WRITE:/ ls_sorted-value.
ENDLOOP.
- Hashed Table
TYPES: BEGIN OF ty_hashed,
table_key TYPE string,
value TYPE string,
END OF ty_hashed.
DATA: lt_hashed TYPE HASHED TABLE OF ty_hashed WITH UNIQUE KEY table_key,
ls_hashed TYPE ty_hashed.
INSERT VALUE #( table_key = 'Key1' value = 'Item 1' ) INTO TABLE lt_hashed.
INSERT VALUE #( table_key = 'Key2' value = 'Item 2' ) INTO TABLE lt_hashed.
READ TABLE lt_hashed INTO ls_hashed WITH KEY table_key = 'Key1'.
IF sy-subrc EQ 0.
WRITE:/ 'Item found:', ls_hashed-value.
ELSE.
WRITE:/ 'Item not found.'.
ENDIF.
Internal Tables Sorting
Use the SORT statement to sort by any field that is part of the internal table, in ascending (ASCENDING) or descending (DESCENDING) order.
SORT ti_suppliers BY dni DESCENDING.
Internal Tables Iteration
To traverse an internal table and process its content record by record, use the LOOP-ENDLOOP statement. You can restrict the records you want to traverse based on a condition specified using the WHERE statement. If no record meets the specified condition in the WHERE clause, SY-SUBRC will be different from zero. More than one condition can be specified with AND or OR concatenation.
- With LOOP - ENDLOOP:
LOOP AT it_suppliers INTO wa_suppliers.
WRITE:/ wa_suppliers-dni,
wa_suppliers-name,
wa_suppliers-surname.
ENDLOOP.
- You can restrict records with the WHERE clause:
LOOP AT ti_suppliers INTO wa_suppliers WHERE NOT name IS INITIAL.
WRITE:/ wa_suppliers-dni,
wa_suppliers-name,
wa_suppliers-surname.
ENDLOOP.
Reading values from an Internal Table
- To read an internal table without traversing it record by record, use the READ TABLE statement. The work area is needed.
READ TABLE it_suppliers INTO wa_supplier WITH KEY name = 'Jega'.
IF sy-subrc EQ 0.
WRITE: / 'Supplier found - Name:', wa_supplier-name, 'DNI:', wa_supplier-dni.
ELSE.
WRITE: / 'Supplier not found.'.
ENDIF.
- To optimize this search, use binary search, which finds the position of a value in a sorted internal table. First, sort the table with SORT and then perform the read with BINARY SEARCH.
SORT ti_suppliers ASCENDING BY name.
READ TABLE ti_suppliers INTO wa_suppliers WITH KEY name='Jega' BINARY SEARCH.
IF sy-subrc EQ 0.
WRITE: / 'Supplier found - Name:', wa_supplier-name, 'DNI:', wa_supplier-dni.
ELSE.
WRITE: / 'Supplier not found.'.
ENDIF.
- If you want to find a specific position, use INDEX.
READ TABLE ti_suppliers INTO wa_suppliers INDEX 5.
IF sy-subrc EQ 0.
WRITE: / 'Supplier found - Name:', wa_supplier-name, 'DNI:', wa_supplier-dni.
ELSE.
WRITE: / 'Supplier not found.'.
ENDIF.
Others statements over Internal Tables.
- To modify the content, use MODIFY. The work area is required.
wa_proveedores-dni = '24987500'.
MODIFY ti_suppliers FROM wa_suppliers INDEX 1.
IF sy-subrc EQ 0.
WRITE: / 'OK'.
ELSE.
WRITE: / 'Error'.
ENDIF.
- To add a record, use INSERT. The work area is needed.
wa_proveedores-nombre = 'Marcelo'.
wa_proveedores-apellido = 'Rivarola'.
wa_proveedores-dni = '20857492'.
INSERT wa_proveedores INTO ti_proveedores INDEX 2.
IF sy-subrc EQ 0.
WRITE: / 'OK'.
ELSE.
WRITE: / 'Error'.
ENDIF.
- For deletion, use DELETE.
DELETE it_suppliers WHERE dni='28280933'.
IF sy-subrc EQ 0.
WRITE: / 'OK'.
ELSE.
WRITE: / 'Error'.
ENDIF.
- To delete all content of an internal table, use REFRESH.
REFRESH it_suppliers.
- To determine the number of records in an internal table, use DESCRIBE TABLE. Declare the variable V_LINES of type i.
DATA: v_lines TYPE i.
DESCRIBE TABLE it_suppliers LINES v_lines.
IF v_lines > 0.
ENDIF.
- To initialize the work area or header, use CLEAR wa_suppliers.
CLEAR wa_suppliers.
- To free up the space occupied by an internal table in memory, use FREE ti_suppliers.
FREE ti_suppliers.
E.g
* Internal table for passengers
DATA: BEGIN OF it_passengers OCCURS 0,
passport(10) TYPE c,
full_name(30) TYPE c,
birthdate TYPE dats,
END OF it_passengers.
* Structure for passengers
DATA: wa_passenger LIKE LINE OF it_passengers.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
CLEAR wa_passenger.
wa_passenger-passport = 'ABC123456'.
wa_passenger-full_name = 'John Smith'.
wa_passenger-birthdate = '19851215'.
APPEND wa_passenger TO it_passengers.
CLEAR wa_passenger.
wa_passenger-passport = 'XYZ987654'.
wa_passenger-full_name = 'Alice Johnson'.
wa_passenger-birthdate = '19780423'.
APPEND wa_passenger TO it_passengers.
CLEAR wa_passenger.
wa_passenger-passport = 'DEF345678'.
wa_passenger-full_name = 'Michael Brown'.
wa_passenger-birthdate = '19901102'.
APPEND wa_passenger TO it_passengers.
WRITE:/1(20) 'Passport',
21(30) 'Full Name',
51(20) 'Birthdate'.
LOOP AT it_passengers INTO wa_passenger.
WRITE:/1(20) wa_passenger-passport,
21(30) wa_passenger-full_name,
51(20) wa_passenger-birthdate.
ENDLOOP.
 
 
 
2 Agradecimientos:
Han agradecido este aporte: Alvaro Ardila Sandoval, Octavio Pasciucco
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 99 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez