INTERNAL TABLE
INTERNAL TABLE
Contributed By
Uzma Sheikh
Under the Guidance of TagSkills.
Internal table
WorkArea
What is WorkArea?
Work area are single rows of data, they should have the same
format as any of internal tables.
It is used to process the data in an internal table one line at a time.
Let us now create a Internal table itab using the TYPE statement.
column1 type I,
column2 type I,
end of line.
Example:
TYPES : begin of line,
empno type I,
empname(20) type c ,
end of line.
An internal table itab is created with the structure of line.Besides declaring the structure of an
internal table, the OCCURS clause also defines how many table entries are maintained in
main storage. Extra records are written out to paging area and can effect performance
Syntax-
Data <f> <type> [with header line].
Example-
DATA itab TYPE line OCCURS 10 with header line.
Here an internal table itab is created of the type line with a header line. Please note “with
header line” is optional.
Let us now create an internal table with a structure of our own. Here the table is created with
an Header line, by default.
Syntax –
Data : Begin of <f> occurs <n>,
<component declaration>,
.................................,
End of <f>.
Example –
Data : Begin of itab occurs 10,
column1 type I,
column2(4) type C,
End of itab.
Syntax –
APPEND [<wa> TO / INITIAL LINE TO] <itable>.
Here work area <wa> or the Initial Line is appended to the internal table <itable>.
The system variable SY-TABIX contains the index of the appended line.
Example:
Data: Begin of itab occurs 10,
col1 type C,
col2 type I,
end of itab.
Initial lines adds a line initialized with the correct value for its type to the table. Here , col1 is
an character and col2 is a integer. Then APPEND initial line, adds a line initialized with
respect to the data type of the columns, i.e. space for col1 and 0 for col2.
Syntax-
COLLECT [<wa> INTO] <itable>.
Incase of tables with Header line, INTO option is omitted. Suppose there is already an entry
having a key same as the one you are trying to append, then a new line is not added to the
table, but the numeric fields of both the entries are added and only one entry corresponding to
the key is present. Value of SY-TABIX is changed to the row of the original entry. Else
COLLECT acts similar to APPEND and SY-TABIX contains the index of the processed line.
INSERT statement adds a line/work area to the internal table. You can specify the position at
which the new line is to be added by using the INDEX clause with the INSERT statement.
Here, the work area <wa> or INITIAL LINE is inserted into internal table <itable> at index
<idx>.
OR
<itab1> = <itab2>.
These copy the contents of ITAB1 to ITAB2. Incase of internal tables with header line we
have to use [] inorder to distinguish from work area. So, to copy contents of internal tables
with header line the syntax becomes,
itab1[] = itab2[].
Syntax
LOOP AT <itable> [INTO <wa>]
...................................
ENDLOOP.
Here when you say LOOP AT ITABLE, then the internal table ITABLE is read line by line.
You can access the values of the columns for that line during any part of the LOOP-
ENDLOOP structure. The value of the SY-SUBRC is set to 0, even if only one record is read.
2. Using READ
The other method of reading the internal table is by using the READ statement.
This statement reads the current line or line as specified by index <idx>. The value of SY-
TABIX is the index of the line read. If an entry with the specified index is found, then SY-
SUBRC is set to 0. If the specified index is less than 0, then run-time error occurs. If the
specified index exceeds table size then SY-SUBRC is set to 4.
Syntax
DELETE <ITABLE>.
This statement works only within a loop. It deletes the current line. You can delete the lines
in a loop conditionally by adding the WHERE clause.
This is used to delete a line from internal table at any know index.
Syntax
DELETE <ITABLE> INDEX <IDX>.
The line with the index <IDX> is deleted. The index of the following line is decremented by
1.
Output:
Disclaimer –
Copyright TagSkills. The copyright in this work is vested in TagSkills.
Please note and abide by copyright laws. This presentation is for educational purpose only, all
logos, photos, and information ,etc used in this presentation is the property of TagSkills. SAP
is a registerd trademark of SAP AG in Germany and many other countries. We are NOT
ASSOCIATED with SAP.