You are on page 1of 43

Internal Tables

What are Internal Tables?


• 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
Types of an internal Table

Standard Table

 Contains linear index.

 Accessed using index or Sorted Table


key.
 Always sorted by a key,
 Key can be Unique or Hashed Table
 Also contains linear index
non unique.
 Always sorted by a key
 Accessed using key or index
 Search is according to  Does not contains a
the linear relationship.  Key can be unique or non
linear index
unique.
 Accessed only through
 Search is done using the
key.
binary search algorithm.
 Key is always Unique.
 Search is done using
the hash algorithm.

3
Internal Tables as Data Types

• The data type of an internal table is fully specified by its line type, key, and table type.

• Line Type: The line type of an internal table can be any data type. The data type of an
internal table is normally a structure.

• Table Type: Standard, Sorted or Hashed.

• Key: The key identifies table rows.


– It can be Unique or Non unique.
– There are two kinds of key for internal tables
• Standard key (default key): contains all character fields

• User defined key (created by user) : can be character or numeric fields.

4
Creating an Internal Table with Header Line
REPORT Y170DM38.
TABLES: EMPLOYEE. The TYPES statement defines
TYPES: BEGIN OF EMP, the structure and data type for
ID LIKE EMPLOYEE-ID, the internal table.
NAME1 LIKE EMPLOYEE-NAME1, The DATA statement with an
INITIAL SIZE creates the
COUNTRY LIKE actual internal table capable
EMPLOYEE-COUNTRY, of storing data. Because of
the WITH HEADER LINE
END OF EMP. addition, this internal table is
DATA: EMPTAB TYPE STANDARD TABLE created with a header line.
ID NAME1 COUNTRY
OF EMP INITIAL SIZE 10 WITH
Header Line
HEADER LINE.

SELECT * FROM EMPLOYEE.


MOVE-CORRESPONDING EMPLOYEE TO
EMPTAB.
APPEND EMPTAB.
ENDSELECT.

5
Loading an Internal Table with a Header Line
REPORT Y170DM42.
With both versions of the
TABLES: EMPLOYEE.
APPEND statement,
TYPES: BEGIN OF EMP, memory space for ten
COUNTRY LIKE EMPLOYEE-COUNTRY, records is allocated when
ID LIKE EMPLOYEE-ID, the first record is written to
the internal table.
SALARY LIKE EMPLOYEE-SALARY,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE
Example 1

OF EMP INITIAL SIZE 10 WITH HEADER LINE. More than ten entries can be
saved in the internal table.
SELECT * FROM EMPLOYEE. Example 2
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB. A maximum of ten entries
can be saved in the
OR MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
internal table. Any
APPEND EMPTAB SORTED BY SALARY. entries that exceed the
ENDSELECT. top ten will
be deleted.

6
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
Internal Table with Header Line

EMPLOYEE

A
COUNTRY ID FORMA NAME1 SORTL . . .

ID NAME1 COUNTRY

B Header Line

8
Internal Table with Header Line contd.

1 EMPLOYEE

COUNTRY ID FORMA NAME1 SORTL . . .


USA 00000001 Company Baker Distributors BAKER . . .

ID NAME1 COUNTRY
Header Line

9
Internal Table with Header Line contd.

1 EMPLOYEE

COUNTRY ID FORMA NAME1 SORTL . . .


USA 00000001 Company Baker Distributors BAKER . . .

2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line

10
Internal Table with Header Line contd.

1 EMPLOYEE

COUNTRY ID FORMA NAME1 SORTL . . .

USA 00000001 Company Baker Distributors BAKER . . .

2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line

00000001 Baker Distributors USA 1


3
This header line
2 is attached to the
body of the
3 internal table.
. .
. .
. .
10

11
Internal Table with Header Line contd.

4 EMPLOYEE

COUNTRY ID FORMA NAME1 SORTL . . .


USA 00000002 Company Diversified Indust.. DIVERS . . .

5
ID NAME1 COUNTRY
00000002 Diversified Indust... USA Header Line

