|
|
ERPGenie.COM -> SAP Technical -> ABAP -> Tips and Tricks -> Internal Tables by Jayaprakash.A.N. 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 ABAPers like me.
Believe this document will provide a clear understanding of Internal Table.
Happy ABAPing. Introduction
..
3 Internal Tables as Data Types
..
4 Internal Tables as Data Objects
..
6 Initializing Internal Tables
6 Sorting Internal Tables
..
10 Reading Table Lines
. 14 Changing Table Lines
.
15 Deleting Table Lines
.
18 Changing Table Lines
. 19
Processing Table Entries in Loops
.
21 Control Level Processing
..
23 Determining Internal Table
Attributes
26 Exception for Internal Table
26 Internal Tables System Fields
28 Glossary
29 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.
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.
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.
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.
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.
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 its the body
then itab[] is used if its 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
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, 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.
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
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.
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 lines 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>.
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>.
The above program reads a
single entry from internal table (itab) with index 2. 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.
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.
|