Sie sind auf Seite 1von 41

Internal table

Internal table is a very important concept in ABAP/4 programming. For a novice programmer, it is

essential that He / She understands the underlying concept of internal table. This documentation

explains internal table in a very precise and in simple words. It explains from the basics of an internal

table and gradually navigating to its features and operations.

I have explained Standard, Sorted and Hashed tables and its operations separately and in the respective

order. My suggestion towards a clear understanding of internal table from this document is to have a

clear idea of Standard Table first and practice and then move to Sorted and Hashed Tables.

For your convenience I have isolated the system fields used for internal tables and defined few terms

in Glossary that are necessary for understanding Internal Tables. I would like to thank

www.erpgenie.com for providing online documentation on ABAP/4 for young ABAP’ers like me.

Believe this document will provide a clear understanding of Internal Table. Happy ABAPing.

Introduction: -

Internal Tables are local tables within a program containing a series of lines having same data type.
ABAP Open SQL allows single field, range of fields, entire database table or view into an Internal
table.

In technical terms Internal table is a dynamic sequential dataset in which all records have the same
data structure and a key.

Internal tables are used for fetching large volume of data from the database, storing in ABAP working
memory line-by-line and processing within a program.

Although Internal tables are declared with the other data objects, at runtime they behave as dynamic
objects (i.e.) no need to specify the size of the object but only the length of a row in internal table is
fixed. The number of rows is determined dynamically at runtime with the fixed structure.

Internal table is characterized by the following: -


Line Type: - The line type may be any data type or another internal table. Generally the data type will
be a structure and each component of a structure is a column in this local table.

Key: - Key is used to identify table rows. You may specify whether the key is UNIQUE or NON-
UNIQUE. As the name indicates UNIQUE key cannot contain duplicate entries whereas NON-
UNIQUE can.

Table Type: - Table type specifies the behavior of Internal table while accessing the individual
entries. There are three types of table.

Standard Table defines the table as one that has the same order of its line type. It can be accessed
either by using internal index or key. The response time for index access increases logarithmically
whereas by key access, it is proportional to the number of entries. The key of a standard table is
always NON-UNIQUE.

Standard tables are filled using the APPEND statement and the entries are read, modified and deleted
using the index access. The apt situation for using standard table is when you need to fill and process
the table in separate steps.

Sorted Table defines as the table that is sorted in a specified order. It can be accessed either by using
internal index or key. The response time for key increases logarithmically with the number of entries.
The key of a Sorted table can be either UNIQUE or NON-UNIQUE.

Sorted tables are filled using the INSERT statement depending upon the UNIQUE or NON-UNIQUE
key. The apt situation is when you need a table for partial sequential processing.

Hashed Table defines as the table that is managed with an internal has procedure. It must be accessed
using its hash key. The response time is independent of the number of entries as it used Hash
Algorithm. The key of a Hashed table must be UNIQUE.

Like Database table, Hashed table have a UNIQUE key. If the main operation in the table is based on
the key and for processing large volume of data, hashed table is the apt one.

Creating Internal Tables: -


Internal Tables can be declared as an abstract data type within a program or in ABAP data dictionary
and then defined a data object. On the other hand it can be directly defined as a data object in the
program but it is considered as outdated.

Internal tables as data types: -

Internal tables can be declared either locally or globally, if it is declared as an abstract data type
within the program it is said to be local whereas if in ABAP Data dictionary it is said to be global
definition.

With all other local types internal table is declared using TYPES statement. The syntax is as follows: -

TYPES <itab> TYPE|LIKE <table type> OF <line type>

[WITH UNIQUE|NON_UNIQUE <key>] [INITIAL SIZE <n>]

When declared as an data object the TYPE|LIKE is followed by existing data type, but here as you are
declaring the Internal Table as a abstract data type, you must specify the table type.

Table Type: - There are two forms of table types, Generic and fully specified.

Generic Table Types: There are two table types namely INDEX TABLE and ANY TABLE.

INDEX TABLE – For creating a generic table type for index access.

ANY TABLE - For creating a fully generic table where the common operation key access is only
allowed

Data types declared using Generic type must be used for field symbols or interface parameters for
routines. For an data type if INDEX TABLE is specified only standard and sorted tables must be
passed to the field symbols or interface parameters, you cannot hashed table. For a data type if ANY
TABLE is specified you can pass standard, sorted and hashed table to the field symbols and interface
parameters but the behavior of all the table will be same (i.e.) field symbols and interface parameters
will allow operations that is common to all tables. In other words only key access is allowed, index
access is not allowed.

Fully Specified Table Types: There are three table types as follows:

STANDARD TABLE – Creates Standard Table and uses linear search

SORTED TABLE - Creates Sorted Table according to the key specified and uses
binary search

HASHED TABLE - Created Hashed Table and uses hash algorithm


Line Type: - The line type depends on TYPE|LIKE defined.

If TYPE is used, the line type must take from data type either declared locally or in ABAP Dictionary.
When internal table is declared for elementary data types (C, N, P, X), default attributes are assigned
when the technical attributes are not defined explicitly.

If LIKE is used, the data object mentioned for <line type> must be recognizable at that point.

Key: - The Key is specified as follows.

WITH UNIQUE|NON-UNIQUE KEY <key>

