Working With Complex Internal Tables
Working With Complex Internal Tables
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
Internal Tables
Declaring a Complex Internal Table
22 mins
As you already learned, the simplest way to add a new row to an internal table
is the APPEND statement with a data object whose type corresponds to the
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 1/23
8/15/25, 10:18 AM Working with Complex Internal Tables
row type of the internal table. This data object is sometimes referred to as
Learning Subscribe
work area.
For simple internal tables the work area used in APPEND can be a scalar
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
variable, constant, or a literal. For complex internal tables, the work area has
to be structured.
It reveals the purpose of the structured variable as work area for the
internal table
It ensures that the work area fits to the internal table, even if the definition
of the internal table changes
If you do not fill the work area before the APPEND statement, the new row of
the internal table will be filled with type-specific initial values.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 2/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning Subscribe
Hint
You get the same result with the special variant APPEND INITIAL
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
LINE TO <internal_table>. This variant does not even require a work
area.
To fill the structured work area, you can either fill the individual components
or, as you can see in the example, use a VALUE #( ) expression.
As you can see in the example, you can also use a VALUE #( ) expression
directly in the APPEND statement. In this case, you do not need a work area.
Note
This can have a positive effect on the overall memory consumption
of your program.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 3/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning Subscribe
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
There is a variant of the VALUE #( ) expression that you can assign directly to
an internal table. In this variant of VALUE #( ) additional pairs of brackets are
used to separate the table rows from each other.
The code example fills internal table carriers with three rows, each with a
different value for carrier_id and carrier_name. As a result of this, column
currency_code is not mentioned, it is filled with the type specific initial value.
Note
With the assignment above, all existing table rows are removed
before the table is filled with the new rows.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 4/23
8/15/25, 10:18 AM Working with Complex Internal Tables
To copy data between identically-named fields of two internal tables, use the
Learning Subscribe
CORRESPONDING operator. This works similarly to CORRESPONDING for
structures: for each row of the source internal table, the system creates a new
/ Browse row in the target
/ Learning internal
Journeys table and
/ Acquiring copies
Core ABAP data
Skills between identically-named
/ Working with Complex Int…
fields. Source fields for which there is no identically named field in the target
are not copied. Target fields for which there is no identically named field in the
source are filled with type-specific initial values.
In the example, the source internal table carriers contains three rows.
Therefore, after the value assignment, the target internal table connections
also contains three rows.
Fields carrier_id and carrier_name exist in both internal tables. They are
copied from source to target. Field currency_code only exists in the source. It
is not copied. Fields connection_id, airport_from_id, and airport_to_id exist
only in the target. They are filled with initial values.
Note
If the target internal table contains data before the assignment, the
system deletes it.
Demo
Start Demo
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 5/23
8/15/25, 10:18 AM Working with Complex Internal Tables
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
Code Snippet
1
2 TYPES: BEGIN OF st_connection,
3 carrier_id TYPE /dmo/carrier_i
4 connection_id TYPE /dmo/connectio
5 airport_from_id TYPE /dmo/airport_f
6 airport_to_id TYPE /dmo/airport_t
7 carrier_name TYPE /dmo/carrier_n
8 END OF st_connection.
9
10 TYPES tt_connections TYPE STANDARD TABLE OF
11 WITH NON-UNIQUE KEY
12
13
14 DATA connections TYPE tt_connections.
15
16 TYPES: BEGIN OF st_carrier,
17 carrier_id TYPE /dmo/carrier_id,
18 carrier_name TYPE /dmo/carrier_nam
19 currency_code TYPE /dmo/currency_co
20 END OF st_carrier.
21
22 TYPES tt_carriers TYPE STANDARD TABLE OF st_
23 WITH NON-UNIQUE KEY ca
24
25 DATA carriers TYPE tt carriers.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 6/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Earlier in this course, you learned how to retrieve a single row from a simple
internal table using an internal table expression. Back then we used an index
access, that is, we identified the row through its position in the internal table.
This index access works for complex internal tables in just the same way. For
complex internal tables, however, internal table expressions with key access
become important, where you identify the row through its content.
Note
Even though this is called a key access, you can use any fields for
the selection, not only key fields of the internal table. If more than
one row fulfills the requirement, the first row is returned, that is, the
row with the lowest index.
The example reads a single row from internal table connections. The key of
this internal table consists of fields, carrier_id and connection_id, but the key
access uses airport_from_id and airport_to_id to identify the row. The Internal
table contains two connections from airport SFO to SIN, so the first of them is
returned.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 7/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning Subscribe
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
After reading the content of a table row into a work area, you sometimes want
to write changes from the work area back into the internal table. One way to
do this is the MODIFY TABLE statement.
This statement is a key access because the system uses the content of the key
fields in the work area to identify the table row that needs to be modified. It
then overwrites this table row with the contents of the work area.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 8/23
8/15/25, 10:18 AM Working with Complex Internal Tables
In the example, the work area carrier contains value 'JL' in key field, carrier_id.
Learning Subscribe
Based on this value, the system identifies the second row to be updated. This
row is then updated with the values from the work area.
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
Note
You can only change non-key fields with MODIFY TABLE. The
statement does not support changes to key fields.
Note
There is also a special variant without addition INDEX. We will
discuss this variant next.
In the example, the MODIFY statement uses the INDEX addition to address
the first table row. In this row, all fields are overwritten with the values from the
work area, even key field carrier_id.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 9/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning Subscribe
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
There will often be times when you need to modify the contents of multiple
rows of an internal table, or maybe even all of them. To do this, you implement
a loop over the table, which places each row you need to change successively
into a work area. Within the loop, you first change the contents of the work
area and then write the changes back into the internal table using the MODIFY
statement.
Note
If you do not write your changes back into the table, the changes
will be lost when the work area is filled with the data from next row.
In the example, the loop reads all rows of internal table carriers for which field
currency_code is not yet filled. This is the case for the last two rows. For each
of these rows the program replaces the initial value in field currency_code
with the new value 'USD'. Finally, it uses the MODIFY statement to overwrite
the current row with the updated values.
Instead of specifying the index explicitly, the code example uses a short form
of the MODIFY statement where the INDEX addition is missing. This short form
is only allowed between LOOP … ENDLOOP. Only there the system can
implicitly update the row it is currently working on.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 10/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Code Snippet
1
2 TYPES: BEGIN OF st_connection,
3 carrier_id TYPE /dmo/carrier_
4 connection_id TYPE /dmo/connecti
5 airport_from_id TYPE /dmo/airport_
6 airport_to_id TYPE /dmo/airport_
7 carrier_name TYPE /dmo/carrier_
8 END OF st_connection.
9
10 TYPES tt_connections TYPE SORTED TABLE OF
11 WITH NON-UNIQUE K
12
13
14 DATA connections TYPE tt_connections.
15 DATA connection LIKE LINE OF connections.
16
17 TYPES: BEGIN OF st_carrier,
18 carrier_id TYPE /dmo/carrier_id
19 currency_code TYPE /dmo/currency_c
20 END OF st_carrier.
21
22 DATA carriers TYPE STANDARD TABLE OF st_car
23 WITH NON-UNIQUE KEY carr
24
25 DATA carrier LIKE LINE OF carriers.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 11/23
8/15/25, 10:18 AM Working with Complex Internal Tables
The ABAP SQL statement, SELECT, reads data from a database table or a CDS
View. When you use the SINGLE option, exactly one record is read from the
database, even if more data exist that meets the conditions in the WHERE
clause.
As you learned earlier, one way to receive this single record result is
structured variable after keyword INTO.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 12/23
8/15/25, 10:18 AM Working with Complex Internal Tables
If you use SELECT without SINGLE, you indicate that you are interested in all
Learning Subscribe
records that match the conditions in the WHERE clause. You then have to
make sure that you can actually receive and store multiple records. The
/ Browse obvious wayJourneys
/ Learning to do this
/ isAcquiring
the usage ofABAP
Core a complex
Skills internal
/ Workingtable
withas target Int
Complex of…the
SELECT statement. This is possible but it requires addition TABLE between
keyword INTO and the name of the internal table.
In the example, we want to read all three airports related to London and not
just a single one of them. Therefore, we leave out the keyword SINGLE after
SELECT, add keyword TABLE after INTO and use internal table airports_full as
target of the SELECT statement.
The example uses an explicit field list after FIELDS that matches the columns
of internal table airports_full. Of course, you can also use FIELDS *, INTO
CORRESPONDING FIELDS OF TABLE, and alias names in the field list.
This example uses FIELDS * instead of an explicit field list and INTO
CORRESPONDING FIELDS OF TABLE instead of INTO TABLE.
As the row type of internal table airports contains only two components
AirportID and Name, only the fields with the same name are read from the
database.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 13/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning Subscribe
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
If you use DATA( ) in a SELECT statement after addition INTO TABLE, you inline
declare an internal table. The row type of this internal table is derived from the
FIELDS clause. For table fields and view elements an alias name is optional.
For expressions in the FIELDS clause, an alias name is mandatory if the INTO
clause contains an inline declaration.
Note
Inline declarations of internal tables are only supported after INTO
TABLE. You cannot use inline declarations after INTO
CORRESPONDING FIELDS OF TABLE.
Inline-declared internal tables are always standard tables without a key. You
cannot declare sorted or hashed tables using inline declarations. This can
cause performance problems if you fill the internal table with many rows and
use key access a lot.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 14/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning Subscribe
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
When you are reading multiple records from the database, some special SQL
techniques become particularly interesting. One of these techniques is the
UNION directive to combine the results of several SELECT statements.
The first SELECT result reads ID and NAME of all carriers with
CURRENCY_CODE = 'GBP'. The second SELECT reads ID and NAME of all
airports with CITY = 'London'. The first SELECT returns one record, the second
SELECT returns three records. Instead of retrieving these results separately,
they are combined into one result with four records. It is important to point out
that this happens inside the database.
A prerequisite for this technique is, of course, that the two results are
compatible with each other, that is, that they have the same number of fields,
the same field names. It is beneficial, though not necessary, that the types of
the fields are also the same.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 15/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning Subscribe
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
The ABAP SQL syntax for this example consists of two SELECT statements.
Each SELECT statement has its own FROM clause, FIELDS clause, and WHERE
clause, but there is only one INTO clause at the very end. The two SELECT
statements are connected by keywords UNION ALL.
Note
With UNION instead of UNION ALL, the database would look for
and eliminate duplicates before returning the result. We use UNION
ALL to avoid this unnecessary additional load on the database.
Both field lists consist of three elements, the first and second element have
identical alias names in both FIELDS clauses. The third field does not need an
alias because the field name is the same in both CDS Views.
Note
The first element in FIELDS is a literal text that allows us to
distinguish between Airlines and Airports in the combined result.
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 16/23
8/15/25, 10:18 AM Working with Complex Internal Tables
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
Code Snippet
1
2 TYPES: BEGIN OF st_airport,
3 airportid TYPE /dmo/airport_id,
4 name TYPE /dmo/airport_name,
5 END OF st_airport.
6
7 TYPES tt_airports TYPE STANDARD TABLE OF st_
8 WITH NON-UNIQUE KEY a
9
10 DATA airports TYPE tt_airports.
11
12
13 * Example 1: Structured Variables in SELECT SIN
14 ***********************************************
15
16 DATA airport_full TYPE /DMO/I_Airport.
17
18 SELECT SINGLE
19 FROM /DMO/I_Airport
20 FIELDS AirportID, Name, City, CountryCode
21 WHERE City = 'Zurich'
22 INTO @airport_full.
23
24 out->write( `-----------------------------
25 out->write( `Example 1: SELECT SINGLE ...
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 17/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Template:
/LRN/CL_S4D400_STS_STRUCTURE (global Class)
Solution:
/LRN/CL_S4D400_ITS_ITAB (global Class)
Steps
1. Copy the class /LRN/CL_S4D400_STS_STRUCTURE to a class in your own package
(suggested name: ZCL_##_ITAB , where ## stands for your group number).
e. Enter the name of your package in the Package field. In the Name field,
enter the name ZCL_##_ITAB , where ## stands for your group number.
Steps
1. Switch to the local class lcl_connection .
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 18/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Component
/ Browse / Learning Journeys / AcquiringData
CoreType
ABAP Skills / Working with Complex Int…
Name
AirportID /dmo/airport_id
Name /dmo/airport_name
Code Snippet
1
2 TYPES:
3 BEGIN OF st_details,
4 DepartureAirport TYPE /dmo/airport_from
5 DestinationAirport TYPE /dmo/airport_to_i
6 AirlineName TYPE /dmo/carrier_name
7 END OF st_details.
8
9 TYPES:
10 BEGIN OF st_airport,
11 AirportId TYPE /dmo/airport_id,
12 Name TYPE /dmo/airport_name,
13 END OF st_airport.
14
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 19/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning Subscribe
Property Value
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
Line type st_airport
NON-UNIQUE DEFAULT
Key Definition
KEY
Code Snippet
1
2 TYPES:
3 BEGIN OF st_airport,
4 AirportId TYPE /dmo/airport_id,
5 Name TYPE /dmo/airport_name,
6 END OF st_airport.
7
8 TYPES tt_airports TYPE STANDARD TABLE OF st_a
9 WITH NON-UNIQUE DEFAUL
10
4. Declare a new private static attribute airports and type it with table type
tt_airports. .
Code Snippet
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 20/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning 1 Subscribe
2 CLASS-DATA airports TYPE tt_airports.
3
/ Browse / Learning Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
Steps
1. Add a class constructor to the local class lcl_connection using a quick fix.
a. Place the cursor on the name of the class and press Ctrl + 1 .
2. In the class constructor, implement a SELECT statement that reads all data
sets from the CDS view entity /DMO/I_Airports into the static attribute
Airports .
Code Snippet
1
2 SELECT FROM /DMO/I_Airport
3 FIELDS AirportID, Name
4 INTO TABLE @airports.
5
Steps
1. Navigate to the implementation of the method get_output .
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 21/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning Hint
Use a table expression Airports[ ... ] and an inline
declaration for the data object departure .
Quick Links
Download Catalog (CSV, JSON, XLSX, XML)
a. At the beginning of the method, add the following code:
SAP Learning Hub
SAP Community 1
Newsletter
2 DATA(departure) = airports[ airportID = detail
3
Learning Support
Get Support
Share Feedback
About SAP 3. Similarly, read the details of the destination airport into a structured data
object destination .
Company Information
a. Adjust the code as follows:
Copyright
Trademark
Code Snippet
Worldwide Directory
Careers 1
News and Press
2 DATA(departure) = airports[ airportID = detail
3 DATA(destination) = airports[ airportID = detail
Site Information
4
Privacy
Terms of Use
Legal Disclosure
4. Use the component name of the two structures to add the airport names to
the
Do Not Share/sell Myoutput.
Personal Information (us Learners Only)
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 22/23
8/15/25, 10:18 AM Working with Complex Internal Tables
Learning
Code Snippet
Subscribe
/ Browse / Learning1Journeys / Acquiring Core ABAP Skills / Working with Complex Int…
2 APPEND |Departure: { details-departureairpor
3 APPEND |Destination: { details-destinationairp
4
5. Optional: Omit the structured data object and use the table expressions
directly in the string templates.
a. Comment the code lines where you fill the structures departure and
destination .
details-departureairport ]-name .
destination ]-name .
6. Activate the class. Execute it and analyze the console output. Check that
the output displays data for all attributes.
Continue to quiz
https://learning.sap.com/learning-journeys/acquire-core-abap-skills/working-with-complex-internal-tables_f8c923f3-6f95-4b47-960f-557001f13977 23/23