Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
com/channel/UC-DphVXZOSTApLu3fzBe-Ww
https://www.youtube.com/watch?v=JeTjVIvA-CA
Re: Inserting duplicate entries into table
VR V Steel
VR V Feb 8, 2013 8:51 AM (in response to Silviya Thomas)
Hi Thomas,
For eg: Consider a database table ZEMPLOYEE and assume that this table has prima
ry key field Employee_ID.
If you want to insert all the values from you internal table to database table i
rrespective of duplicate add field counter which will be the part of primary key
field and insert.
Now primary key for this ZEMPLOYEE table will be EMP_ID and Counter fields.
Emp_ID
COUNTER
Emp_name
1000
1000
Emp_COUNTRY
ABC
ABC
India
India
http://www.erpgenie.com/sap/abap/exa
ternal 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 documentatio
n on ABAP/4 for young ABAP ers like me. Believe this document will provide a clear
understanding of Internal Table. Happy ABAPing.
Introduction
.. 3
.. 4
.. 6
. 8
. 8
.. 10
12
13
. 14
. 15
16
. 17
. 18
. 19
. 21
.. 23
25
26
26
27
28
29
Introduction: -
Internal Tables are local tables within a program containing a series of lines h
aving same data type. ABAP Open SQL allows single field, range of fields, entire
database table or view into an Internal table.
Internal tables are used for fetching large volume of data from the database, st
oring in ABAP working memory line-by-line and processing within a program.
Although Internal tables are declared with the other data objects, at runtime th
ey behave as dynamic objects (i.e.) no need to specify the size of the object bu
t only the length of a row in internal table is fixed. The number of rows is det
ermined 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. Gener
ally the data type will be a structure and each component of a structure is a co
lumn in this local table.
Key: - Key is used to identify table rows. You may specify whether the key is UN
IQUE or NON-UNIQUE. As the name indicates UNIQUE key cannot contain duplicate en
tries whereas NON-UNIQUE can.
Table Type: - Table type specifies the behavior of Internal table while accessin
g 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 fo
r index access increases logarithmically whereas by key access, it is proportion
al 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 standa
rd 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 incre
ases logarithmically with the number of entries. The key of a Sorted table can b
e 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 sequenti
al 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 th
e 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 th
e table is based on the key and for processing large volume of data, hashed tabl
e is the apt one.
Creating Internal Tables: -
With all other local types internal table is declared using TYPES statement. The
syntax is as follows: -
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 m
ust 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
ANY TABLE - For creating a fully generic table where the common operation key a
ccess is only allowed
Data types declared using Generic type must be used for field symbols or interfa
ce parameters for routines. For an data type if INDEX TABLE is specified only st
andard and sorted tables must be passed to the field symbols or interface parame
ters, 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 param
eters but the behavior of all the table will be same (i.e.) field symbols and in
terface 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
SORTED TABLE
HASHED TABLE
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.
UNIQUE specifies the mentioned column cannot contain any duplicate entries where
as the NON-UNIQUE specifies the other way.
In Structured Line type the <coli> belong to key if its not anyway related to in
ternal table or references. Key fields can be Nested Structures and are expanded
as the corresponding fields are accessed. The syntax is as shown
In an elementary line type the entire line can be defined as a key. The syntax i
s as shown.
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 intern
al table whose line type is an internal table the default key is empty.
For an internal table specifying the key is not mandatory, if the key is not spe
cified the system defines an arbitrary key.
With the above addition you can specify the initial memory by specifying the num
ber of lines of an internal table. Often, you cannot be sure of the number of li
nes 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 in
ternal table, and once its full, the system allocates twice as much of memory al
located initially up to 8KB and upon crossing the 8KB limit it allocates 12KB ea
ch to the memory of the internal table.
In order to reserve initial size and at the same time avoiding excessive usage o
f memory, the value of <n> can be assigned to the quotient of 8KB divided by the
length of a line in internal table.
The above table defines an internal table as a fully specified data type. All th
ree internal tables have been initially allocated 10 lines, but the way they acc
ess the individual entries is different. itab , is a standard table with default ke
y (as <line-type> is elementary data type, the default key is the entire line). s
ort_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 Gene
ric Tables that are used for passing to Field Symbols and interface parameters r
outines.
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 procedure
s 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 p
rogram.
With all the other data objects internal table is declared using the DATA statem
ent with the LIKE or TYPE addition. The syntax for both is as follows:
The above syntax is used to create an internal table object where the LIKE addit
ion refers to the existing table object within the program.
The above syntax is used to create an internal table object where the TYPE addit
oin refers to the type defined within the program using the TYPES statement or t
ype defined in the ABAP Dictionary.
In contrast to the internal table declaration using the TYPES statement, DATA st
atement does not allow to define to generic internal types. Internal tables decl
arations 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. A
s 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 i
s termed as work area.
Before explaining the importance of Header line, let me make clear how the inter
nal table is accessed.
The systems perspective to the internal table is header of the internal table an
d then body of the internal table.
Header is nothing but a single row of the internal table components. When a syst
em 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 manipulat
ions.
The header of the internal table can be declared in two ways. One with the inter
nal table declaration and other declaring separately using the DATA statement as
shown.
The above syntax declares internal table with the header line. As you can see th
e header line and the body is declared in the statement and so it has the same n
ame. They are differentiated as follows. <itab[]> addresses the body of the head
er line and <itab> address the header of the internal table.
The above syntax declared internal table and header line separately hence has di
fferent names. Now a day its better to define the header line separately in orde
r 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 cl
ear the contents of the work area or the header line at certain points in the pr
ogram.
Header line,
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 hea
der line. As mentioned before, in latter case both internal table and header lin
Note: - If you are using internal table with header lines they are processed sep
arately because both share the same name. The body of the internal table is deno
ted by <itab>[] and header line of the internal table is denoted by <itab>. If t
he work area or header line is declared separately, then they can process with t
heir own names.
First let us discuss the operations that favor the entire internal table process
ing.
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 oc
cupied 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 b
ody of the internal table use <itab>[] and to clear the header of the internal t
able use <itab> as shown in Table 3.
clear : itab_obj1,
Itab_obj1[]
REFRESH <itab>.
This statement always the clear the body of the internal table, header of the in
ternal table cannot be accessed. As with CLEAR statement, the memory remains all
ocated.
refresh itab_obj1
FREE <itab>.
This statement always applies to the body of the internal table, header line can
not be accessed. But if you want to really release the memory allocated to the i
nternal table this statement is used. But the memory assigned to the header line
remains allocated.
free itab_obj1
An Internal table can be assigned to another internal table if they are compatib
le and convertible. The entire contents of one internal table are assigned to th
e other. When you are using Internal table with header line Only concern is whet
her you are processing the body or the work area of the internal table, if it s th
e 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 varia
bles. Alternatively you can use the = statement as follows.
Work Area
<itab1> = <itab2>
<itab1[]> = <itab2>
n =0.
do.
n = n +1.
move
to wa-num.
move n to wa-num1.
move wa to itab_obj.
append itab_obj.
while n <=10.
move 2
move 10
to wa-num.
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
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, t
hen 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 identif
ies the condition false.
If you want to sort a standard or hashed table using its table key (defined at t
he time of declaration), the following syntax applies:
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 spe
cified at the time of declaration.
If you want to sort a standard or hashed table using different key the following
syntax applies:
ASCENDING|DESCENDING addition: -
As the name implies, this addition is used to sort the fields either in ascendin
g 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 ty
pe 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 oft
en. In order to have a stabilized order this addition is used so that the sort o
rder does not change.
sort itab_obj
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 fi
rst and second output. But with addition STABLE you always have the same order d
oesn t matter how many times you sort.
Operations for Index Tables: - The following operations are allowed only for Ind
ex tables (i.e.) Standard and Sorted Tables. Of all the three tables listed in t
his 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 co
pied 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.
As mentioned before, it is always better to declare work area and internal table
separately in terms of performance.
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 th
e 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 li
ne declaration of the internal table
Data:
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 a
s shown. This statement works the same way as the above but better in performanc
e.
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 th
is command for standard table, APPEND is considered to be the best in terms of p
erformance.
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:
The <line> can be a work area that is either compatible or convertible to the <l
ine 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 nu
mber of lines of an internal table is equal to <index> - 1, the <line> is insert
ed 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>.
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>.
struct-empno =
0001 . struct-empname =
Stephen .
Jack .
Loop at itab.
Write:/ itab-empno, itab-empname.
Endloop.
Output
--------0001 Stephen
0002 Jack
0003 Jill
Loop at jtab.
Write:/ jtab-empno, jtab-empname.
Endloop.
Jill .
Output
--------0001 Stephen
0002 Jack
0003 Jill
The above program shows the demonstration of both the insert statements for a so
rted table with unique key.
3.
In addition to inserting, lines from the local tables can be read using READ sta
tement. The syntax is as follows.
The system reads the line with the <index> from the table <itab>.
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 NO
N-UNIQUE key, the first entry is changed.
To change a single line of the local table without the condition the following s
yntax is used.
The <wa> must be compatible with the <line type> defined (declared with the inte
rnal 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.
The <wa> must be compatible with the <line type> defined (declared with the inte
rnal table). It searches for the contents to be changed and contains the new con
tents as well. All the lines of the internal table that satisfies the condition
is changed.
Jason
0003 ).
Output
---------
0001 Stephen
0002 Jack
0003 Jason
The above program demonstrates the use of MODIFY Statement. As struct is compatibl
e with the internal table (itab) line type, the internal table is searched for t
he 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 st
atement.
The above statement deletes the line from the internal table <itab> that corresp
onds 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 manipulatio
n is carried implicitly using SY-TABIX.
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 firs
t line till <n2>. Likely If you do not specify TO <n2> the system deletes all li
nes 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
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 fiel
d is less than 2, (i.e.) the first entry of the internal table.
You can process an internal within a loop using the index. The syntax is as foll
ows.
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 exe
cuted 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 orde
r to improve the performance.
The operation listed applies to any table type, but if you know the table type i
ts better to use the apt command for that table type. For instance, you can appe
nd 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 th
is command for standard table, APPEND is considered to be the best in terms of p
erformance.
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:
The <line> can be a work area that is either compatible or convertible to the <l
ine 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>.
The above statement inserts the lines from <itab1> to <itab2> line by line like
the above INSERT statement.
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 keyw
ord in most of the statements.
If it s a Standard table, the line is appended to the end of the internal table, i
f its sorted table, the line is inserted according to the table key and for Hash
ed table, the line is inserted according to the hash algorithm.
struct-empno =
0001 . struct-empname =
Stephen .
Jack .
Jill .
Loop at itab.
Write:/ itab-empno, itab-empname.
Endloop.
Output
--------0001 Stephen
0002 Jack
0003 Jill
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 so
rted table with unique key. It is same as example demonstrated for INSERT statem
ent 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 NO
N-UNIQUE key, the first entry is changed.
To change a single line of the local table without the condition the following s
yntax is used.
The <wa> must be compatible with the <line type> defined (declared with the inte
rnal 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 inte
rnal table). It searches for the contents to be changed and contains the new con
tents as well. All the lines of the internal table that satisfies the condition
is changed.
Jason
0003 ).
Output
--------0001 Stephen
0002 Jack
0003 Jason
The above program demonstrates the use of MODIFY Statement. As struct is compatibl
e with the internal table (itab) line type, the internal table is searched for t
he entries that are compatible with the work area and they are modified. It is s
ame as example demonstrated for INSERT statement for INDEX tables. In order to d
ifferentiate that only TABLE key is included in INSERT statement for ALL TABLES op
erations, I have given the same example.
Deleting Lines: -
You can delete a single line or group of lines using DELETE statement. The syste
m searches the table using linear search, binary search and hash algorithm for S
tandard, Sorted and Hashed tables respectively. If the table contains a NON-UNIQ
UE key, the first entry is changed.
To delete a single entry of the internal table using the table key, one of the f
ollowing syntax is used.
The above statement deletes a single entry from the internal table using the cor
responding table key from <wa>. The <wa> must be compatible with the line type o
f <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 ta
ble key fields.
To delete a group of lines of the internal table that meets certain condition, t
he following syntax is used.
DELETE ADJACENT DUPLICATE ENTRIES FROM <itab> COMPARING <f1> <fn> [ALL FIELDS].
If you give the COMPARING <f1> <fn>, the fields <f1> till <fn> are compared wi
th all the lines of the table, if it founds identical entries, they are deleted.
If you give ALL FIELDS, all the fields of the internal table is compared and
upon finding identical entries, they are deleted.
By omitting both the options, the key fields of the internal table are compa
red and upon finding identical entries, they are deleted.
Move 1
Move 10
to wa-num.
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
1 6
1 7
1 8
1 9
1 10
1 10
2 10
wa-num = 1. wa-num= 2.
delete table itab from wa.
------ A
delete adjacent duplicate entries from itab_obj comparing num
----- B
The statement A deletes the entries where all the fields are identical in adjace
nt 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.
As explained above in order to be valid statement for any type of table, you mus
t 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 u
se the following statement.
Alternatively you can specify the table keys expicitly in the READ statement as
follows: -
. <kn> = <fn>.
If you want to specify a different key other than table key, you can use the fol
lowing statement.
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 statemen
ts.
Struct-empno = 0001 .
Read table itab from struct.
Write:/ struct-empno, struct-empname.
Output
---------0001 Stephen
Output
-------0002 Jack
Output
---------
Jason .
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 o
f the internal table.
The second output reads from the internal table with the table key defined expli
citly.
The third output reads from the internal table using a different key defined exp
licitly but not the table key.
There is a complicate form of READ statement where you can retrieve from the int
ernal table by comparing the fields in the work area and internal table and tran
sporting the required fields in to the same work area. Here you can see that sam
e work area is used to check the entries against the internal table and used for
output as well. The statement uses the following syntax.
As you can see both the comparing and transporting are optional and then it beha
ves like a normal read statement. The work area must be compatible with line typ
e 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 t
he READ concept. The work area is filled with 0003 and the statement reads the int
ernal table by comparing the corresponding entry in the work area and then the em
pname field is transported to the work area.
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 synta
x is as follows: -
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 w
ithout 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.
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 tab
le that satisfies certain condition.
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 =
Wa -name
123 .
= Thomson .
Wa -mark1 = 76 .
Wa -mark2 = 54
Append wa to itab.
Wa-roll_no =
Wa -name
124 .
= Daniel .
Wa -mark1 = 87 .
Wa -mark2 = 54
Append wa to itab.
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
124 .
Output
-------124 Daniel 87 54
123 .
Output
-------123 Thomson 76 54
Initialize n = 0.
endloop.
Output
-------2
Control Level Processing of an internal table means that you can divide the inte
rnal table into different groups based on certain fields. A very important aspec
t 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 y
ou 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 l
evels. The syntax is as shown.
AT <level>
<Statements>
ENDAT.
FIRST
LAST
NEW <field> - Beginning of a group of entries that has the same contents as <fie
ld>
and to the left of <field>
END OF <field> - Ending of a group of entries that has the same contents as <fie
ld>
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: -
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 entr
y of table key fields between <wa> and the table.
If it fails to find an entry, the statement behaves like a normal INSERT stateme
nt. 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 tabl
e, then why we need a statement to find the attributes. The internal table we de
clare is a static definition but during runtime it gets dynamically assigned. To
determine the attributes use the following statement.
The LINES return the number of lines of the internal table. OCCURS returns the v
alue 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.
After this statement, the contents are not appended to the last line of the inte
rnal table but gets sorted and inserted in the respective order. The main featur
e 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 t
he line is discarded thereby defining a static declaration to internal table.
Tips & Tricks: -
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 e
ntries 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 ea
ch 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 ca
n 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 in
ternal table must of either Standard or Sorted table. For Hashed table this fiel
d 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 tabl
e
(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 tha
t it had before entering loop.
READ: SY-TABIX is set to the index of the line of the internal table that is rea
d.
The operations DESCRIBE, LOOP AT and READ TABLE sets the following system fields
as follows.
SY-TFILL
SY-TLENG
SY-TOCCU
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 increas
e 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 sto
res 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