Register Login

Internal Tables Processing

Updated May 18, 2018

Identifying Table Lines

Internal Table Index:

The sequential number of a table line. Created and managed automatically by the system. Can be used with the DELETE, INSERT, MODIFY, LOOP, and READ TABLE statements, specified either as literal or as variable. After processing a particular line of an internal table, the SY-TABIX generally contains the index of that line.

Internal Table Key:

Standard - by definition, the key fields of an internal table are those fields which are not numeric (type F, I, and P) and are not internal tables. These key fields form the standard key of an internal table. To obtain the standard key of an internal tables with nested structures (table lines which contain field strings as components), the system breaks down the sub-structures to the level of elementary fields;

Self-Defined - can be specified when reading lines from an internal table using the READ TABLE.

Filling Table

Appending Lines:

APPEND [{<wa>|INITIAL LINE} TO] <itab>.

Appends a new line to table. For tables with a header line the source area can be omitted. After statement, the SY-TABIX contains the index of the appended line.

APPEND LINES OF <itab1> [FROM <idx1>] [TO <idx2>] TO <itab2>.

Appends lines from one table to another table. Default appends entire table. After statement, the SY-TABIX contains the index of the last appended line. About 3 to 4 times faster than appending lines line by line in a loop.

APPEND [<wa> TO] <itab> SORTED BY <f>.

Inserts the new line so that the internal table <itab> is sorted in descending order by the field <f>. If you use the SORTED BY option, the table can contain only the number of lines specified in the OCCURS parameter. If you add more lines than specified, the last line is discarded. This is useful for creating ranked lists of limited length (e.g. "Top Ten"). For ranked lists containing more than 100 entries, use instead the SORT for performance reasons.

Read here: How to Use a Table control with an Internal Table?

Inserting Lines:

INSERT [{<wa>|INITIAL LINE} INTO] <itab> [INDEX <idx>].

Inserts a new line into table. For tables with a header line the source area can be omitted. With INDEX option, the new line is inserted before the line which has the index <idx>. After the insertion, the new entry has the index <idx> and the index of the following lines is incremented by 1.

If the table consists of <idx> - 1 entries, the system appends the new entry after the last existing table line. If the table has less than <idx> - 1 entries, the system cannot insert the entry: SY-SUBRC = 4. If success, SY-SUBRC = 0.

Use INSERT without the INDEX option is allowed only within a LOOP - ENDLOOP loop by inserting the new entry before the current line (i.e. the line which has the index returned by SY-TABIX).

INSERT LINES OF <itab1> [FROM <idx1>] [TO <idx2>] INTO <itab2> [INDEX <idx>].

Inserts lines from one table into another table. Default inserts entire table. Can be up to 20 times faster than inserting lines line by line in a loop.

Collecting Lines:

COLLECT [<wa> INTO] <itab>.

If a table entry with the same standard key does not exists (all non-numeric fields), the COLLECT has the same effect as the APPEND. If exists, the COLLECT adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry. SY-TABIX contains the index of the processed line.

Reading Table

Reading Single Lines:

READ TABLE <itab> [INTO <wa>] INDEX <idx>.

Reads the single line with index <idx> into <wa> (or into header line, if omitted). This is the fastest method.

<idx> <= 0 : runtime error;
<idx> > table size : SY-SUBRC = 4;
<idx> found : SY-SUBRC = 0, SY-TABIX = <idx>.

READ TABLE <itab> [INTO <wa>] [BINARY SEARCH].

Reads the first line with a particular standard key, which should be placed in the table work area (works only for internal tables with header line).

entry found : SY-SUBRC = 0, SY-TABIX = <idx>;
entry not found : SY-SUBRC <> 0;.

READ TABLE <itab> [INTO <wa>] WITH KEY <key>[BINARY SEARCH].

Reads the first line with <key>.

entry found : SY-SUBRC = 0, SY-TABIX = <idx>;
entry not found : SY-SUBRC <> 0;.

Key definition for internal tables:

....WITH KEY <k1> = <f1> ... <kn> = <fn> ...

The self-defined key consists of the table components <k1>...<kn>. If the data type of <fi> is not compatible to the data type of <ki>, then <fi> is converted to the type of <ki>. You can specify an offset and length for any component you use in the key. You can set key fields at runtime by replacing <ki> with (<ni>). The key field is the contents of the field <ni>. If <ni> is blank at runtime, the system ignores this key field. If <ni> contains an invalid component name, a runtime error occurs.

....WITH KEY = <value> ...

Defines entire line as key (will be converted if necessary).

....WITH KEY <value> ...

The system compares the (left-justified) beginning of a line with <value>. <value> cannot contain an internal table or a structure containing an internal table. In contrast to the above two options, the comparison is processed using the data type of <value>.

Read here: How to Copy a Portion of an Internal Table to another?

Additions:

