1. - 1 -
Assigning Values for Complex Data Objects
MOVE also works for copying complex tables and structures. Embedded field strings
and table components are also copied.
Table Type
An internal table is a sequence of lines with the same type.
If you want to define a table type, follow the line type declaration with
the addition OCCURS 0.
You can also write OCCURS <n> instead of OCCURS 0, where <n> is
any integer.
Assigning Values Field by Field
The MOVE-CORRESPONDING <rec1> to <rec2> statements transports
values field by field between field strings <rec1> and <rec2>. This only works
if the components have identical names.
The system looks for all fields in <rec1> whose name also occurs in <rec2>
and transports field <rec1>-<field name> to <rec2>-<field name> in all cases
where it finds a match. All other fields remain unchanged.
Internal Tables
As you have already seen, internal tables (‘arrays’ or ‘matrices’ in other terminology)
are a set of lines with the same type(s).
To define an internal table, simply use the addition OCCURS <n> when
you declare a data object with the desired line type.
The OCCURS addition turns the data object into an internal table with
the same line type as that of the data object. It also determines the
number of lines with which the internal table is created. Unlike the array
2. - 2 -
concept of other programming languages, ABAP/4 can increase the
number of lines in the table dynamically at runtime.
If you do not know how big your internal table will be, set the OCCURS
addition to 0. If you know that your internal table will be smaller than
8KB, specify the number of table lines using the OCCURS parameter.
This ensures that only this amount of memory area is occupied. This is
particularly important when you are working with nested structures. If
the memory area is insufficient, further table lines are allocated.
Working with Internal Tables
In contrast with the array concepts found in other programming languages,
ABAP/4 does NOT use direct access to table entries. Instead, operations on
table entries are carried out using a work area. This is a temporary holding area
that contains the content of the current entry for almost ALL commands dealing
with the table.
This work space may be either a separately defined work area or a
header line that is defined when the table is defined.
ABAP/4 recognizes essentially the following operations on internal tables:
Command Effect
APPEND
Appends the contents of the work area at the end of the
internal table
COLLECT
Inserts the cumulative contents of the work area into the
internal table
INSERT
Inserts the contents of the work area into a particular table
entry
MODIFY
Overwrites a particular table entry with the content of the
work area
DELETE Deletes a specified entry from the internal table
LOOP AT
Places the entries of an internal table in a work area one at a
time
READ
TABLE
Places a single internal table entry in a work area
SORT Sorts the internal table
3. - 3 -
CLEAR Deletes the work area or the internal table
REFRESH Deletes the internal table
FREE Releases space in memory previously occupied by table
Internal Tables With Header Line
You access internal tables record by record. You must use a work area as an interface
for transferring data to and from the table.
When you read data from an internal table, the contents of a specified table record or
line overwrites the contents of the work area. Then, you can reference the contents of
the work area in your program. When you write data to an internal table, you must
first enter the data in the work area from with the system can transfer the data to the
internal table.
To avoid inconsistencies, it is beneficial if the work area has the same
data type as the records or lines of the internal table. A safe procedure
for creating work areas which are compatible with internal tables is to
use the same data type for declaring both the internal table and the work
area.
In ABAP/4, you can distinguish between two kinds of internal tables:
Internal tables WITH header lines
Internal tables WITHOUT header lines
A header line is similar to a work area for a database table. A work area
is used as temporary storage for one entry of a database table. In a
similar way, a header line is used to hold one line of an internal table.
An internal table with header line is a tuple from a work area (header
line) and the bulk of the table itself. Both are addressed using the same
name, the interpretation of the name is context-sensitive. Hence it would
4. - 4 -
stand for the header line in a MOVE statement, but would stand for the
bulk of the table in a SEARCH statement.
When you create an internal table WITH a header line, a work area is
created automatically with the same data type as the rows of the internal
table. The header line and the internal table have the same name. Then,
the system uses this work area implicitly.
In ABAP/4 statements for accessing internal tables, you can specify the
work area to be used. For internal tables with header lines, you can
leave out this specification. Then, the system uses the table work area
implicitly.
Internal Tables Without Header Line
If a table does not have a header line, you MUST provide a work area as a separate
record to hold the content of the current entry for most commands used in processing
tables.
Internal tables WITHOUT a header line do NOT have a table work area
declared which can be used implicitly. To access internal tables
WITHOUT header lines, you must specify a work area explicitly in the
corresponding ABAP/4 statements.
It is a matter of your preference whether you use a header line or a work
area.
Remember that, for internal tables with header lines, the internal table
itself and the table work areas have the same name. If you use the name
in a statement, the system interprets it as the name of the table work area
and NOT as the table itself. In some statement, however, you can enter
square brackets after the name to address the internal table instead of the
table work area as follows: <name> [ ].
Declaring Internal Tables with Header Line
You define an internal table with a header line using the addition WITH HEADER
LINE.
A HEADER line is a work area where the currently active, individual
record in the table resides.
Filling Internal Tables
5. - 5 -
The SELECT command, which you first encountered in Lesson 1, places one entry
from a database table into the table work area of the same name in each loop pass. If
you want to be able to address all table entries during the program runtime, each table
entry must be saved in the internal table.
You use the APPEND statement to fill the internal table: APPEND <wa> TO
<itab> adds the contents of work area <wa> to the end of the internal table
<itab>. If you are working with an internal table with header line, the syntax is
shortened to APPEND <itab>.
Array Fetch
You can fill an internal table with entries from a database table using a single
SELECT statement.
The database system reads the entries in bundles, not singly. Once read,
the bundle is inserted en masse into the internal table. This method has
performance advantages over reading in records individually via a loop.
Since this is not a loop, there is no ENDSELECT.
In the basic form (... INTO TABLE ...) the internal table is filled with the
database entries found and existing entries are overwritten.
In the variant ... APPENDING TABLE... the entries are appended to the
existing entries in the internal table.
Filling Internal Tables with Cumulative Values
You use the COLLECT statement to add the work area or header line to an existing
entry of the same type or (if no such entry exists) to add it to the table as a new entry.
To do this, COLLECT searches in the internal table for an entry, all of
whose alphanumeric fields are identical with those of the entry in the
work area or header line.
If such an entry is found, COLLECT adds all numeric fields from the
work area or header line to the corresponding fields in the table entry.
Otherwise, the COLLECT statement appends the contents of the work
area or header line to the end of the table.
Sorting an Internal Table
You can sort an internal table using the SORT statement.
6. - 6 -
If you do NOT specify any sort criteria, the table is sorted by ALLl
fields except those with data type P, I and F. The fields are sorted
ascending in the order in which they occur.
You can use the additions BY <field name> and ASCENDING or
DESCENDING to limit the sort to certain fields and determine the sort
sequence and hierarchy.
You should use the BY addition to narrow down the sort criteria for
performance reasons.
As of Release 3.0D you can perform a language-specific alphabetical
sort of text fields using the TEXT addition.
Using Loops with an Internal Table
You can process an internal table using the loop statement LOOP AT ... ENDLOOP.
With each loop pass the system places the next table entry in the work area <wa> or
the header line of the internal table <itab>.
You can restrict the entries which are read using the WHERE addition in
the same way as you do when using the SELECT command.
At the beginning of each loop pass, SY-TABIX is set to the value of the
current table entry. When the system leaves the LOOP, SY-TABIX has
the same value as it had before the loop started.
Reading an Entry from an Internal Table
You use the READ TABLE <itab> statement to read a single table entry. When the
entry has been read, it is in the work area <wa> or the header line of <itab>.
If the entry is successfully read, the value of SY-SUBRC is zero,
otherwise it is not equal to zero. SY-TABIX takes the value of the table
entry read.
READ TABLE <itab> INDEX <n>. The nth table entry is read.
If you wish to specify individual fields as a search argument you can use
the following syntax: READ TABLE <itab> WITH KEY <ki> = <v1>
<k2> = <v2>...<kn> = <vn>. In this case, the first entry from <itab> is
read which corresponds with the components specified in <k1>...<kn>.
If the internal table is sorted by the search argument, you can use the
BINARY SEARCH addition. The system then carries out a more
performance-efficient binary search for the specified entry.
The online documentation for the READ statement contains details of
further additions.
7. - 7 -
Changing an Internal Table I
The MODIFY <itab> INDEX <i> [FROM <wa>] statement overwrites table
entry ‘i’ with the contents of the work area or the header line. Entry ‘i’ must
already exist.
The INSERT [<wa> INTO] <itab> INDEX <i> statement creates a new table
entry before entry ‘i’ containing the contents of the work area or the header
line. If the table has ‘i’-1 entries, the contents of the work area or header line
are appended to the internal table.
The DELETE <tab> INDEX <i> statement deletes table entry ‘i’.
Changing an Internal Table II
You can make changes to an internal table from within a LOOP. The changes always
apply to the current table entry (SY-TABIX).
INSERT A new record is inserted before the current entry containing the
contents of the work area or header line.
MODIFY The current entry is overwritten with the contents of the work area
or header line.
DELETE The current record is deleted.
The following additions can be used with the DELETE statement (cf. corresponding
additions with the LOOP statement):
...WHERE <condition>. The DELETE command applies to all table
entries which satisfy the condition.
...FROM <n1> TO <n2>. All entries from <n1> to <n2> inclusive are
deleted. If only the FROM addition is specified, all entries from <n1> to
the end of the table are deleted. If only the TO addition is specified, all
entries from the beginning of the table to <n2> are deleted.
Internal Table Index
The index is the sequential number of a table line. It is NOT a table field, but is
created and managed automatically by the system.
You can use the index with the DELETE, INSERT, MODIFY, LOOP, and READ
statements. In these statements, you can specify the index either as a literal or as a
variable.
8. - 8 -
After processing a particular line of an internal table, the system field SY-TABIX
generally contains the index of that line.
Deleting an Internal Table
If you are using an internal table without a header line, CLEAR <itab> deletes the
body of the table.
If your internal table has a header line, CLEAR <itab> deletes the header line.
If you only want to address the body of an internal table with a header line, you use
the form <itab>[].
Example 1: ITAB_WITHOUT and ITAB_WITH are two internal tables
without and with header lines respectively. The command
ITAB_WITHOUT = ITAB_WITH[] assigns the body of ITAB_WITH
to ITAB_WITHOUT.
Example 2: CLEAR ITAB_WITH[] deletes only the body of the internal
table ITAB_WITH.
REFRESH <itab> deletes the body of the table.
Information about Internal Tables
You can get information about an internal table using the DESCRIBE TABLE
statement:
The LINES addition returns the number of entries currently in the table.
The OCCURS addition returns the number of OCCURS in the table
definition.
You can display information about any data object using the DESCRIBE FIELD
statement (see the online documentation for the DESCRIBE statement).