internal tables described
TRANSCRIPT
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 1/46
Internal Tables Described
Top of Form
User Rating: / 31
Poor Best
Rate vote 0 com_content 62
104 /abaptips/index.p
Bottom of FormWritten by Jayaprakash.A.N.Sunday, 04 February 2007
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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 2/46
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 processingwithin 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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 3/46
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 4/46
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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 5/46
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 t he entire line can be defined as a key. The syntax is as shown.
WITH UNIQUE|NON-UNIQUE KEY TABLE LINE
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 6/46
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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 7/46
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 DATAstatement 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. Internaltables 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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 8/46
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 9/46
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 processedseparately 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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 10/46
First let us discuss the operations that favor the entire internal table
processing.
<!--[if !supportLists]-->1. <!--[endif]-->Initializing Internal Tables: - There arethree 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 clearthe 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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 11/46
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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 12/46
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
--------
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
2 10
Now the internal table ‘itab_obj’ has values as shown above. Alternatively
you can use equal statements as well.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 13/46
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 theinternal 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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 14/46
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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 15/46
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
9 1
The second write statement may output
10 2
10 1
9 1
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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 16/46
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 17/46
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,
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 18/46
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.
<!--[if !supportLists]-->2. <!--[endif]-->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:
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 19/46
INSERT <line> INTO <itab> [INDEX <index>]
The <line> can be a work area that is either compatible or convertible tothe <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>.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 20/46
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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 21/46
Output
---------
0001 Stephen0002 Jack
0003 Jill
The above program shows the demonstration of both the insert statements
for a sorted table with unique key.
<!--[if !supportLists]-->3. <!--[endif]-->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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 22/46
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 searchand 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’).
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 23/46
‘ 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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 24/46
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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 25/46
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.
<!--[if !supportLists]-->4. <!--[endif]-->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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 26/46
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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 27/46
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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 28/46
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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 29/46
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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 30/46
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 hashalgorithm 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 definethe table key fields.
To delete a group of lines of the internal table that meets certain condition,
the following syntax is used.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 31/46
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: -
1. 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.
2. If you give ALL FIELDS, all the fields of the internal table is compared and upon finding identical entries, they are deleted.
3. 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
--------
1 1
1 2
1 3
1 4
1 5
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 32/46
1 6
1 7
1 8
1 91 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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 33/46
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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 34/46
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 cansee 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>
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 35/46
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 empnotransporting 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. Thereare 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>
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 36/46
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 fromthe 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,
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 37/46
End of struct.
Data : itab like standard table of struct,
Wa like line of itab,
Itab1 like sorted table of struct with headerline,
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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 38/46
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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 39/46
--------
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
--------
2
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 firstfield 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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 40/46
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 tableLAST – 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.
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 41/46
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: -
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 42/46
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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 43/46
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 en tries 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 w ithin a loop that behaves differently at each pass make sure that you have cl eared 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
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 44/46
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 theinternal 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 followingsystem 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 internaltable.
Glossary:
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 45/46
8/3/2019 Internal Tables Described
http://slidepdf.com/reader/full/internal-tables-described 46/46
Sorted TableAn Internal table type that is sorted with its specified key
Standard TableAn internal table that is unsorted