Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Internal Table
What is an internal table ?
Internal tables (arrays) are data objects that allow you to retain several data
records within the same structure in working memory -- the access time to
read and process the data stored inside the internal tables is significantly less
than a read of the database table
Internal tables have the same general structure as a database table, but are held
in memory, and initially contain no records.
The number of data records stored in an internal table is only restricted by the
capacity limits of the computer system.
An internal table consists of a body and an optional header line.
The header line holds the current line of the table.
Internal tables are used to process large data sets in a structured manner:
Temporarily storing data from database tables for future processing;
Structuring and formatting data for output;
Formatting data for the use of other services.
What is an internal table ?
Defining an Internal Table
Basic ways of defining internal tables with and without header lines.
data: itabsbook like sbook occurs 0. (no header)
report intabex.
tables: customers.
data: begin of itabcust occurs 0,
cnum like customers-cnum,
cname like customers-cname,
end of itabcust.
1)
sort stud_tab.
2)
sort stud_tab by studname, studid.
3)
sort stud_tab by stud_id descending.
Retrieving lines
Once an internal table has been filled, data can be retrieved by reading
each line of the table using a loop, or reading individual lines.
Examples :
1) loop at stud_tab.
write / stud_tab-studid.
write stud_tab-studname.
endloop.
2) loop at stud_tab
where studname = ‘Smith’.
write / stud_tab-studid.
endloop.
The binary search algorithm requires that the table entries are sorted according to
the specified key. This is a much faster way of locating entries in large tables.
Changing an internal table
The contents of a given line in an internal table can be updated using the modify
command. For example :
report ychgtabex.
tables:customers.
data: cust_info like customers occurs 0
with header line.
select * from customers into table cust_info.
sort cust_info by cnum.
read table cust_info
with key cnum = 3456755
binary search.
if sy-subrc = 0.
cust_info-cphone = ‘9654-2345’.
modify cust_info index sy-tabix.
endif.
loop at cust_info.
write: / cust_info-cnum, cust_info-cname.
endloop.
Internal Tables Types
Determines how ABAP accesses the individual entries
Three Types
◦ Standard Tables
◦ Sorted Tables
◦ Hashed Tables
Internal Tables - Standard Tables
By default, all the records that you append to your internal table have a key. This
key is the combination of all non-numeric fields. This is the implicit key.
You can also define your own key for an internal table. You would add WITH
KEY FIELD1 FIELD2 … etc. to your DATA statement.
More specifically, you can make your user-defined key:
UNIQUE: additional records with the same key would not be permitted
NON-UNIQUE: additional records with the same key would be permitted
Standard tables can have:
NON-UNIQUE KEY (same as just plain KEY), or
no user-defined key at all (more likely)
Indexing is used more than keys when it comes to reading standard internal
tables.
Loading an Internal Table with a
Header Line
EMPLOYEE
ID NAME1 COUNTRY
B Header Line
Internal Table with Header Line
1 SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE
TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
1 EMPLOYEE
ID NAME1 COUNTRY
Header Line
Internal Table with Header Line
1 SELECT * FROM EMPLOYEE.
2 MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
1 EMPLOYEE
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line
Internal Table
1
2
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. with Header
3 APPEND EMPTAB.
ENDSELECT. Line
EMPLOYEE
1
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line
5
ID NAME1 COUNTRY
00000002 Diversified Indust... USA Header Line
3
. .
. .
. .
10
Creating an Internal Table without a
Header Line
REPORT Y170DM40. The TYPES statement defines
the structure and data type
TABLES: EMPLOYEE.
for the internal table and its
TYPES: BEGIN OF EMP, work area
ID LIKE EMPLOYEE-ID,
The DATA statement with an
NAME1 LIKE EMPLOYEE-NAME1,
INITIAL SIZE creates the
COUNTRY LIKE EMPLOYEE-COUNTRY, actual internal table without a
END OF EMP. header line. The DATA
statement without the
INITIAL SIZE creates the work
DATA: EMPTAB TYPE STANDARD TABLE
area for the internal table.
OF EMP INITIAL SIZE 10,
EMPTAB_WA TYPE EMP. Work Area
ID NAME1 COUNTRY
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB_WA.
APPEND EMPTAB_WA TO EMPTAB.
ENDSELECT.
APPEND <work area> to <EMPTAB>.
Internal Table without a Header Line
WHY???
Why would we choose to use an internal table without a header line when it is
easier to code one with a header line?
Separate Internal Table Work Area: The work area (staging area) defined for
the internal table is not limited to use with just one internal table.
Suppose you want two internal tables for EMPLOYEE – one to contain all
records and one to contain only those records where country = ‘USA’. You
could create both of these internal tables without header lines and use only
one work area to load data into both of them. You would append all records
from the work area into the first internal table. You would conditionally
append the ‘USA’ records from the same work area into the second
internal table.
Performance Issues: Using an internal table without a header line is more
efficient than one with a header line
Nested Internal Tables: If you want to include an internal table within a
structure or another internal table, you must use one without a header line.
Internal Table without a Header Line
A TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
ID LIKE EMPLOYEE-ID,
NAME1 LIKE EMPLOYEE-NAME1,
COUNTRY LIKE EMPLOYEE-COUNTRY,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 ,
B EMPTAB_WA TYPE EMP.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB_WA.
APPEND EMPTAB_WA TO EMPTAB.
ENDSELECT.
EMPLOYEE
ID NAME1 COUNTRY
B Work Area
1 SELECT * FROM EMPLOYEE. Internal Table without a
Header Line
2 MOVE-CORRESPONDING EMPLOYEE TO
EMPTAB_WA.
3 APPEND EMPTAB_WA TO EMPTAB.
ENDSELECT.
1 EMPLOYEE
ID NAME1 COUNTRY
2
00000001 Baker Distributors USA Work Area
ID NAME1 COUNTRY
3 00000001 Baker Distributors USA 1 This work area
is not attached
2 to the body of
the internal
3
. table.
.
.
10
Transferring ABAP Dictionary Table
Structures
REPORT Y170DM41.
TABLES: EMPLOYEE.
DATA: EMPTAB LIKE STANDARD TABLE OF
EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.
The internal table EMPTAB
will have the exact same
SELECT * FROM EMPLOYEE. structure as the dictionary
MOVE EMPLOYEE TO EMPTAB. table EMPLOYEE.
APPEND EMPTAB.
ENDSELECT.
The above code loads the EMPLOYEE data into the internal table
EMPTAB.
Automatic Field Conversion
MOVE-CORRESPONDING or MOVE field to field
◦ Individual field type conversion
MOVE
◦ Structure to structure
◦ Field to structure
◦ Structure to field
Intermediate C type
Followed by adoption of new types
When a MOVE-CORRESPONDING is performed, type conversion occurs automatically for the individual
fields. The process is carried out on a field-by-field basis. MOVE specified field to specified field behaves
the same way.
However, the following three scenarios are handled similarly to each other:
when you MOVE a structure to a structure of a different definition
MOVE a field to a structure
a structure to a field
In these case, the data would be converted to one long character string (type C) first, then conversion
would take place.
If a piece of data is moving to a longer space in the new structure, it will be padded with spaces or zeroes
according to its data type. Moving into a shorter length would cause truncation.
Structures with an internal table included as a component do not follow the typical rules.
Mass Reading from Database Tables
into Internal Tables
REPORT Y170DM69.
SELECT * FROM <table> . . .
TABLES: EMPLOYEE. 1. INTO TABLE <EMPTAB>.
2. APPENDING TABLE
DATA: EMPTAB LIKE STANDARD TABLE EMPLOYEE <EMPTAB>.
INITIAL SIZE 10 WITH HEADER LINE.
Performance Tips:
Looping at internal tables for processing is more efficient than processing within
SELECT … ENDSELECT loops. The following is even more efficient than nested
loops:
Performance Tips:
COLLECT can be very CPU intensive - when using an internal table with > 50
entries.
Sorting an Internal Table
REPORT Y170DM44.
TABLES: EMPLOYEE. Sorting options:
TYPES: BEGIN OF EMP,
1) SORT <EMPTAB> - sorts
COUNTRY LIKE EMPLOYEE-COUNTRY,
the entries of the internal
NAME1 LIKE EMPLOYEE-NAME1, table <EMPTAB> in
SALES LIKE EMPLOYEE-SALES, ascending order.
END OF EMP.
2) SORT <EMPTAB> BY
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE.
<field> - sorts the table on
one or more fields within
SELECT * FROM EMPLOYEE.
the table.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
SORT EMPTAB BY SALES DESCENDING. screen output
LOOP AT EMPTAB.
WRITE: / ITAB-COUNTRY, ITAB-NAME1, ITAB-SALES.
ENDLOOP.
Sorting an Internal Table
An internal table is sorted using the SORT <internal table> statement. If sort criteria
is not specified, the table is sorted by all fields (except those of data types P, I, and F)
in ascending order in the sequence in which they were declared.
With the additional specifications BY <field name> and ASCENDING or
DESCENDING, you can restrict the sort process to specific fields (or to fields of type
P, I, F) and can determine the sorting sequence and hierarchy.
Where possible, the sort process should be limited by using the BY <field name>
parameter. ABAP then requires less storage space in the roll area for the sorting
process.
The sort process is the same whether or not the internal table has a header line.
You have the option of using a sorted table instead of a standard table. Then the
entries will be sorted automatically on their way from the header line into the internal
table.
Performance Tips:
It is advisable to always specify the fields to be sorted by rather than just SORT.
Reading a Single Table Entry
REPORT Y170DM47.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMPTAB.
READ TABLE ….
Reading a Single Table Entry
The READ TABLE <internal table> statement reads a single table entry.
When using an internal table without a header line, the syntax of the READ TABLE
statement changes to: READ TABLE <internal table> INTO <work area>.
If an entry was found with the READ statement:
◦ SY-SUBRC is set to zero,
◦ SY-TABIX is set to the line number of the entry read,
◦ the table entry read is placed into the internal table header line or work area.
If an entry was not found with the READ statement:
◦ SY-SUBRC is set to a non-zero number,
◦ SY-TABIX is undefined,
◦ the internal table header line or work area remains unchanged.
When you perform a LOOP AT <internal table> … ENDLOOP, the effect is the same
as a READ TABLE <internal table> INDEX <i> where <i> starts at one and
continues incrementing until the entire internal table is read. In other words, with
each loop pass, SY-TABIX is set to the line number of the entry read and the
header line or work area contains the data from the entry read.
Reading a Single Table Entry -
Options
READ TABLE <EMPTAB> options:
1) READ TABLE <EMPTAB>.
2) READ TABLE <EMPTAB> WITH KEY <k1> = <v1>…
<kn> = <vn>.
3) READ TABLE <EMPTAB> WITH TABLE KEY <k1> = <v1> ...
<kn> = <vn>.
4) READ TABLE <EMPTAB> WITH KEY = <value>.
5) READ TABLE <EMPTAB> WITH KEY . . . BINARY SEARCH.
6) READ TABLE <EMPTAB> INDEX <i>.
7) READ TABLE <EMPTAB> COMPARING <f1> <f2> . . . .
8) READ TABLE <EMPTAB> COMPARING ALL FIELDS.
9) READ TABLE <EMPTAB> TRANSPORTING <f1> <f2> . . . .
10) READ TABLE <EMPTAB> TRANSPORTING NO FIELDS.
Reading a Single Table Entry -
Options
READ TABLE <internal table>.
◦ The contents of the header line determine the table entry to be read. ABAP searches for the
first entry in the internal table that matches the header line’s search term which consists of
all non-numeric fields with contents not equal to space.
READ TABLE <internal table> WITH KEY ‘<key>‘.
◦ Enter the search argument after the parameter KEY (in single quotes).
◦ Beginning with the first character of the first field of the first entry in the table, ABAP
compares each record character by character with the search argument (‘<key>‘).
READ TABLE <internal table> WITH KEY ‘<key>‘ BINARY SEARCH.
◦ Like variant 2, but using a binary search (faster than linear search).
◦ The internal table must be sorted in ascending order by the
search argument.
READ TABLE <internal table> INDEX <i>.
◦ The i-th table entry is read.
For information on the other READ options available, see Online Help.
Performance Tips:
A BINARY SEARCH should always be used whenever possible, but the table
MUST be sorted first.
Maintaining Internal Tables
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO
EMPTAB.
APPEND EMPTAB. INSERT <EMPTAB> INDEX <i>.
ENDSELECT. MODIFY <EMPTAB> INDEX <i>.
DELETE <EMPTAB> INDEX <i>.
READ TABLE EMPTAB INDEX 1.
MOVE ‘ABC’ TO EMPTAB-NAME1.
MODIFY EMPTAB INDEX SY-TABIX.
IF SY-SUBRC NE 0.
WRITE / ‘Attempt to modify failed.’.
Check SY-SUBRC after
ELSE.
every attempt to
WRITE: / EMPTAB-COUNTRY, EMPTAB- change an internal table
NAME1.
entry.
ENDIF.
INSERT EMPTAB INDEX 1.
DELETE EMPTAB INDEX SY-TABIX.
Maintaining Internal Tables
The INSERT <internal table> INDEX <i> statement generates a new table entry
before line <i> with the contents of the header line. If the table <EMPTAB> has no
entries, the contents of the header line are added to the table.
The MODIFY <internal table> INDEX <i> statement overwrites table line <i> with the
contents of the header line. Line <i> must already exist.
The DELETE <internal table> INDEX <i> statement deletes table line <i>.
Within a LOOP AT <internal table> … ENDLOOP, you can make changes to an
internal table. The line affected is always the current line (SY-TABIX).
◦ INSERT <internal table>:
A new line with the contents of the header line is inserted before the current line.
◦ MODIFY <internal table>:
The current line is overwritten by the contents of the header line.
◦ DELETE <internal table>:
The current line is deleted.
Check SY-SUBRC after every attempt to change a table entry. If the change was
successful, SY-SUBRC will be set to zero.
Performance Tips:
INSERT/MODIFY/DELETE <internal table> WHERE .... is more efficient than first
LOOPing AT the table using the WHERE clause, then maintaining the tables header
line.
Working with an Internal Table
without a Header Line
APPEND <work area> TO <internal table>.
Performance Tips:
Always use DESCRIBE to find out how many entries there are in
an internal table - it is much more efficient than LOOPing AT the internal
table and incrementing a counter.