00000001 Baker Distributors USA 1

6 2
00000002 Diversified Indust... USA

3
. .
. .
. .
10

12
Creating an Internal Table without a Header Line
REPORT Y170DM40.
TABLES: EMPLOYEE. The TYPES statement defines
TYPES: BEGIN OF EMP, the structure and data type for
the internal table and its work
ID LIKE EMPLOYEE-ID,
area
NAME1 LIKE EMPLOYEE-NAME1,
The DATA statement with an
COUNTRY LIKE EMPLOYEE-COUNTRY, INITIAL SIZE creates the
END OF EMP. actual internal table without a
header line. The DATA
statement without the INITIAL
DATA: EMPTAB TYPE STANDARD TABLE SIZE creates the work area for
OF EMP INITIAL SIZE 10, the internal table.
EMPTAB_WA TYPE EMP.

Work Area ID NAME1 COUNTRY


SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB_WA.
APPEND EMPTAB_WA TO EMPTAB.
APPEND <work area> to <EMPTAB>.
ENDSELECT.

13
Internal Table without a Header Line WHY???

Separate Internal Table Work Area

Performance Issues

Nested Internal Tables

14
Internal Table without a Header Line

EMPLOYEE

A
COUNTRY ID FORMA NAME1 SORTL . . .

ID NAME1 COUNTRY
B Work Area

15
Internal Table without a Header Line contd.

1 EMPLOYEE

COUNTRY ID FORMA NAME1 SORT . . .


USA 00000001 Company Baker Distributors BAKER . . .

2 ID NAME1 COUNTRY
00000001 Baker Distributors USA Work Area

ID NAME1 COUNTRY
00000001 Baker Distributors USA 1
3 This work area
2 is not attached
to the body of
the internal
3
. table.
.
.
10

16
Transferring ABAP Dictionary Table Structures

REPORT Y170DM41.
TABLES: EMPLOYEE.
DATA: EMPTAB LIKE STANDARD TABLE OF
EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.
The internal table EMPTAB
will have the exact same
SELECT * FROM EMPLOYEE. structure as the dictionary
MOVE EMPLOYEE TO EMPTAB.
table EMPLOYEE.

APPEND EMPTAB.
ENDSELECT.

Notice the MOVE statement


instead of a MOVE-
CORRESPONDING.

17
Automatic Field Conversion

• MOVE-CORRESPONDING or MOVE field to field

– Individual field type conversion

• MOVE

– Structure to structure

– Field to structure

– Structure to field

• Intermediate C type

• Followed by adoption of new types

18
Mass Reading from Database Tables into Internal Tables

REPORT Y170DM69.
SELECT * FROM <table> . . .
TABLES: EMPLOYEE. 1. INTO TABLE <EMPTAB>.
2. APPENDING TABLE
DATA: EMPTAB LIKE STANDARD TABLE <EMPTAB>.
EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.

SELECT * FROM EMPLOYEE INTO TABLE EMPTAB


Notice no ENDSELECT is
WHERE COUNTRY = ‘USA’. needed here because no
loop processing occurs.

19
Processing an Internal Table
Report - Retrieving Internal Tables
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.

20
Processing an Internal Table
REPORT Y170DM45.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY, This LOOP AT <EMPTAB>
NAME1 LIKE EMPLOYEE-NAME1, statement allows for a logical
SALES LIKE EMPLOYEE-SALES, expression in a WHERE clause
to limit the processing of the
END OF EMP.
internal table.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10
WITH HEADER LINE.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
LOOP AT EMPTAB WHERE COUNTRY BETWEEN ‘A’ AND ‘D’. If no internal table
WRITE: / EMPTAB-COUNTRY, EMPTAB-NAME1, entries qualify under the
EMPTAB-SALES. logical expression, the
ENDLOOP. statement within the
IF SY-SUBRC NE 0. loop is not executed and
WRITE: / ‘NO ENTRIES’.
SY-SUBRC is set to 4.
ENDIF.

21
System Field SY-TABIX