BINARY SEARCH - performs a binary search instead of the standard sequential search (much faster). Table must be sorted.

  • SY-SUBRC = 0 : entry found, SY-TABIX = index of the found entry;
  • SY-SUBRC = 4 : entry not found, SY-TABIX = index of the next largest entry, output area remains unchanged;
  • SY-SUBRC = 8 : entry not found, the key is greater than last table entry, SY-TABIX = (number of all entries + 1), output area remains unchanged.
    • TRANSPORTING {<f1> ... <fn>|NO FIELDS} - only transports selected fields to the target area.
    • COMPARING {<f1> ... <fn>|ALL FIELDS} - reads single line with key or index into the target area. After reading the line, the components specified in fields list are compared to the corresponding components of the target area:
  • SY-SUBRC = 0 : line is read and the contents of the compared fields are the same;
  • SY-SUBRC = 2 : line is read, but the contents of the compared fields are not the same;
  • SY-SUBRC = 4 : no line was read.

LOOP Processing:

LOOP AT <itab> [INTO <wa>] [FROM <n1>] [TO <n2>] [WHERE <condition>].
...
ENDLOOP.

<itab> is read line by line into <wa> or into the table work area. For each line read, processed the statement block between LOOP and ENDLOOP. Within the statement block, SY-TABIX contains
the index of the current line. The loop ends as soon as all lines of the table have been processed. After the ENDLOOP statement:

SY-SUBRC = 0 : at least one line was read;
SY-SUBRC = 4 : otherwise it is set to 4.

Restriction the number of lines to be processed:

FROM <n1> - the index of the first line to be read.
TO <n2> - the index of the last line to be read.
WHERE <condition> - the first operand of the logical expression must be a component of <itab>. Cannot be used together with the control keyword AT inside the loop.

Note. The FROM and TO options restrict the number of lines which the system has to read. The WHERE option only prevents unnecessary filling of the work area. With the WHERE option, the system must read all lines. To improve performance, you should use the FROM and TO options as much as possible. It can be also beneficial to leave the loop with the EXIT statement instead of using the WHERE option.

Calculating Totals:

SUM.

Can be used only in LOOP - ENDLOOP block. A component of an internal table line may not be another table. In an AT - ENDAT block, calculates totals for the numeric fields (I, F, P) of all lines in the current line group and writes them to the corresponding fields in the work area. Outside an AT - ENDAT block - do it for lines of the internal table in each loop pass. Therefore, SUM should be used only in AT - ENDAT blocks.

Using Control Levels for Groups of Lines (AT ... ENDAT Events):

AT <line>.
...
ENDAT.

The <line> condition:

<line> Meaning
FIRST First line of the internal table
LAST Last line of the internal table
NEW <f> Beginning of a group of lines with the same contents in the field <f> and in the fields left of <f>
END OF <f>End of a group of lines with the same contents in the field <f> and in the fields left of <f>

<f> can be:

table field name;
table field name with Offset/Length specification;
field symbol. If it does not point to a valid component, a runtime error occurs;
(<name>) instead of <f> (See also). The <name> contains the name of the field <f>. If <name> is empty at runtime, the system ignores the control break criterion. If it contains an invalid component name, a runtime error occurs.

Cannot be used together with the WHERE restriction of a LOOP.

Read here: ABAP Performance Tuning with Select statements, Internal table usage, Database Index

Immediately after AT:

All default key fields [on the right of <f>] are filled with '*';
All other fields [on the right of <f>] are cleared;
If SUM used then appropriate control totals are inserted in numeric fields;
After AT the old contents of LOOP output area are restored.

Modifying Table

MODIFY <itab> [FROM <wa>] [INDEX <idx>] [TRANSPORTING <f1> ... <fn> [WHERE <condition>]].

Modifies the line in the table. For tables with a header line the source area can be omitted. With INDEX option, will be modified the line which has the index <idx>. In this case: SY-SUBRC = 4 if no line with <idx> exists; SY-SUBRC = 0 - success.

Use MODIFY without the INDEX option is allowed only within a LOOP - ENDLOOP loop by modifying the current line (i.e. the line which has the index returned by SY-TABIX).

With the TRANSPORTING option, the system transports only the components <f1> ... <fn> from the work area into the internal table. Strongly recommended suppress unnecessary transports of fields which are internal tables itself. You can specify a component dynamically by writing (<name>). In this case, the system reads the name of the component from field <name> at runtime. An invalid component name leads to a runtime error.

WHERE <condition> with the TRANSPORTING option determines all the lines of the table into which the components <f1> ... <fn> are to be transported. First operand should be a component of the internal table's line structure. WHERE cannot be used together with the INDEX option.

WRITE <f>[+<o1>][(<l1>)] TO <itab>[+<o2>][(<l2>)] INDEX <idx>.