UNIQUE specifies the mentioned column cannot contain any duplicate entries whereas the NON-
UNIQUE specifies the other way.

In Structured Line type the <coli> belong to key if its not anyway related to internal table or
references. Key fields can be Nested Structures and are expanded as the corresponding fields are
accessed. The syntax is as shown

WITH UNIQUE|NON-UNIQUE KEY <col-1>…<col-n>

In an elementary line type the entire line can be defined as a key. The syntax is as shown.

WITH UNIQUE|NON-UNIQUE KEY TABLE LINE

In addition to the above syntax you can specify the default key. The default key for a structured line
type is a all non-numerical column of an internal table, for an elementary line type the default key is
the entire line and for an internal table whose line type is an internal table the default key is empty.

WITH UNIQUE|NON-UNIQUE DEFAULT KEY

For an internal table specifying the key is not mandatory, if the key is not specified the system defines
an arbitrary key.

Initial Memory Requirement: -

INITIAL SIZE <n>

With the above addition you can specify the initial memory by specifying the number of lines of an
internal table. Often, you cannot be sure of the number of lines of an internal as they are assigned
dynamically. When using deep structures this addition will be really useful. But you can reserve a
initial size of an internal table, and once its full, the system allocates twice as much of memory
allocated initially up to 8KB and upon crossing the 8KB limit it allocates 12KB each to the memory of
the internal table.

In order to reserve initial size and at the same time avoiding excessive usage of memory, the value of
<n> can be assigned to the quotient of 8KB divided by the length of a line in internal table.

Types: begin of tab,

num type I,

num1 type I,

end of tab.

Types: itab type standard table of tab with default key initial size 10,

sort_itab type sorted table of tab with unique key num initial size 10,

hash_itab type hashed table of tab with unique key num initial size 10.

The above table defines an internal table as a fully specified data type. All three internal tables have
been initially allocated 10 lines, but the way they access the individual entries is different. ‘itab”, is a
standard table with default key (as <line-type> is elementary data type, the default key is the entire
line). ‘sort_itab’ is an internal table that is sorted according to the key ‘num’. hash_itab is an internal
table using hash algorithm and its key is defined as ‘num.

If you use ANY TABLE or INDEX TABLE in the place of <table-type> you define Generic Tables
that are used for passing to Field Symbols and interface parameters routines.

Internal Tables as Data Objects: -

Internal table can be declared directly as data objects using DATA, STATICS and CLASS-DATA
statement. The STATICS is used to create internal tables in procedures and CLASS-DATA is used to
create internal tables in classes. The DATA is used to declare internal table data objects with all the
other local objects in the program.

With all the other data objects internal table is declared using the DATA statement with the LIKE or
TYPE addition. The syntax for both is as follows:

DATA: <itab> LIKE <obj> [with header line].


The above syntax is used to create an internal table object where the LIKE addition refers to the
existing table object within the program.

DATA: <itab> TYPE <type> [with header line].

The above syntax is used to create an internal table object where the TYPE additoin refers to the type
defined within the program using the TYPES statement or type defined in the ABAP Dictionary.

In contrast to the internal table declaration using the TYPES statement, DATA statement does not
allow to define to generic internal types. Internal tables declarations using the DATA statement must
be fully specified.

Header Line or Work Areas: -

Header line or Work Areas is one of the important concepts in Internal tables. As you see with the
above syntax internal table is declared with the data object, header line. Both header line and work
area is associated data object with the internal table. They share the same meaning except that when it
is declared with the internal table, is termed as header line otherwise if declared separately is termed as
work area.

Before explaining the importance of Header line, let me make clear how the internal table is accessed.

The systems perspective to the internal table is header of the internal table and then body of the
internal table.

Header is nothing but a single row of the internal table components. When a system processes the
body of the internal table it must have accessed the header of the internal table (i.e.). Work area or the
header line act as interfaces to the body of the internal table. Precisely, when a system performs a
write operation on the body of the internal table, it first writes to the header and then copy to the body
of the internal table and it applies to the read and other manipulations.

The header of the internal table can be declared in two ways. One with the internal table declaration
and other declaring separately using the DATA statement as shown.

DATA: <itab> LIKE <obj> [with header line].

The above syntax declares internal table with the header line. As you can see the header line and the
body is declared in the statement and so it has the same name. They are differentiated as follows.
<itab[]> addresses the body of the header line and <itab> address the header of the internal table.
DATA: <itab> LIKE <obj>,

<itab_wa> LIKE LINE OF <itab>.

The above syntax declared internal table and header line separately hence has different names. Now a
day its better to define the header line separately in order to improve the performance of the program.

In either case, each time the work area is accessed the contents are overwritten. This is the most
important feature to be remembered, as we have to manually clear the contents of the work area or the
header line at certain points in the program.

data:itab_obj type itab1,

itab_obj1 like itab2 with header line,

itab_obj2 like table of mara with header line ‘defined from ABAP

Dictionary,

wa like line of itab_obj.

write: itab_obj1 ‘ Header line,

itab_obj1[] ‘Body [just writes the last row as its not in the

loop]

The above table creates a internal table object from the type defined in Table 1. itab_obj is an internal
table data object of <table-type> itab1 without header line. itab_obj1 is an internal table data object of
<table-type> itab2 with header line. As mentioned before, in latter case both internal table and header
line shares the same name. It is differentiated as shown above.

