Professional Documents
Culture Documents
An Internal table is a temporary table gets created in the memory of application server during program execution
and gets destroyed once the program ends. It is used to hold data temporarily or manipulate the data. It contains
one or more rows with same structure.
An internal table can be defined using the keyword TABLE OF in the DATA statement. Internal table can be
defined by the following ways.
TYPES: BEGIN OF ty_student,
id(5)
TYPE n,
name(10) TYPE c,
END OF ty_student.
TYPE n,
name(10) TYPE c,
END OF ty_student.
DATA: gwa_student TYPE ty_student.
*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
"Referring to local data type
DATA: it TYPE TABLE OF ty_student.
gwa_student-id
= 1.
gwa_student-name
= 'JOHN'.
= 2.
gwa_student-name
= 'JIM'.
= 3.
gwa_student-name
= 'JACK'.
NAME
JOHN
JIM
JACK
Usually internal tables are used to hold data from database tables temporarily for displaying on the screen or
further processing. To fill the internal table with database values, use SELECT statement to read the records
from the database one by one, place it in the work area and then APPEND the values in the work area to internal
table.
DATA: gwa_employee TYPE zemployee,
gt_employee
After ENDSELECT the internal table GT_EMPLOYEE contains all the records that are present in table
ZEMPLOYEE.
Using INTO TABLE addition to SELECT statement we can also read multiple records directly into the internal
table directly. No work area used in this case. This select statement will not work in loop, so no ENDSELECT is
required.
SELECT * FROM zemployee INTO TABLE gt_employee.
The first INSERT statement without INDEX addition will simply add the record to the end of the internal table. But
if we want to insert the line to specific location i.e. if we want to insert it as second record then we need to specify
2 as the index in the INSERT statement.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
id(5)
TYPE n,
name(10) TYPE c,
END OF ty_student.
*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it TYPE TABLE OF ty_student.
gwa_student-id
= 1.
gwa_student-name
= 'JOHN'.
= 2.
gwa_student-name
= 'JIM'.
= 3.
gwa_student-name
= 'JACK'.
= 4.
gwa_student-name
= 'RAM'.
We can also insert multiple lines to an internal table with a single INSERT statement i.e. we can insert the lines of
one internal table to another internal table.
Syntax to insert multiple lines to internal table
INSERT LINES OF <itab1> [FROM <index 1>] [TO <index 2>] INTO TABLE <itab2>.
OR
INSERT LINES OF <itab1> [FROM <index 1>] [TO <index 2>] INTO
<itab2> INDEX <index>.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
id(5)
TYPE n,
name(10) TYPE c,
END OF ty_student.
*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
= 1.
gwa_student-name
= 'JOHN'.
= 2.
gwa_student-name
= 'JIM'.
= 3.
gwa_student-name
= 'JACK'.
= 4.
gwa_student-name
= 'ROB'.
= 1.
gwa_student-name
= 'RAM'.
= 4.
gwa_student-name
= 'RAJ'.
We can also use the above MODIFY statement without INDEX addition inside LOOP. Inside LOOP if we do not
specify the INDEX, then the current loop line will be modified.
We can use the WHERE clause to change single or multiple lines. All the lines that meet the logical condition will
be processed. If at least one line is changed, the system sets SY-SUBRC to 0, otherwise to 4.
MODIFY <internal table> FROM <work area>
TRANSPORTING <f1> <f2> ... WHERE <condition>.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
id(5)
TYPE n,
name(10)
TYPE c,
place(10) TYPE c,
age
TYPE i,
END OF ty_student.
*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it
gwa_student-id
= 1.
gwa_student-name
= 'JOHN'.
gwa_student-place
= 'London'.
gwa_student-age
= 20.
= 2.
gwa_student-name
= 'JIM'.
gwa_student-place
= 'New York'.
gwa_student-age
= 21.
gwa_student-id
= 3.
gwa_student-name
= 'JACK'.
gwa_student-place
= 'Bangalore'.
gwa_student-age
= 20.
= 4.
gwa_student-name
= 'ROB'.
gwa_student-place
= 'Bangalore'.
gwa_student-age
= 22.
= 4.
gwa_student-name
= 'ROB'.
gwa_student-place
= 'Mumbai'.
gwa_student-age
= 25.
= 9.
gwa_student-name
= 'TOM'.
gwa_student-place
= 'Bangalore'.
gwa_student-age
= 30.
= 'Mumbai'.
We can also use the above DELETE statement without INDEX addition inside LOOP. Inside LOOP if we do not
specify the INDEX, then the current loop line will be deleted.
We can use the WHERE clause to delete single or multiple lines. All the lines that meet the logical condition will
be deleted. If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.
DELETE <internal table> [FROM <n1>] [TO <n2>] [WHERE <condition>].
With WHERE clause we can also specify the lines between certain indices that we want to delete by specifying
indexes in FROM and TO additions.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
id(5)
TYPE n,
name(10)
TYPE c,
place(10) TYPE c,
age
TYPE i,
END OF ty_student.
*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it
gwa_student-id
= 1.
gwa_student-name
= 'JOHN'.
gwa_student-place
= 'London'.
gwa_student-age
= 20.
= 2.
gwa_student-name
= 'JIM'.
gwa_student-place
= 'New York'.
gwa_student-age
= 21.
= 3.
gwa_student-name
= 'JACK'.
gwa_student-place
= 'Bangalore'.
gwa_student-age
= 20.
= 4.
gwa_student-name
= 'ROB'.
gwa_student-place
= 'Bangalore'.
gwa_student-age
= 22.
SORT is the statement to sort an ABAP internal table. We can specify the direction of the sort using the additions
ASCENDING and DESCENDING. The default is ascending.
SORT <internal table> [ASCENDING|DESCENDING]
We can also delete the adjacent duplicates from an internal table by using the following statement.
DELETE ADJACENT DUPLICATE ENTRIES FROM <internal table>
[COMPARING <f1> <f2> ... |ALL FIELDS].
COMPARING ALL FIELDS is the default. If we do not specify the COMPARING addition, then the system
compares all the fields of both the lines. If we specify fields in the COMPARING clause, then the system
compares only the fields specified after COMPARING of both the lines. If at least one line is deleted, the system
sets SY-SUBRC to 0, otherwise to 4.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
id(5)
TYPE n,
name(10)
TYPE c,
place(10) TYPE c,
age
TYPE i,
END OF ty_student.
*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it
= 1.
gwa_student-name
= 'JOHN'.
gwa_student-place
= 'London'.
gwa_student-age
= 20.
= 2.
gwa_student-name
= 'JIM'.
gwa_student-place
= 'New York'.
gwa_student-age
= 21.
= 3.
gwa_student-name
= 'JACK'.
gwa_student-place
= 'Bangalore'.
gwa_student-age
= 20.
= 4.
gwa_student-name
= 'ROB'.
gwa_student-place
= 'Bangalore'.
gwa_student-age
= 22.
= 2.
gwa_student-name
= 'JIM'.
gwa_student-place
= 'New York'.
gwa_student-age
= 21.
ENDLOOP.
WRITE:/ 'Values in IT after deleting duplicates' COLOR 4.
*Delete duplicates
SORT it.
DELETE ADJACENT DUPLICATES FROM it.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
gwa_student-age.
ENDLOOP.
WRITE:/ 'Values in IT after deleting duplicates comparing place' COLOR 4.
*Delete duplicates comparing only place
SORT it BY place.
DELETE ADJACENT DUPLICATES FROM it COMPARING place.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
gwa_student-age.
ENDLOOP.
Output
We can exit out of LOOP/ENDLOOP processing using EXIT, CONTINUE and CHECK similar to all other LOOPS.
We can also initialize the internal table using FREE, CLEAR and REFRESH statements. CLEAR and REFRESH
just initializes the internal table where as FREE initializes the internal table and releases the memory space.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
id(5)
TYPE n,
name(10)
TYPE c,
place(10) TYPE c,
age
TYPE i,
END OF ty_student.
*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it
gwa_student-id
= 1.
gwa_student-name
= 'JOHN'.
gwa_student-place
= 'London'.
gwa_student-age
= 20.
= 2.
gwa_student-name
= 'JIM'.
gwa_student-place
= 'New York'.
gwa_student-age
= 21.
AT FIRST / ENDAT
AT LAST / ENDAT
AT NEW / ENDAT
AT END OF / ENDAT
SUM
ON CHANGE OF / ENDON
The code between AT NEW and ENDAT is executed only during the first loop pass. So it is used to write the
headers or some other initialization processing.
The code between AT LAST and ENDAT is executed only during the last loop pass. So it is used to write the
totals or some report footers.
** *Data Declaration *
*
DATA: gwa_spfli TYPE spfli.
DATA: gt_spfli
AT FIRST.
ENDAT.
WRITE:/ gwa_spfli-carrid,14 gwa_spfli-connid,
29 gwa_spfli-cityfrom,44 gwa_spfli-cityto.
AT LAST.
ULINE.
WRITE:/ 'End of Loop'.
ENDAT.
ENDLOOP.
Output
Between AT FIRST and ENDAT the work area will not contain any data. The default key fields are filled with
asterisks(*) and the numeric fields are filled with zeros. The ENDAT restores the contents to the values they had
prior to entering the AT FIRST. Changes to the work area within AT FIRST and ENDAT are lost. The same
applies for AT LAST and ENDAT.
** *Data Declaration *
*
DATA: gwa_spfli TYPE spfli.
DATA: gt_spfli
Output
AT NEW and ENDAT is used to detect a change in the value of the field between the loop passes. The field that
is specified in AT NEW is called control level. The code between AT NEW and ENDAT will be executed during
the first loop pass and every time the value of the control level changes or any other field left to the control level
changes. Between AT NEW and ENDAT all the fields in the work area that are right to the control level are filled
with zeros and asterisks.
Similarly The code between AT END OF and ENDAT will be executed during the last loop pass and every time
the value of the control level changes or any other field left to the control level changes.
** *Data Declaration *
*
DATA: gwa_spfli TYPE spfli.
DATA: gt_spfli
WRITE:/14 gwa_spfli-connid,
29 gwa_spfli-cityfrom,44 gwa_spfli-cityto,
58 gwa_spfli-distance.
AT END OF carrid. ULINE. WRITE:/ End of Airline : , gwa_spfli-carrid. ULINE. ENDAT.
AT LAST.
WRITE:/ 'End of Loop'.
ENDAT.
ENDLOOP.
Output
In AT FIRST and AT LAST event blocks the numeric values in the work area contains zeros. SUM
statement calculates the totals of numeric fields and places the totals in the corresponding fields of work area.
In AT NEW and AT END OF event blocks SUM statement finds all the rows within the control level and calculates
the totals of numeric fields that are right to the control level and places the totals in the corresponding fields of
work area.
** *Data Declaration *
*
DATA: gwa_spfli TYPE spfli.
DATA: gt_spfli
SUM.
WRITE:/ gwa_spfli-carrid,58 gwa_spfli-distance.
ULINE.
ENDAT.
AT LAST.
SUM.
WRITE:/ 'Total',58 gwa_spfli-distance.
Output
** *Data Declaration *
*
DATA: gwa_spfli TYPE spfli.
DATA: gt_spfli
ENDLOOP.
Output
Below table summarizes the differences between AT NEW and ON CHANGE OF statements.
AT NEW
ON CHANGE OF
statement.
SELECT, DO etc..
used.
by OR can be used.
control
and zeros.
values.
level
contains
original