Internal tables in ABAP can store and process data extracted from database tables. They consist of a body containing multiple rows of data and an optional header line. Various statements like APPEND, INSERT, DELETE, and LOOP AT can be used to manipulate data in internal tables. Control structures like AT NEW and AT END allow processing data based on changes in field values between table rows.
2. Internal tables fulfill the function of arrays.
Stores data extracted from database tables.
Internal tables can be nested.
It consists of Body and Header line.
Body Holds the rows of the internal table.
Header line Has same structure as row of the body
holding a single row only.
Work Area :
To change or output the contents of an internal table, you need a work
area.
When processing an internal table, the system always fills the work area
with the contents of the current table line.
You can then process the work area.
Header line is the default work area for internal tables with header line
2
4. 4
TYPE typ OCCURS n
Defines an internal table without header line.
Example
TYPES: BEGIN OF LINE_TYPE,
NAME(20) TYPE C,
AGE TYPE I,
END OF LINE_TYPE.
DATA: PERSONS TYPE LINE_TYPE OCCURS 20,
PERSONS_WA TYPE LINE_TYPE.
PERSONS_WA-NAME = 'Michael'.
PERSONS_WA-AGE = 25.
APPEND PERSONS_WA TO PERSONS.
5. 5
TYPE typ OCCURS n WITH HEADER LINE
Defines an internal table with header line. Such a table consists of any number of
table lines with the type typ and a header line.
Example
TYPES: BEGIN OF LINE_TYPE,
NAME(20) TYPE C,
AGE TYPE I,
END OF LINE_TYPE.
DATA: PERSONS TYPE LINE_TYPE OCCURS 20
WITH HEADER LINE.
PERSONS-NAME = 'Michael'.
PERSONS-AGE = 25.
APPEND PERSONS.
6. Referencing data dictionary object
With header line
DATA FLIGHT_TAB LIKE SFLIGHT OCCURS 10 WITH HEADER LINE.
Without header line
DATA FLIGHT_TAB LIKE SFLIGHT OCCURS 10.
DATA FLIGHT_TAB LIKE SFLIGHT.
Including structures
You can Include another structure into Internal table.
DATA : BEGIN OF T_TAB1 OCCURS 10,
FIELDS1 LIKE BKPF-BELNR,
FIELDS2 LIKE BSEG-BUZEI,
END OF T_TAB1.
DATA : BEGIN OF T_TAB2 OCCURS 10.
INCLUDE STRUCTURE T_TAB1.
DATA : END OF T_TAB2.
In this example, T_TAB2 will also contain the fields FIELD1 & FIELD2.
6
7. APPEND Statement
APPEND [wa TO | INITIAL LINE TO] itab.
Appends a new line to the end of the internal table itab. If you specify wa
TO, the new line is taken from the contents of the explicitly specified work
area wa.
If you use INITIAL LINE TO, a line filled with the correct value for the
type is added. If the specification before itab is omitted, the new line is
taken from the internal table itab.
After the APPEND, the system field SY-TABIX contains the index of the
newly added table entry.
7
Report - Filling IInntteerrnnaall TTaabblleess
8. INSERT Statement
INSERT [wa INTO | INITIAL LINE INTO] itab [INDEX idx].
Inserts a new line into an internal table. If you specify wa INTO , the new line
is taken from the contents of the explicitly specified work area wa.
When using INITIAL LINE INTO , a line containing the appropriate initial
value for its type is inserted into the table. If you omit the specification
before itab , the new line is taken from the header line of the internal table
itab.
INDEX idx specifies the table index before which the line is inserted into the
table itab .
8
Report - Filling IInntteerrnnaall TTaabblleess
9. Internal tables without header line
MOVE ITAB1 TO ITAB2.
ITAB2 = ITAB1
Internal tables with header line
MOVE ITAB1[ ] TO ITAB2[ ].
ITAB2[ ] = ITAB1[ ].
9
10. SELECT c1 c2 cn|* FROM <dbtable>
INTO TABLE <itab>
WHERE <condition>.
dbtable Name of the database table
c1,c2,cn columns in the database table
itab Internal table that holds the data
condition WHERE clause condition
10
11. LOOP AT Statement
LOOP AT itab.
LOOP AT itab INTO wa.
Processes an internal table (DATA) in a loop which begins with LOOP and
ends with ENDLOOP. Each of the internal table entries is sent to the output
area in turn.
When LOOP AT itab. is used, the header line of the internal table itab is used
as output area.
In the case of LOOP AT itab INTO wa , there is an explicitly specified work
area wa.
If the internal table is empty, all the statements between LOOP and
ENDLOOP are ignored.
In each loop pass, SY-TABIX contains the index of the current table entry.
After leaving a LOOP, SY-TABIX has the same value as it had before.
11
Report - Retrieving IInntteerrnnaall TTaabblleess
12. READ Statement
READ TABLE itab INDEX idx [INTO WA].
READ TABLE itab WITH KEY <k1> = <f1>
<k2> = <f2>
<kn> = <fn> [INTO wa]
[BINARY SEARCH].
Reads an internal table entry. An entry can be chosen using a key or its
index idx.
With "READ TABLE itab.", the header line of the internal table itab is used
as the output area; with "READ TABLE itab INTO wa." the explicitly
specified work area wa is used for this purpose.
For BINARY SEARCH, the internal table itab should be sorted by the keys
k1,k2kn.
12
Report - Retrieving IInntteerrnnaall TTaabblleess
13. MODIFY Statement
MODIFY itab [FROM wa] [INDEX idx].
Changes an entry in the internal table itab .
If you specify FROM wa , the line is replaced by the explicitly specified work
area wa . If the FROM specification is omitted, the line is replaced by the
header line from itab .
With INDEX idx, you can specify the table index of the line to be changed.
The index specification can be omitted in a LOOP on an internal table.
The INDEX specification can also appear before the FROM specification.
Note
The counting of table entries begins with 1.
13
Report - Modifying IInntteerrnnaall TTaabblleess
14. DELETE Statement
DELETE itab.
The current entry of the internal table itab is deleted in a LOOP loop.
Return code value is set to 0.
DELETE itab INDEX idx.
Deletes the idx entry from the internal table itab .
The return code value is set as follows:
SY-SUBRC = 0 The entry was deleted.
SY_SUBRC = 4 The entry does not exist.
14
Report - Deleting IInntteerrnnaall TTaabblleess
15. DELETE Statement
DELETE itab FROM idx1 TO idx2.
Deletes the line area from index idx1 to idx2 from internal table itab. At
least one of the two parameters FROM idx1 or TO idx2 should be
specified.
If parameter FROM is missing, the area from the start of the table to line
idx2 is deleted.
If parameter TO is missing, the area from line idx1 to the end of the table
is deleted.
Start index idx1 must be greater than 0. The return code value is set as
follows:
SY-SUBRC = 0 At least one entry was deleted.
SY_SUBRC = 4 None of the entries were deleted.
15
Report - Deleting IInntteerrnnaall TTaabblleess
16. SORT Statement
SORT itab DESCENDING.
SORT itab ASCENDING.
SORT itab BY f1 f2 ... fi.
Sorts the entries of the internal table itab in ascending order. The default key
is used as the sort key for internal tables.
Sorts itab by the sub-fields f1, f2 , ..., fi which form the sort key. These fields
can be any type (even number fields or tables).
Unless you specify otherwise, the sort is in ascending order. You can also
use additions 1 and 2 before BY if you want all sub-fields to apply.
To change the sort sequence for each individual field, specify
DESCENDING or ASCENDING after each of the sub-fields f1 , f2 , ..., fi .
16
Report - Sorting IInntteerrnnaall TTaabblleess
17. CLEAR ITAB.
If ITAB is an internal table without a header line, the entire table is
deleted together with all its entries.
If, however, ITAB is an internal table with a header line, only the
subfields in the table header entry are reset to their initial values.
To delete the entire internal table together with all its entries, you can
use CLEAR ITAB[ ] or REFRESH ITAB.
NOTE:
CLEAR f.
Clears the field contents
17
18. DESCRIBE Statement
DESCRIBE TABLE itab.
Returns the attributes of the internal table itab. You must use at least one of
the additions listed below.
Additions :
1. ... LINES lin
Places the number of filled lines of the table t in the field lin.
2. ... OCCURS n
Transfers the size of the OCCURS parameter from the table
definition to the variable n.
18
Report - Retrieving Internal TTaabbllee aattttrriibbuutteess
19. All these structures begin with AT and end with ENDAT. The
sequence of statements which lies between them is then executed if a
control break occurs.
AT NEW f. / AT END OF f.
f is a sub-field of an internal table processed with LOOP.
The sequence of statements which follow it is executed if the sub-field
f or a sub-field in the current LOOP line defined (on the left)
before f has a different value than in the preceding (AT NEW) or
subsequent (AT END OF) table line.
AT FIRST. / AT LAST.
Executes the appropriate sequence of statements once during the
first (AT FIRST) or last (AT LAST) loop pass.
19
Report - Control Break WWiitthh IInntteerrnnaall TTaabblleess
20. AT FIRST
Statements are executed before any records are processed while
looping at Internal table.
Example :
LOOP AT itab.
AT FIRST.
WRITE : SY-ULINE.
ENDAT.
ENDLOOP.
20
Report - Control Break WWiitthh IInntteerrnnaall TTaabblleess
21. AT LAST
Statements are executed after all records are processed while looping
at Internal table.
Example :
LOOP AT itab.
AT LAST.
WRITE : SY-ULINE.
ENDAT.
ENDLOOP.
21
Report - Control Break WWiitthh IInntteerrnnaall TTaabblleess
22. AT NEW <Field Name>
Statements are executed at the beginning of a group of records
containing the same value for <Field Name>..
Example :
LOOP AT itab.
AT NEW I_LIFNR.
WRITE : SY-ULINE
ENDAT.
ENDLOOP.
22
Report - Control Break WWiitthh IInntteerrnnaall TTaabblleess
23. AT END OF <Field Name>
Statements are executed at the end of a group of records containing the
same value for <Field Name>.
Example :
LOOP AT itab.
AT END OF I_LIFNR.
WRITE : SY-ULINE.
ENDAT.
ENDLOOP.
Note :
AT NEW and AT END OF make sense only for a
sorted table.
23
Report - Control Break WWiitthh IInntteerrnnaall TTaabblleess
24. SUM Statement
SUM.
SUM calculates the control totals of all fields of type I , F and P and places them in
the LOOP output area (header line of the internal table or an explicitly specified
work area).
You can use the SUM statement both at the end and the beginning of a control
group
Example:
LOOP AT itab.
AT LAST.
SUM.
WRITE : itab-fld1.
ENDAT.
ENDLOOP.
Prints the sum of values of fld1 in all rows of itab.
Fld1 should be a numeric type field
24
RReeppoorrtt -- SSuummmmaattiioonn
25. COLLECT Statement
COLLECT [wa INTO] itab.
COLLECT is used to summate entries in an internal table.
COLLECT = APPEND, if no entries with the same key exists
= Adds the numeric values to their corresponding field values, if an entry
with same key exists
Used to create summarized tables.
25
RReeppoorrtt -- SSuummmmaattiioonn