Processing Internal Tables: -

Internal tables can be processed either as a whole or in individual lines. When internal table is
processed on whole you address the body of the internal table whereas when internal table is processed
line by line you address the header or work area of the internal table.
Note: - If you are using internal table with header lines they are processed separately because both
share the same name. The body of the internal table is denoted by <itab>[] and header line of the
internal table is denoted by <itab>. If the work area or header line is declared separately, then they can
process with their own names.

First let us discuss the operations that favor the entire internal table processing.

1. Initializing Internal Tables: - There are three statements to initialize the internal table with its
own unique features.

CLEAR <itab>.

This statement clears the internal table and its contents only but the memory occupied is not cleared.
The unique feature of this statement is it can be used to clear both the body and header of the internal
table separately. To clear the body of the internal table use <itab>[] and to clear the header of the
internal table use <itab> as shown in Table 3.

clear : itab_obj1, ‘ clearing the header line of the internal table

Itab_obj1[] ‘ clearing the body of the internal table

REFRESH <itab>.

This statement always the clear the body of the internal table, header of the internal table cannot be
accessed. As with CLEAR statement, the memory remains allocated.

refresh itab_obj1 ‘ clearing the body of the internal table


FREE <itab>.

This statement always applies to the body of the internal table, header line cannot be accessed. But if
you want to really release the memory allocated to the internal table this statement is used. But the
memory assigned to the header line remains allocated.

free itab_obj1 ‘ clearing the body of the internal table

2. Assigning Internal tables.

An Internal table can be assigned to another internal table if they are compatible and convertible. The
entire contents of one internal table are assigned to the other. When you are using Internal table with
header line Only concern is whether you are processing the body or the work area of the internal table,
if it’s the body then itab[] is used if it’s the header line then itab is used.

Internal tables can be assigned using the MOVE statement as with the other variables. Alternatively
you can use the ‘=’ statement as follows.

MOVE <itab1> TO <itab2> “ If the work area is processed

MOVE <itab1>[] TO <itab2>[] “ if the body is processed

MOVE <itab1[]> TO <itab2> “ This returns an ERROR

MOVE <itab1> TO <WA> “ Internal table header line is assigned to

Work Area

Alternatively you can use,

<itab1> = <itab2> “ When the work areas is accessed

<itab1[]> = <itab2[]> “ When the body is accessed

<itab1[]> = <itab2> “ Returns ERROR


n =0.

do.

n = n +1.

move ‘1’ to wa-num.

move n to wa-num1.

move wa to itab_obj.

append itab_obj.

while n <=10.

move ‘2’ to wa-num.

move ‘10’ to wa-num.

move wa to itab_obj.

append itab_obj.

loop at itab_obj.

write:/ itab_obj-num, itab_obj-num1.

endloop.

Output

--------

11

12

13

14

15
16

17

18

19

1 10

2 10

Now the internal table ‘itab_obj’ has values as shown above. Alternatively you can use equal
statements as well.

3. Comparing Internal Tables: -

Internal tables can be compared with the operands that are used to compare other data objects. The
most important criteria for comparing the internal table are the number of lines they contain. The
larger the number of lines, the larger it is for comparisons. If the both the internal tables have same
number of lines, then they are compared line by line. The operands used for comparisons are LE, LT,
GE, GT, EQ, NE.

Except for EQ, the comparison stops at the first pair of components that identifies the condition false.

4. Sorting Internal Tables: -

If you want to sort a standard or hashed table using its table key (defined at the time of declaration),
the following syntax applies:

SORT <itab> [ASCENDING|DESCENDING] [AS TEXT] [STABLE]


In the absence of the table key field during declaration the default key is the non-numerical field of the
table.

You cannot sort a sorted table, as they are sorted dynamically using the key specified at the time of
declaration.

If you want to sort a standard or hashed table using different key the following syntax applies:

SORT <itab> ASCENDING|DESCENDING] AS TEXT [STABLE}

BY <field1> ASCENDING|DESCENDING] AS TEXT….

The above syntax sorts the internal table according to the <field1> instead of the table key.

ASCENDING|DESCENDING addition: -

As the name implies, this addition is used to sort the fields either in ascending or in descending order
specified.

AS TEXT addition: -

This addition is used to sort the strings in alphabetical order. It can be used to sort the entire table or a
single field with the provision that it must be type C. Without this addition, the system sorts according
to the hardware platform.

STABLE addition: -

If you sort a table several times using the same key, the sort order changes often. In order to have a
stabilized order this addition is used so that the sort order does not change.
sort itab_obj ‘ sorts the itab (from Table 1) using table key in

ascending (default)

sort itab_obj by num descending ‘ sorts itab using key defined in

descending order

Sort itab_obj by num1 descending.

Loop at itab_obj.

Write:/ itab_obj-num1, itab_obj-num.

Endloop.

Sort itab_obj by num1 descending.

Loop at itab_obj.

Write:/ itab_obj-num1, itab_obj-num.

Endloop.

Sort itab_obj by num1 descending stable.

Loop at itab_obj.

Write:/ itab_obj-num1, itab_obj-num.

Endloop.

Output

--------