Variant of WRITE TO. Overwrites the section of the line of the <itab> that has index <idx>. Does not affect header line. Does not recognize the structure of table lines. Return codes: SY-SUBRC = 4 - no line with <idx> exists; SY-SUBRC = 0 - success. If <idx> <= 0 - runtime error.

Deleting lines:

DELETE <itab>.

Deletes current line in LOOP - ENDLOOP (that is line which have index SY-TABIX). After the first line has been deleted, the current line and its assignment to the contents of SY-TABIX can be undefined. To process further lines within this loop, use only statements with the INDEX option.

DELETE <itab> INDEX <idx>.

Deletes line with the index <idx>. Return codes: SY-SUBRC = 4 - no line with <idx> exists; SY-SUBRC = 0 - success.

DELETE ADJACENT DUPLICATES FROM <itab> [COMPARING {<f1> ... <f1>|ALL FIELDS}].

Deletes adjacent duplicates entries. Return codes: SY-SUBRC = 0 - at least one entry was deleted; SY-SUBRC = 4 - otherwise. Without the COMPARING option, the contents of the standard key fields must be the same.

DELETE <itab> [FROM <n1>] [TO <n2>] [WHERE <condition>].

Deletes selected entries. At least one of three criteria should be used. First operand in WHERE should be a component of the internal table's line structure. Return codes: SY-SUBRC = 0 - at least one entry was deleted; SY-SUBRC = 4 - otherwise.

Clearing Table

You can check whether an internal table is empty using logical expression:

... <itab> IS INITIAL ...

REFRESH <itab>.

Empties internal table.

CLEAR <itab>[].

Empties internal table without clearing the table work area.

CLEAR <itab>.

Empties internal table that has no table work area (header line).

Clears the table work area if the table has it.

FREE <itab>.

Empties internal table and releases reserved memory without clearing the table work area.

Other Operations With Internal Table

SORT <itab> [<order>] [AS TEXT]

[BY <f1> [<order>] [AS TEXT] ... <fn> [<order>] [AS TEXT]].

<order>: ASCENDING (default) or DESCENDING. Sorts by standard or self -defined (if BY defined) key. The system uses the options you specify before BY as a default for all fields specified behind BY. The options that you specify after individual fields overwrite for these fields the options specified before BY.

<f1> ... <fn> can be:

of any type, including type P, I, and F fields, or tables;
up to 250;
(<name>) instead of <f> (See also). The <name> contains the name of the field <f>. If <name> is empty at runtime, the system ignores it. If it contains an invalid component name, a runtime error occurs.;
table field name with Offset/Length specification;

Without the option AS TEXT, the system sorts character fields binarily and according to their platform-dependent internal coding. With the option AS TEXT, the system sorts character fields alphabetically according to the current text environment. By default, the text environment is set in the user's master record. As an exception, you can set the text environment with the SET LOCALE

LANGUAGE. The option AS TEXT frees you from converting a character field into a sortable format before sorting. Such a conversion is only necessary, if you want to

sort an internal table alphabetically first and search it binarily afterwards. The order of an internal table after alphabetical sorting differs from the order after binary sorting.
sort an internal table several times with alphabetical keys. In this case, the performance is better, because the conversion is processed only once;
create alphabetical indexes for database tables in your program.

AS TEXT before BY influences only the character fields in the sort key. If you specify AS TEXT after a field name, this field must be of type C. Sorting is not stable. This means that the old sequence of lines with the same sort key may not necessarily be retained. If there is not enough space for sorting in the main memory, the system writes data into a temporary external file. The name of this file is defined in the SAP profile parameter DIR_SORTTMP.

Searching Internal Tables for Character String:

SEARCH <itab> FOR <str> [<options>].

Options and patterns like searching string for string. No type conversion is performed also (the entire table line is treated as character string). <itab> is always table, not header line, if even the table has header line. In addition to SY-SUBRC and SY-FDPOS, SY-TABIX is set to found line index.

Logical Expressions:

... <itab1> {EQ|=|NE|<>|><|GE|>=|LE|<=|GT|>|LT|<} <itab2> ...

The first criterion for comparing internal tables is the number of lines they contain. The more lines an internal table contains, the larger it is. If two internal tables contain the same number of lines, they are compared line by line, component by component. If components of the table lines are themselves internal tables, they are compared recursively. If you use operators other than the equality operator, the system stops the comparison as soon as it finds a pair of components which are not equal and returns the result. In the case of internal tables with a header line, you can use square brackets ([]) after the table name to distinguish the table body from the table work area.

Assignment Operator:

MOVE <itab1> TO <itab2>.

[COMPUTE] <itab2> = <itab1>.

Copies entire contens of internal table that has not header line.

MOVE <itab1>[] TO <itab2>[].

[COMPUTE] <itab2>[] = <itab1>[].

Copies entire contens of internal table with header line.

Read more here SAP ABAP Tutorial


 Download attached file.

You must be Logged in to download this file

×