You are on page 1of 8

F T ra n sf o F T ra n sf o

PD rm PD rm
Y Y
Y

Y
er

er
ABB

ABB
y

y
bu

bu
3.0

3.0
to

to
re

re
[Pick the date]
he

he
k

k
lic

lic
C

C
w om w om
w

w
w. w.
A B B Y Y.c A B B Y Y.c

ABAP INTERNAL TABLE

Internal table basics | Tanumoy


F T ra n sf o F T ra n sf o
PD rm PD rm
Y Y
Y

Y
er

er
ABB

ABB
y

y
bu

bu
3.0

3.0
to

to
re

re
he

he
k

k
lic

lic
Created by Tanumoy Mistri 2w . A B
C

C
w om w m
w

w
w.
A B B Y Y.c co
B Y Y.

Internal Tables
Internal table is a temporary table which is stored in RAM on the application server. It is created and filled by a
program during execution and is discarded when the program ends.

Definition of Internal table: An internal table consists of a header line and body.

Header Line: A header line is a field string with the same structure as a row of the internal table. It can only
hold a single row. It’s used as a buffer while data is inserted to internal table or retrieved from the internal
table.

Body: The body holds the rows of the internal table.

Structure of internal table:

Header Line of Internal table it.

Body of Internal table it.

Where it is the name of the Internal table.

How to define Internal table in a program:

data: begin of <internal_table_name> occurs n,

<internal_table component 1>,

<internal_table component 2>,

<internal_table component n>,

end of <internal_table_name>.

From the above code, we use begin of to define an internal table with header line. If we use with header line,
we define an internal table with a header line.
F T ra n sf o F T ra n sf o
PD rm PD rm
Y Y
Y

Y
er

er
ABB

ABB
y

y
bu

bu
3.0

3.0
to

to
re

re
he

he
k

k
lic

lic
Created by Tanumoy Mistri 3w . A B
C

C
w om w m
w

w
w.
A B B Y Y.c co
B Y Y.

Here, we use occurs n to define the body of the internal table. When the first row is added to the internal
table, memory is allocated for the internal table.

A nested internal table cannot have header line.

Work Area: Work area is a field string having the same structure as a row of an internal table. Work area is
also acting like a buffer while adding data to or retrieving data from internal table. There are two types of
work areas are there: 1) Explicit Work Area

2) Implicit Work Area.

Any field string having the same structure as a row of an internal table can be used as an Explicit Work Area.

If any work area is not defined, the Implicit Work Area is used.

Implicit Work Area is known Header Line of an Internal table.

Adding data to an Internal table:

To add a single row to an internal table, we use append statement. append copies the single row of the work
area or the header line and places it at end of the existing rows of the internal table body.

Syntax: 1) append it. This statement implies that, it copies the single row of the header line it and

places it at the end of the existing rows of the internal table body.

2) append it to it. It does the same thing as append it.

3) append wa to it. This statement implies that, it copies the single row of the explicit work area

wa and places it at the end of the existing row of the internal table body.

4) append initial line to it. This statement appends the initial line i.e. blanks and zeros to the

internal table body.

5) append lines of it1 [from index1 to index1] to it2

This statement appends the lines of an internal table (it1) to another


internal table (it2).
F T ra n sf o F T ra n sf o
PD rm PD rm
Y Y
Y

Y
er

er
ABB

ABB
y

y
bu

bu
3.0

3.0
to

to
re

re
he

he
k

k
lic

lic
Created by Tanumoy Mistri 4w . A B
C

C
w om w m
w

w
w.
A B B Y Y.c co
B Y Y.

Example of append it or append it to it:

REPORT ZDAY11_PROG6.

data: begin of it occurs 0,


f1(1) type n,
f2(4) type c,
end of it.

it-f1 = 1.
it-f2 = 'AAAA'.

append it to it.
write: /'Sy-tabix: ', sy-tabix.

it-f1 = 2.
it-f2 = 'BBBB'.

append it.
write : /'Sy-tabix: ', sy-tabix.

Output:
Sy-tabix: 1

Sy-tabix: 2

Analysis : In this program, we declare an internal table named it. The components of the internal table are f1
and f2 of type ‘n’ and ‘c’. The statement it-f1 = 1. and it-f2 = ‘AAAA’. fills the header line it. Here it refers to
the header line of the internal table. The statement append it to it. states that, data of the header line it is
appended at the first row of the internal table it. For this reason, the next statement write: /’Sy-tabix’, sy-
tabix gives the output 1, where sy-tabix gives current row index. The next statement append it is same as
append it to it.

How does it work?