The first write Statement outputs (I have taken only 3 values)

10 1

10 2

91
The second write statement may output

10 2

10 1

91

As you see although you sorts in descending order the itab_obj-num changes in first
and second output. But with addition STABLE you always have the same order
doesn’t matter how many times you sort.

The third write statement with STABLE

10 1

10 2

91

Operations for Index Tables: - The following operations are allowed only for Index tables (i.e.)
Standard and Sorted Tables. Of all the three tables listed in this document, Standard table is more
flexible. As it does not allow UNIQUE key, there are not many constraints to be checked before filling
the table. Hence, it is advisable to create a standard table and then copy to the table of need.

1. Appending Table lines: - Appending the table is one of the quickest ways to fill the index
tables. The simple form of Append is as follows:

APPEND <itab>.

When an internal is declared with the header line, the above statement moves the contents from the
table (defined in <line type>) to the header line and then copied to the body of the internal table.
If the Internal table and work area and declared separately with the same <line type> then the
following syntax applies.

APPEND <wa> TO <itab>.

As mentioned before, it is always better to declare work area and internal table separately in terms of
performance.

Appending Several Lines of Internal Table: -

APPEND LINES OF <itab1> [FROM <n1> TO <n2>] TO <itab2>

The above statement is used to append the whole of <itab1> to <itab2>.

Note: Often during programming, you might not realize what APPEND exactly does. At any case, it
always adds the table thereby keeping the existing entries if the table is not empty.

The <n1> and <n2> determines the index of the first and last lines of <itab1> to be copied to <itab2>.

In the case of Sorted table, the same applies except we have to keep up with the key defined during the
declaration of internal table.

The program below shows two forms of append statement depending on the header line declaration of
the internal table

data : begin of struct,

matnr like mara-matnr,

ersda like mara-ersda,


ernam like mara-ernam,

end of struct.

types tab type standard table of struct.

Data: itab like table of tab with header line,

itab1 like table of tab,

wa like line of itab.

select matnr ersda ernam from mara into corresponding fields of itab.

append itab.

endselect.

Select matnr ersda ernam from mara into corresponding fields of wa.

append wa to itab.

endselect.

Alternatively you can modify the select without append and endselect statement as shown. This
statement works the same way as the above but better in performance.

select matnr ersda ernam from mara appending fields of itab.

endselect.

select matnr ersda ernam from mara appending fields of table itab.
2. Inserting Table lines: - The INSERT statement allows you to insert lines to the Index tables.
This command is opt for Sorted table. Though we can use this command for standard table,
APPEND is considered to be the best in terms of performance.

Like APPEND, you can insert either a single line or multiple lines to the table. To insert a single line
to the following syntax applies:

INSERT <line> INTO <itab> [INDEX <index>]

The <line> can be a work area that is either compatible or convertible to the <line type> declared with
the internal table.

Without the INDEX addition, this statement is allowed only within a loop so that it inserts the lines to
internal table thereby incrementing index automatically.

With the INDEX addition, the internal table is filled before the line specified in <index> and the
following line’s index is incremented by one. When the total number of lines of an internal table is
equal to <index> - 1, the <line> is inserted at the end of the local table. If a table has less than <index>
- 1 lines, SY-SUBRC is set 4.

Inserting several lines: - The following syntax applies when you want to insert several lines from one
internal table to the other specifying the <index>.

INSERT LINES OF <itab1> INTO <itab2> [INDEX <index>]

The above statement inserts the lines from <itab1> to <itab2> line by line like the above INSERT
statement.

INSERT LINES OF <itab1> [FROM <n1> TO <n2>] INTO <itab2> [INDEX <index>]

The above statement specifies <n1> and <n2> thereby the first and last lines of <itab1> to <itab2>.
Data : begin of struct,

Empno type I

Empname(50) type char,

End of struct.

Types tab type sorted table of struct with unique key empno

Data : itab like tab with header line,

Jtab like tab with header line,

struct-empno = ‘0001’. struct-empname = ‘Stephen’.

Insert struct into itab.

struct-empno = ‘0002’. struct-empname = ‘Jack’.

Insert struct into itab.

struct-empno = ‘0003’. struct-empname = ‘Jill’.

Insert struct into itab.

Loop at itab.

Write:/ itab-empno, itab-empname.

Endloop.

Output

---------

0001 Stephen

0002 Jack

0003 Jill
Insert lines of itab into jtab.

Loop at jtab.

Write:/ jtab-empno, jtab-empname.

Endloop.

Output

---------

0001 Stephen

0002 Jack

0003 Jill

The above program shows the demonstration of both the insert statements for a sorted table with
unique key.

3. Reading Lines using the Index.

In addition to inserting, lines from the local tables can be read using READ statement. The syntax is
as follows.

READ TABLE <itab> INDEX <index> <result>.

The system reads the line with the <index> from the table <itab>.

Read table itab index 2 into struct.

Write:/ struct-empno, struct-empname.


Output

---------

0002 Jack

The above program reads a single entry from internal table (itab) with index 2.

4. Changing Lines: -

You can change a single line or a group of lines using the MODIFY statement. The system searches
the table using linear search, binary search and hash algorithm for Standard, Sorted and Hashed tables
respectively. If the table contains a NON-UNIQUE key, the first entry is changed.