REPORT Y170DM46.
TABLES: EMPLOYEE.

TYPES: BEGIN OF EMP,


COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE.

Screen output
PARAMETERS: START LIKE SY-TABIX DEFAULT 10,
END LIKE SY-TABIX DEFAULT 20.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB. SY-TABIX
ENDSELECT.
LOOP AT EMPTAB FROM START TO END.
WRITE: / SY-TABIX, EMPTAB-COUNTRY, EMPTAB-NAME1.
ENDLOOP.

22
Accumulating Data within an Internal Table

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.

23
Accumulating Data within an Internal Table
REPORT Y170DM43.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COLLECT <EMPTAB>.
COUNTRY LIKE EMPLOYEE-COUNTRY, Country Sales
Header
SALES LIKE EMPLOYEE-SALES, D 400,000
Line
END OF EMP. USA 1,000,000
DATA: EMPTAB TYPE STANDARD TABLE OF EMP GB 500,000
INITIAL SIZE 10 WITH HEADER LINE. D 7,800,000

Screen output
SELECT * FROM EMPLOYEE.
A 371,065.00
MOVE-CORRESPONDING EMPLOYEE TO CH 45,305.00
EMPTAB.
D 8,200,000.00
COLLECT EMPTAB. F 0.00
ENDSELECT. GB 500,000.00
LOOP AT EMPTAB. NL 577,000.00
NO 234.00
WRITE: / EMPTAB-COUNTRY, EMPTAB-SALES.
USA 1,000,000.00
ENDLOOP. HK 0.00

24
Sorting an Internal Table

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.

25
Sorting an Internal Table

REPORT Y170DM44.
TABLES: EMPLOYEE.
Sorting options:
TYPES: BEGIN OF EMP,
1) SORT <EMPTAB> - sorts the
COUNTRY LIKE EMPLOYEE-COUNTRY,
entries of the internal table
NAME1 LIKE EMPLOYEE-NAME1, <EMPTAB> in ascending
SALES LIKE EMPLOYEE-SALES, order.
END OF EMP. 2) SORT <EMPTAB> BY <field> -
DATA: EMPTAB TYPE STANDARD TABLE OF EMP sorts the table on one or more
INITIAL SIZE 10 WITH HEADER LINE. fields within the table.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
SORT EMPTAB BY SALES DESCENDING. screen output
LOOP AT EMPTAB.
WRITE: / ITAB-COUNTRY, ITAB-NAME1, ITAB-SALES.
ENDLOOP.

26
Control Level Processing

• 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 FIRST

• AT NEW < field >

• AT END < field >

• AT LAST

27
Control Level Processing contd.

AT FIRST AT LAST

• Statements are executed before any • Statements are executed after all records
records are processed while looping are processed while looping at Internal
at Internal table. table.
• Example : Example :
LOOP AT itab. LOOP AT itab.
AT FIRST. AT LAST.
WRITE : SY-ULINE. WRITE : SY-ULINE.
ENDAT. ENDAT.
ENDLOOP. ENDLOOP.

28
Control Level Processing contd.

AT NEW <Field Name> AT END OF <Field Name>

• Statements are executed at the • Statements are executed at the end of


beginning of a group of records a group of records containing the same
containing the same value for <Field value for <Field Name>.
Name>..

Example :
Example : LOOP AT itab.
LOOP AT itab. AT END OF I_LIFNR.
AT NEW I_LIFNR. WRITE : SY-ULINE.
WRITE : SY-ULINE ENDAT.
ENDAT. ENDLOOP.
ENDLOOP.

29
Reading an Internal Table
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,k2…kn.

30
Reading a Single Table Entry

REPORT Y170DM47.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMPTAB.

DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10


WITH HEADER LINE.

SELECT * FROM EMPLOYEE.


MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.

READ TABLE ….

31
Reading a Single Table Entry - Options

READ TABLE <EMPTAB> options:


1) READ TABLE <EMPTAB>.
2) READ TABLE <EMPTAB> WITH KEY <k1> = <v1>…
<kn> = <vn>.
3) READ TABLE <EMPTAB> WITH TABLE KEY <k1> = <v1> ...
<kn> = <vn>.
4) READ TABLE <EMPTAB> WITH KEY = <value>.
5) READ TABLE <EMPTAB> WITH KEY . . . BINARY SEARCH.
6) READ TABLE <EMPTAB> INDEX <i>.
7) READ TABLE <EMPTAB> COMPARING <f1> <f2> . . . .
8) READ TABLE <EMPTAB> COMPARING ALL FIELDS.
9) READ TABLE <EMPTAB> TRANSPORTING <f1> <f2> . . . .
10) READ TABLE <EMPTAB> TRANSPORTING NO FIELDS.

32
Filling Internal Tables

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 .

33
Filling Internal Tables

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.

34
Deleting Internal Tables

DELETE Statement

1. DELETE itab.

• The current entry of the internal table itab is deleted in a LOOP loop.

• Return code value is set to 0.

2. 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.

35
Deleting Internal Tables contd.

3. 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.

36
Maintaining Internal Tables

SELECT * FROM EMPLOYEE. INSERT <EMPTAB> INDEX <i>.


MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB. MODIFY <EMPTAB> INDEX <i>.
ENDSELECT.
DELETE <EMPTAB> INDEX <i>.
READ TABLE EMPTAB INDEX 1.
MOVE ‘ABC’ TO EMPTAB-NAME1.
MODIFY EMPTAB INDEX SY-TABIX.
IF SY-SUBRC NE 0.
WRITE / ‘Attempt to modify failed.’.
ELSE. Check SY-SUBRC after
WRITE: / EMPTAB-COUNTRY, every attempt to change
EMPTAB-NAME1. an internal table entry.
ENDIF.
INSERT EMPTAB INDEX 1.
DELETE EMPTAB INDEX SY-TABIX.

37
Deleting an Internal Table

CLEAR <internal table>

 Initialises the header line.

 Internal table lines REFRESH <internal table>


remain unchanged.
FREE <internal table>
 Deletes all table lines.
 Storage space is not
released.  Deletes all table lines.
 Paging is released.  Storage space is
released.
 Header line remains
unchanged.  Header line remains
unchanged

38
Retrieving Internal Table attributes
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 itab in the field lin.

2. ... OCCURS n

• Transfers the size of the OCCURS parameter from the table definition to the variable
n.

39
Information about an Internal Table

REPORT Y170DM49.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10
WITH HEADER LINE,
DESCRIBE TABLE <internal table>
LINE_COUNT TYPE I,
LINES <var1>
INITIAL_COUNT TYPE I.
OCCURS <var2>.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
DESCRIBE TABLE EMPTAB
LINES LINE_COUNT
OCCURS INITIAL_COUNT.
WRITE: / ‘ lines:’, LINE_COUNT, screen output
/ ‘occurs:’, INITIAL SIZE_COUNT.

40
Operations on Internal Tables

Operations without header line Operations with header line


Operations for all Table Types
INSERT <wa> INTO TABLE <itab>. INSERT TABLE ITAB.
COLLECT <wa> INTO <itab>. COLLECT <itab>.
READ TABLE <itab> ... INTO <wa>. READ TABLE <itab>
MODIFY TABLE <itab> FROM <wa> MODIFY TABLE <itab>
MODIFY <itab> FROM <wa> ...WHERE MODIFY <itab> ... WHERE
DELETE TABLE <itab> FROM <wa>. DELETE TABLE <itab>.
LOOP AT ITAB INTO <wa> ... LOOP AT ITAB

Operations for Index Tables


APPEND <wa> TO <itab>. APPEND <itab>.
INSERT <wa> INTO <itab> ... INSERT <itab>
MODIFY <itab> FROM <wa> ... MODIFY <itab>

41
ABAPDOCU

ABAPDOCU is a transaction code which contains reference programs. Internal table


sample programs are available over here.

42
Thank You!

You might also like