it-f1 = 1.

it-f2 = ‘AAAA’.

header line it 1 AAAA

append it to it

Body of internal table it 1 AAAA

Here Sy-tabix is 1.
F T ra n sf o F T ra n sf o
PD rm PD rm
Y Y
Y

Y
er

er
ABB

ABB
y

y
bu

bu
3.0

3.0
to

to
re

re
he

he
k

k
lic

lic
Created by Tanumoy Mistri 5w . A B
C

C
w om w m
w

w
w.
A B B Y Y.c co
B Y Y.

it-f1 = 2.

it-f2 = ‘BBBB’.

header line it 2 BBBB

append it to it

Body of internal table it 1 AAAA


2 BBBB
Here Sy-tabix is 2.

Example of append wa to it:

REPORT ZDAY11_PROG7.

data : begin of it occurs 0,


f1(1) type n,
f2(4) type c,
end of it,
wa like it.

wa-f1 = 1.
wa-f2 = 'AAAA'.

append wa to it.

write : /'Sy-tabix', sy-tabix.

wa-f1 = 2.

append wa to it.

write : /'Sy-tabix', sy-tabix.


Output:
Sy-tabix: 1

Sy-tabix: 2

Analysis : In this program, we declare an internal table named it. The components of the internal table are f1
and f2 of type ‘n’ and ‘c’. We also declare a work area wa like the internal table it using wa like it statement.
The statement it-f1 = 1. and it-f2 = ‘AAAA’. fills the work area wa. The statement append wa to it. states that,
data of the work area wa is appended at the first row of the internal table it. For this reason, the next
statement write: /’Sy-tabix’, sy-tabix gives the output 1.

The next statement wa-f1 = 2 changes the content of the f1 component but content of f2 remain same.
F T ra n sf o F T ra n sf o
PD rm PD rm
Y Y
Y

Y
er

er
ABB

ABB
y

y
bu

bu
3.0

3.0
to

to
re

re
he

he
k

k
lic

lic
Created by Tanumoy Mistri 6w . A B
C

C
w om w m
w

w
w.
A B B Y Y.c co
B Y Y.

How does it work?

wa-f1 = 1.

wa-f2 = ‘AAAA’.

Work area wa 1 AAAA

append wa to it

Body of internal table it 1 AAAA

Here Sy-tabix is 1.

wa-f1 = 2.

Work area wa 2 AAAA

append wa to it

Body of internal table it 1 AAAA


2 AAAA

Here Sy-tabix is 2.

Example of append lines of it1 to it2:

REPORT ZPRACTICE1 .

data : begin of it1 occurs 0,


f1 type i,
f2 type c,
f3 type n,
end of it1,

begin of it2 occurs 0,


g1 type i,
g2 type c,
g3 type n,
end of it2.

it1-f1 = 2.
it1-f2 = 'BBBB'.
it1-f3 = 002.
F T ra n sf o F T ra n sf o
PD rm PD rm
Y Y
Y

Y
er

er
ABB

ABB
y

y
bu

bu
3.0

3.0
to

to
re

re
he

he
k

k
lic

lic
Created by Tanumoy Mistri 7w . A B
C

C
w om w m
w

w
w.
A B B Y Y.c co
B Y Y.

append it1.

it1-f1 = 3.
it1-f2 = 'CCCC'.
it1-f3 = 003.

append it1.

loop at it1.

write : / it1-f1,
it1-f2,
it1-f3.

endloop.

it2-g1 = 1.
it2-g2 = 'AAAA'.
it2-g3 = 001.

append it2.

append lines of it1 to it2.

loop at it2.

write : / it2-g1,
it2-g2,
it2-g3.

endloop.

Output:

Importing content of one internal table to another internal table

2 B2

3 C3

1 A1

2 B2

3 C3

Reading data from an Internal table:

We commonly use two statements to read data from an internal table.

1) loop at.
2) read table.

Reading multiple rows from an internal table using loop at:


F T ra n sf o F T ra n sf o
PD rm PD rm
Y Y
Y

Y
er

er
ABB

ABB
y

y
bu

bu
3.0

3.0
to

to
re

re
he

he
k

k
lic

lic
Created by Tanumoy Mistri 8w . A B
C

C
w om w m
w

w
w.
A B B Y Y.c co
B Y Y.

We commonly use loop at statement to read multiple rows from an internal table. loop at statement reads
multiple rows from an internal table and places them into a work area.

Syntax: 1) loop at it.

-----

endloop.

2) loop at it into wa.

-------

endloop.

3) loop at it [into wa] from m to n where exp.

--------

endloop.

You might also like