To change a single line of the local table without the condition the following syntax is used.

MODIFY <itab> from <wa>

The <wa> must be compatible with the <line type> defined (declared with the internal table). It
searches for the contents in the internal table whose table key values correspond to the values in <wa>
and then the table is modified.

To change one or more lines that meet certain condition the following syntax is used.

MODIFY <itab> from <wa> TRANSPORTING <f1>…<fn> WHERE <cond>

The <wa> must be compatible with the <line type> defined (declared with the internal table). It
searches for the contents to be changed and contains the new contents as well. All the lines of the
internal table that satisfies the condition is changed.

Struct-empno = ‘0003’. Struct-empname = ‘Jason’

Modify itab form struct.

Modify itab from struct transporting empname where (empno = ‘0003’).

‘ This is the alternative way to modify, but it changes several lines

if exist.
Loop at itab.

Write:/ itab-empno, itab-empname.

Endloop

Output

---------

0001 Stephen

0002 Jack

0003 Jason

The above program demonstrates the use of MODIFY Statement. As ‘struct’ is compatible with the
internal table (itab) line type, the internal table is searched for the entries that are compatible with the
work area and they are modified.

Deleting Lines from Internal Table: -

To delete single or more lines from the internal table using index use DELETE statement.

To delete a line using the index the syntax is as follows: -

DELETE ITAB [INDEX <index>]

The above statement deletes the line from the internal table <itab> that corresponds to the INDEX
<index> and reduces the subsequent lines by 1.
Without the INDEX option it can be only used within the loop and the manipulation is carried
implicitly using SY-TABIX.

To delete more lines using the index the syntax is as follows: -

DELETE ITAB [FROM <n1> TO <n2>] WHERE <cond>

The above statement deletes all the lines from index <n1> to <n2> that satisfies the condition. If you
do not specify FROM <n1> the system deletes from the first line till <n2>. Likely If you do not
specify TO <n2> the system deletes all lines from <n1> till the end of the table.

Do

N = n +1.

Itab_obj1-num = n.

Itab_obj1-num1 = 10

Append itab_obj1.

While n<=5.

Loop at itab_obj1.

Write:/ itab_obj1-num, itab_obj1-num1.

Endloop.

Output

--------

1 10

2 10

3 10

4 10

5 10

Delete itab index 2. ---------------------------- A


Delete itab from 1 to 5 where num < 2. ------ B

The statement A deletes the second entry from top of the internal table.

The statement B deletes the entries from index 1 to index 5 where the first field is less than 2, (i.e.) the
first entry of the internal table.

LOOP AT Operations using INDEX.

You can process an internal within a loop using the index. The syntax is as follows.

LOOP AT <itab> [FROM <n1> TO <n2>] [WHERE <cond>]

<Statements>

ENDLOOP

The above statement loops the internal table from index <n1> to index <n2> that satisfies the
condition <cond> and then processed. The loop statement can be executed even without the additions,
except that whole internal table is processed, but it’s advised that wherever you meet conditions, its
better to specify in order to improve the performance.

Operation for ANY TABLE: -

The operation listed applies to any table type, but if you know the table type its better to use the apt
command for that table type. For instance, you can append lines in a standard table using both
APPEND and INSERT command, but APPEND is preferred to INSERT for performance reasons. The
following shows the operations for any tables.

The most important thing in these operations is, to work for any type of table; you must use the
operations that are common (i.e.) you must specify the key but not the index.

4. Inserting Table lines: - The INSERT statement allows you to insert lines to the Index tables.
This command is opt for Sorted table. Though we can use this command for standard table,
APPEND is considered to be the best in terms of performance.

Like APPEND, you can insert either a single line or multiple lines to the table. To insert a single line
to the following syntax applies:
INSERT <line> INTO TABLE <itab>

The <line> can be a work area that is either compatible or convertible to the <line type> declared with
the internal table.

Inserting several lines: - The following syntax applies when you want to insert several lines from one
internal table to the other specifying the <index>.

INSERT LINES OF <itab1> INTO TABLE <itab2>

The above statement inserts the lines from <itab1> to <itab2> line by line like the above INSERT
statement.

INSERT LINES OF <itab1> [FROM <n1> TO <n2>] INTO TABLE <itab2>

The above statement specifies <n1> and <n2> thereby the first and last lines of <itab1> to <itab2>.

As you can see the INSERT statement of Index Table and for All Tables differs by the keyword
TABLE. Generally, for All tables you need to include the TABLE keyword in most of the statements.

If it’s a Standard table, the line is appended to the end of the internal table, if its sorted table, the line is
inserted according to the table key and for Hashed table, the line is inserted according to the hash
algorithm.

Data : begin of struct,

Empno type I

Empname(50) type char,

End of struct.
Types tab type sorted table of struct with unique key empno

Data : itab like tab with header line,

Jtab like tab with header line,

struct-empno = ‘0001’. struct-empname = ‘Stephen’.

Insert struct into table itab.

Struct-empno = ‘0002’. struct-empname = ‘Jack’.

Insert struct into table itab.

struct-empno = ‘0003’. Struct-empname = ‘Jill’.

Insert struct into table itab.

Loop at itab.

Write:/ itab-empno, itab-empname.

Endloop.

Output

---------

0001 Stephen

0002 Jack

0003 Jill

Insert lines of itab into table jtab.

Loop at jtab.
Write:/ jtab-empno, jtab-empname.

Endloop.

Output

---------

0001 Stephen

0002 Jack

0003 Jill

The above program shows the demonstration of both the insert statements for a sorted table with
unique key. It is same as example demonstrated for INSERT statement for INDEX tables. In order to
differentiate that only ‘TABLE’ key is included in INSERT statement for ALL TABLES operations, I
have given the same example.

Changing Lines: -

You can change a single line or a group of lines using the MODIFY statement. The system searches
the table using linear search, binary search and hash algorithm for Standard, Sorted and Hashed tables
respectively. If the table contains a NON-UNIQUE key, the first entry is changed.

To change a single line of the local table without the condition the following syntax is used.

MODIFY TABLE <itab> from <wa>

The <wa> must be compatible with the <line type> defined (declared with the internal table). It
searches for the contents in the internal table whose table key values correspond to the values in <wa>
and then the table is modified.

To change one or more lines that meet certain condition the following syntax is used.

MODIFY TABLE <itab> from <wa> TRANSPORTING <f1>…<fn> WHERE <cond>

The <wa> must be compatible with the <line type> defined (declared with the internal table). It
searches for the contents to be changed and contains the new contents as well. All the lines of the
internal table that satisfies the condition is changed.
Struct-empno = ‘0003’. Struct-empname = ‘Jason’

Modify table itab form struct.

Modify table itab from struct transporting empname where (empno = ‘0003’).

‘ This is the alternative way to modify, but it changes several lines

if exist.

Loop at itab.

Write:/ itab-empno, itab-empname.

Endloop

Output

---------

0001 Stephen

0002 Jack

0003 Jason

The above program demonstrates the use of MODIFY Statement. As ‘struct’ is compatible with the
internal table (itab) line type, the internal table is searched for the entries that are compatible with the
work area and they are modified. It is same as example demonstrated for INSERT statement for
INDEX tables. In order to differentiate that only ‘TABLE’ key is included in INSERT statement for
ALL TABLES operations, I have given the same example.

Deleting Lines: -

You can delete a single line or group of lines using DELETE statement. The system searches the table
using linear search, binary search and hash algorithm for Standard, Sorted and Hashed tables
respectively. If the table contains a NON-UNIQUE key, the first entry is changed.

To delete a single entry of the internal table using the table key, one of the following syntax is used.
DELETE TABLE <itab> FROM <wa>

The above statement deletes a single entry from the internal table using the corresponding table key
from <wa>. The <wa> must be compatible with the line type of <itab>.

DELETE TABLE <itab> WITH TABLE KEY <k1> = <f1>…<kn> = <fn>

The above statement is exactly the same but you have to explicitly define the table key fields.

To delete a group of lines of the internal table that meets certain condition, the following syntax is
used.

DELETE TABLE <itab> WHERE <cond>

To delete the adjacent duplicate entries the following syntax is used.

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab> COMPARING <f1>…<fn> [ALL


FIELDS].

In order to execute this statement successfully, we have to give the following options: -

If you give the COMPARING <f1>…<fn>, the fields <f1> till <fn> are compared with all the lines of
the table, if it founds identical entries, they are deleted.

1. If you give ALL FIELDS, all the fields of the internal table is compared and upon finding
identical entries, they are deleted.
2. By omitting both the options, the key fields of the internal table are compared and upon finding
identical entries, they are deleted.

Move ‘1’ to wa-num.

Move ‘10’ to wa-num1.

Append wa to itab_obj.

Loop at itab_obj.

Write:/ itab_obj-num, itab_obj-num1.

Endloop.
Output

--------

11

12

13

14

15

16

17

18

19

1 10

1 10

2 10

wa-num = 1. wa-num= 2.

delete table itab from wa. ‘ This statement deletes the content in itab

that corresponds to wa

delete adjacent duplicate entries from itab_obj comparing all fields ‘-

------ A

delete adjacent duplicate entries from itab_obj comparing num ‘----- B

The statement A deletes the entries where all the fields are identical in adjacent rows, (i.e.) [1 10 & 1
10]
The statement B deleted all the entries that have identical values in the first field, (i.e.) [Internal Table
itab_obj contains only 2 10]

Reading lines: -

To read a single entry of an internal table of any table type use the following statement.

READ TABLE <itab> <key> <result>

As explained above in order to be valid statement for any type of table, you must use the <key> not
the <index> that you use for INDEX tables.

The key you specify can be either table key or user defined key. First lets look at table key. When you
don’t want specify all the table keys explicitly you can use the following statement.

READ TABLE <itab> FROM <wa>.

Alternatively you can specify the table keys expicitly in the READ statement as follows: -

READ TABLE <itab> WITH TABLE KEY <k1> = <f1> …. <kn> = <fn>.

If you want to specify a different key other than table key, you can use the following statement.

READ TABLE <itab> WITH KEY <k1> = <f1> …. <kn> = <fn>.

When you compare both the READ Statements, you can easily trace out, ‘TABLE’ keyword is
missing. The following program demonstrates the use of all the READ statements.

Struct-empno = ‘0001’.

Read table itab from struct.

Write:/ struct-empno, struct-empname.

Output

----------

0001 Stephen

read table itab with table key empno = ‘0002’


write:/ itab-empno, itab-empname.

Output

--------

0002 Jack

read table itab with key empname = ‘Jason’.

Write:/ itab-empno, itab-empname

Output

---------

0003 Jason

The first output reads from the internal table using the table key from the work area. The only
requirement is work area must be compatible with the line type of the internal table.

The second output reads from the internal table with the table key defined explicitly.

The third output reads from the internal table using a different key defined explicitly but not the table
key.

There is a complicate form of READ statement where you can retrieve from the internal table by
comparing the fields in the work area and internal table and transporting the required fields in to the
same work area. Here you can see that same work area is used to check the entries against the internal
table and used for output as well. The statement uses the following syntax.

READ TABLE <itab> <key> INTO <wa> [COMPARING <f1>…<fn>

ALL FIELDS]

[TRANSPORTING <f1>…

| ALL FIELDS

| NO FIELDS]
As you can see both the comparing and transporting are optional and then it behaves like a normal
read statement. The work area must be compatible with line type of the internal table.

Struct-empno = ‘0003’.

Read table itab struct into struct comparing empno transporting empname

Write:/ struct-empno, struct-empname

Output

---------

0003 Jason

The above statement is not necessary for this table but just for understanding the READ concept. The
work area is filled with ‘0003’ and the statement reads the internal table by comparing the
corresponding entry in the work area and then the ‘empname’ field is transported to the work area.

Processing Table Entries in Loops.

Loop statements are used to process the entries in the Internal Table. There are lots of options in order
to process the required entries in the loop. The syntax is as follows: -

LOOP AT <itab> [INTO <wa>] [WHERE <cond>]

<Statements>

ENDLOOP.

As you can see the INTO <wa> and WHERE <cond> are optional. If you use the loop without any
options, all the entries one-by-one are processed from the internal table. The <wa> must be compatible
with the line type of the internal table.

Generally, the INTO <wa> is used when the internal table is declared without the header line. But
performance wise, it is better to declare the internal table without the header line and declare a
separate work area that is compatible with the line type of the internal table.

WHERE <cond> option is used to avoid processing all the entries in the internal table. Hence the
internal table contents that satisfies the condition will enter the loop and further they are processed
according to the requirements.

LOOP AT <itab> TRANSPORTING NO FIELDS WHERE <cond>

As the statement indicates, none of the internal table fields are transported in to the loop, even the
entries that satisfy the condition. But this statement is used at the high end where you want to find the
number of lines of internal table that satisfies certain condition.
The following program explains each of the statements above clearly,

Data : begin of struct

Roll_no type I,

Name(50) type c,

Mark1 type I,

Mark2 type I,

End of struct.

Data : itab like standard table of struct,

Wa like line of itab,

Itab1 like sorted table of struct with header line,

N type I.

Wa-roll_no = ‘121’.

Wa –name = ‘Wilson’.

Wa –mark1 = ‘80’.

Wa –mark2 = ‘85’

Append wa to itab.

Wa-roll_no = ‘122’.

Wa –name = ‘William’.

Wa -mark1 = ‘82’.

Wa -mark2 = ‘45’
Append wa to itab.

Wa-roll_no = ‘123’.

Wa -name = ‘Thomson’.

Wa -mark1 = ‘76’.

Wa -mark2 = ‘54’

Append wa to itab.

Wa-roll_no = ‘124’.

Wa -name = ‘Daniel’.

Wa -mark1 = ‘87’.

Wa -mark2 = ‘54’

Append wa to itab.

Insert lines of itab from itab1.

Loop at itab into wa.

Write:/ wa-roll_no, wa-name, wa-mark1, wa-mark2.

Endloop,

Output

--------

121 Wilson 80 85

122 William 82 45
123 Thomson 76 54

124 Daniel 87 54

Loop at itab1.

Write:/ itab1-roll_no, itab1-name, itab1-mark1, itab1-mark2.

Endloop.

Output

--------

121 Wilson 80 85

122 William 82 45

123 Thomson 76 54

124 Daniel 87 54

Loop at itab into wa where roll_no = ‘124’.

Write:/ wa-name, wa-mark1, wa-mark2.

Endloop.

Output

--------

124 Daniel 87 54

Loop at itab1 where roll_no = ‘123’.

Write:/ itab1-name, itab1-mark1, itab1-mark2.


Endloop.

Output

--------

123 Thomson 76 54

loop at itab1 transporting no fields where mark2 = ‘54’.

N = n +1 ‘ Initialize n = 0.

endloop.

Output

--------

Control Level Processing

Control Level Processing of an internal table means that you can divide the internal table into
different groups based on certain fields. A very important aspect of the control level processing is
sorting the internal table. The first field of the internal table has the highest control level and so on.
Therefore when you declare the internal table the control levels of all the fields must be known.

Once you declare the internal table defining the correct control levels for the fields the internal table is
sorted by the first field, second field and so on. The control level statement has a structure with AT
and ENDAT with the control levels. The syntax is as shown.

AT <level>

<Statements>

ENDAT.

There are 4 levels defined with the AT statement.


FIRST – First Line of the internal table

LAST – Last Line of the internal table.

NEW <field> - Beginning of a group of entries that has the same contents as <field>

and to the left of <field>

END OF <field> - Ending of a group of entries that has the same contents as <field>

and to the left of <field>

The control level statements are allowed within the loop to make optimum use of this feature. The
program below demonstrates the use of the control levels.

Loop at itab1.

At first

Write:/ itab1-roll_no, itab1-name, itab1-mark1, itab1-mark2.

Endat.

Loop at itab1.

At last

Write:/ itab1-roll_no, itab1-name, itab1-mark1, itab1-mark2.

Endat.

Loop at itab1.

At new mark2

Write:/ itab1-roll_no, itab1-name, itab1-mark1, itab1-mark2.

Endat.
Loop at itab1.

At end of mark2

Write:/ itab1-roll_no, itab1-name, itab1-mark1, itab1-mark2.

Endat.

Output

----------

121 Wilson 80 85

124 Daniel 87 54

123 Thomson 76 54

124 Daniel 87 54

Creating Summarized Internal Tables: -

To create a summate entries in an Internal table COLLECT statement is used. The syntax is as
follows: -

COLLECT <wa> INTO <itab>

The <wa> must compatible with the line type of <itab>. This creates a sum of all numerical fields in
the internal table if the system finds a corresponding entry of table key fields between <wa> and the
table.

If it fails to find an entry, the statement behaves like a normal INSERT statement. The only pre-
requisite to create a summarized internal table is that all the fields that are not part of the table key
must be numerical columns.

Determining the Attributes of Internal Table: -

This sounds a bit strange as we are defining the attributes of the internal table, then why we need a
statement to find the attributes. The internal table we declare is a static definition but during runtime it
gets dynamically assigned. To determine the attributes use the following statement.
DESCRIBE TABLE <itab> [LINES <l>] [OCCURS <n>] [KIND <k>]

The LINES return the number of lines of the internal table. OCCURS returns the value of the INTIAL
SIZE of the internal table and KIND returns the table type of the internal table ‘T’ for standard table,
‘S’ for sorted table and ‘H’ for Hashed table.

Exception of Internal Table: -

APPEND statement is used to create Ranked List for standard tables. In order to create Ranked List,
declare the internal table and use the following statement.

APPEND <wa> to <itab> SORTED by <f>.

After this statement, the contents are not appended to the last line of the internal table but gets sorted
and inserted in the respective order. The main feature of this statement is that it violates that internal
table are dynamic datasets as follows.

The contents of <wa> are inserted in to <itab> as long the number of lines of an internal table does
not exceed <n> (declared in INTIAL SIZE). When it exceeds the line is discarded thereby defining a
static declaration to internal table.

Tips & Tricks: -

• The number of internal tables in a program must be kept minimum as possible


• If you are creating an internal of standard type and going to process small amount of data its
better to declare a internal table directly using DATA statement.
• Try to avoid using internal table with header line; declare a separate work area compatible to
the internal table line type.
• Always try to use the system fields when you processing the internal table entries within the
loop.
• APPEND statement always adds to the last line of the internal table whereas MODIFY changes
entry that is already in the table.
• If you are using internal table within a loop that behaves differently at each pass make sure that
you have cleared the header line or work area.
• If you don’t know the exact value or don’t know the variable till runtime you can address those
values using ‘<variable-name>’.
• Use the appropriate statements for Standard, Sorted and Hashed Tables

System Fields for Internal Tables: -

SY-SUBRC – This is common to all the statements in ABAP. This system field is set to 0 when the
statement is executed successfully else it is set to 4.

SY-TABIX – This system field contain the current line of an internal table. The internal table must of
either Standard or Sorted table. For Hashed table this field is not set. As this field is set only for Index
tables SY-TABIX is set to the index for the following operations.
APPEND: APPEND sets SY-TABIX to the total number of entries in the internal table

(I.e.) index of the last line of the internal table.

COLLECT: COLLECT sets SY-TABIX to the index of the existing or inserted line of the

internal table.

LOOP AT: When the internal table enters loop, SY-TABIX contains the index of the line that enters
loop and when it exits loop SY-TABIX is reset to the index that it had before entering loop.

READ: SY-TABIX is set to the index of the line of the internal table that is read.

The operations DESCRIBE, LOOP AT and READ TABLE sets the following system fields as
follows.

SY-TFILL – contains the number of lines in the internal table

SY-TLENG – contains the length of the lines in the internal table

SY-TOCCU – contains the initial amount of memory allocated to the internal table.

Glossary:

Data Object

Instance of a Data type. It occupies a field in memory

Data Type

Describe the technical attributes of a Data Object

Flat Structure

Structure containing only elementary data types

Hashed Table

One type of an internal table where it is accessed using hash algorithm and hash key. In contrast to
Standard and Sorted tables, searching time does not increase with the size of the table.

Header Line

It’s a work area of an internal table that acts as an interface to the table.

Index Table
Index table uses linear search and an index to access the table. It access time increases with the size of
the table

Internal Tables

A Data Object (either declared directly or declared from the Data Type) that stores the data and
process within the program. You can define three types namely, Standard, Sorted and Hashed Table.

Sorted Table

An Internal table type that is sorted with its specified key

Standard Table

An internal table that is unsorted

Das könnte Ihnen auch gefallen