Вы находитесь на странице: 1из 57

Sesi 8

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)

 data: itabsbook like sbook occurs 0


with header line. (header)
 data: begin of itabsbook occurs 0,
f1,
f2,
f3,
end of itabsbook. (header)

 data: itabsbook type table of sbook,


wa_sbookinfo like line of itabsbook.
(user-defined work area in place of header)
Filling an internal table

Tables can be filled with data by:


Reading data from a database table e.g.
tables: vendor.
data vendtab like vendor occurs 0
with header line.
select * from vendor into table vendtab.
Or
select * from vendor appending table vendtab.

Appending lines e.g.


data: begin of vendtab occurs 0,
no(6) type n,
name(20) type c,
end of vendtab.
select * from vendor.
vendtab-no = vendor-no.
vendtab-name = vendor-name.
append vendtab.
endselect.
Filling an internal table – Example 1

A table customers has the following fields :


 cnum customer number
cname customer name
caddress customer address
cphone customer phone no.
report intabex.
tables: customers.
data: begin of itabcust occurs 0,
cnum like customers-cnum,
cname like customers-cname,
end of itabcust.

select * from customers.


move-corresponding customers to itabcust.
append itabcust.
endselect.
Filling an internal table – Example 2

A table customers has the following fields :


 cnum customer number
cname customer name
caddress customer address
cphone customer phone no.

report intabex.
tables: customers.
data: begin of itabcust occurs 0,
cnum like customers-cnum,
cname like customers-cname,
end of itabcust.

select cnum cname from customers


into table itabcust.
Filling an internal table (cont’d)

Inserting lines at a specified position.


Single lines or a block of lines can be inserted in a table before a specified line number.
All subsequent entries are moved down.
Examples :
insert vendtab index 3.
The contents of the header line will be inserted before line 3.

insert lines of new_vend


from 2 to 5
into vendtab index 3.
Lines 2 to 5 of the internal table new_vend will be inserted before line 3 of vendtab.
Filling an internal table (cont’d)

Moving complete tables


An internal table can be filled with the contents of another one in a single step by
using the move command.
Examples :
move new_vend to vendtab.
Note :
If an internal table with a header line is involved, this header line (but not the internal table
itself) is copied by move.
Sorting an Internal Table

Data in an internal table can be sorted in a number of ways:


Examples:
data stud_tab like student occurs 10.

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.

During the loop the current line number is held in sy-tabix


Retrieving lines (cont’d)

3) read table stud_tab index 5.


if sy-subrc = 0.
write / stud_tab-studid.
else.
write / ‘Record not found’.
endif.

4) read table stud_tab If this read is


with key studid = 3064537. successful, the index of
if sy-subrc ………. the line is returned in
sy-tabix.
5) read table stud_tab
with key studname = ‘Smith’
course = ‘BGCC’.

6) read table stud_tab with key studid


= 3165432 binary search.

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 :

read table stud_tab


with key studid = 3354631.
if sy-subrc = 0.
stud_tab-course = ‘BBBC’.
modify stud_tab index sy-tabix.
endif.
Lines can also be inserted into a table and deleted from a table.
Changing an internal table - 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

 Have an internal linear index

 Records are accessed by index or keys

 The response time for key access is proportional to


number of entries

 The key is always non-unique


Internal Tables - Sorted Tables

 Always saved sorted by the key

 Have an internal index

 Records can be accessed by table index or key

 Uses Binary Search for access

 The response time for key access is logarithmically proportional to the


number of table entries

 Key can be Unique or non-Unique


Internal Tables - Hashed Tables

 Has no linear index

 Only accessed using its key

 Key must be Unique

 Uses Hash Algorithm for accessing records

 Response time is constant


Creating an Internal Table
with Header Line
REPORT Y170DM38. The TYPES statement defines
TABLES: EMPLOYEE. the structure and data type
for the internal table.
TYPES: BEGIN OF EMP,
ID LIKE EMPLOYEE-ID, The DATA statement with an
NAME1 LIKE EMPLOYEE-NAME1, INITIAL SIZE creates the
actual internal table capable
COUNTRY LIKE EMPLOYEE-COUNTRY,
of storing data. Because of
END OF EMP. the WITH HEADER LINE
addition, this internal table is
DATA: EMPTAB TYPE STANDARD TABLE created with a header line.
OF EMP INITIAL SIZE 10 WITH HEADER LINE. ID NAME1 COUNTRY
Header Line
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
Create Internal Table
 An internal table type is defined using the TYPES
statement. No memory is allocated when defining a type.
 An internal table object is created with the DATA statement
by referring to an internal table type using the TYPE
parameter. It is this DATA statement occupies memory.
Internal table objects can also be created through a reference
to an existing table or structure using the LIKE parameter.
 The internal table object must have an INITIAL SIZE <n>
parameter to indicate the initial number of table lines for
which memory will be allocated.
 The above code creates a standard internal table EMPTAB of
structure EMP, and then loads the employee data from table
work area EMPLOYEE into the internal table header line of
EMPTAB, and then appends to the body of EMPTAB.
Internal Table Keys
 Implicit Key
◦ All character fields
 Explicit Key
◦ User-defined
 e.g. … WITH [ UNIQUE/NON-UNIQUE ] KEY FIELD1 FIELD2
...

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

APPEND <int. table>. APPEND <int. table>


SORTED BY <field>.
Department Salary Department Salary Header
R&D 400,000 R&D 400,000
1 PROD 7,800,000
MKTG 1,000,000
2
SALES 500,000 MKTG 1,000,000
3
PROD 7,800,000 4
SALES 500,000
IT 50,000 5 HR 140,000
HR 140,000 6 IT 50,000
Loading an Internal Table with a
Header Line
With both versions of the
REPORT Y170DM42. APPEND statement, memory
TABLES: EMPLOYEE. space for ten records is
TYPES: BEGIN OF EMP, allocated when the first
record is written to the
COUNTRY LIKE EMPLOYEE-COUNTRY,
internal table.
ID LIKE EMPLOYEE-ID,
SALARY LIKE EMPLOYEE-SALARY,
END OF EMP. Example 1
DATA: EMPTAB TYPE STANDARD TABLE More than ten entries can be
OF EMP INITIAL SIZE 10 WITH HEADER LINE.
saved in the internal table.

SELECT * FROM EMPLOYEE. Example 2


A maximum of ten entries
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
can be saved in the
APPEND EMPTAB. internal table. Any entries
OR that exceed the top ten
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
will
APPEND EMPTAB SORTED BY SALARY.
be deleted.
ENDSELECT.
Internal Table with Header Line
A. TABLES: EMPLOYEE. B DATA: EMPTAB TYPE STANDARD TABLE OF
TYPES:BEGIN OF EMP, EMP INITIAL SIZE 10 WITH HEADER LINE.
ID LIKE EMPLOYEE-ID,
NAME1 LIKE EMPLOYEE-NAME1, SELECT * FROM EMPLOYEE.
COUNTRY LIKE EMPLOYEE-COUNTRY, MOVE-CORRESPONDING EMPLOYEE TO
END OF EMP. EMPTAB.
APPEND EMPTAB.
ENDSELECT.

EMPLOYEE

A COUNTRY ID FORMA NAME1 SORTL . . .

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

COUNTRY ID FORMA NAME1 SORTL . . .


USA 00000001 Company Baker Distributors BAKER . . .

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

COUNTRY ID FORMA NAME1 SORTL . . .


USA 00000001 Company Baker Distributors BAKER . . .

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

COUNTRY ID FORMA NAME1 SORTL . . .


USA 00000001 Company Baker Distributors BAKER . . .

2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line

3 00000001 Baker Distributors USA 1 This header


line is
2 attached to
the body of
3 the internal
. . table.
. .
. .
10
4 SELECT * FROM EMPLOYEE.
5 MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
6 APPEND EMPTAB.
Internal Table
ENDSELECT.
with Header
Line
4 EMPLOYEE

COUNTRY ID FORMA NAME1 SORTL . . .


USA 00000002 Company Diversified Indust.. DIVERS . . .

5
ID NAME1 COUNTRY
00000002 Diversified Indust... USA Header Line

00000001 Baker Distributors USA 1


6 00000002 Diversified Indust... USA 2

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

A COUNTRY ID FORMA NAME1 SORTL . . .

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

COUNTRY ID FORMA NAME1 SORT . . .


USA 00000001 Company Baker Distributors BAKER . . .

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.

Notice the MOVE statement


instead of a MOVE-
CORRESPONDING.
The example above demonstrates how to create an internal table that
gets its structure from an existing table/structure in the ABAP Dictionary
(in this example, EMPLOYEE). This is a very valuable capability.
Referencing other previously defined structures can save time (not
having to code them) and problems.

The only difference between making an internal table from a TYPES


statement or from an existing dictionary structure, is the use of the word
TYPE or LIKE in the DATA declaration.

It is also possible to refer to other objects defined in the program.

Notice the use of MOVE instead of MOVE-CORRESPONDING. The


MOVE statement is used because the structure of EMPTAB is identical
to the structure of EMPLOYEE.

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.

SELECT * FROM EMPLOYEE INTO TABLE EMPTAB


Notice no ENDSELECT is
WHERE COUNTRY = ‘USA’. needed here because no
loop processing occurs.
Mass Reading from Database Tables into
Internal Tables
 To read records from a database table directly into an internal table, you need a
basic SELECT * FROM <database> statement with one of the following
variations:
◦ INTO TABLE <internal table>
◦ APPENDING TABLE <internal table>
 The INTO TABLE <internal table> addition fills the internal table with the
selected database records. Any old entries in the internal table are overwritten.
 The APPENDING TABLE <internal table> appends the selected database
records to any existing entries in the internal table.
 The database records are placed in the internal table EMPTAB in a single
operation rather than one-by-one as in a basic SELECT processing loop.
 Since no loop processing occurs, no ENDSELECT is needed.
 The WHERE and ORDER BY additions are optional.
 The internal table must be at least as wide as the database table. Records are
placed in the internal table left-justified (i.e., starting from the first field listed in
the definition of the internal table), so any additional field(s) you want in the
internal table must be the last field(s) defined beyond the width of the database
table.
Processing an Internal Table
REPORT Y170DM45.
TABLES: EMPLOYEE.
This LOOP AT <EMPTAB>
statement allows for a
TYPES: BEGIN OF EMP,
logical expression in a
COUNTRY LIKE EMPLOYEE-COUNTRY,
WHERE clause to limit the
NAME1 LIKE EMPLOYEE-NAME1, processing of the internal
SALES LIKE EMPLOYEE-SALES, table.
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE
10 WITH HEADER LINE.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB. If no internal table
ENDSELECT. entries qualify under the
LOOP AT EMPTAB WHERE COUNTRY BETWEEN ‘A’ AND ‘D’. logical expression, the
WRITE: / EMPTAB-COUNTRY, EMPTAB-NAME1, statement within the
EMPTAB-SALES. loop is not executed and
ENDLOOP. SY-SUBRC is set to 4.
IF SY-SUBRC NE 0. WRITE: / ‘NO ENTRIES’. ENDIF.
Processing an Internal Table
 An internal table is processed using the LOOP AT … ENDLOOP statement. On each
loop pass, the system ‘reads’ the next table entry and places it in the header line. Also,
the system field SY-TABIX is set to the line number of the entry read.
 When internal table fields are referenced within a program (i.e., EMPTAB-COUNTRY),
the data comes from the header line, not the body of the internal table. You cannot
directly access values from inside the body of the internal table – you must first ‘read’
an entry into the header line.
 After the WHERE parameter, you can specify a logical expression. The entire internal
table is read. If at least one entry satisfies the logical expression,
SY-SUBRC is set to zero. If no table entry satisfies the logical expression,
the statements within the loop are not executed and the system field
SY-SUBRC is set to four. For each entry that satisfies the logical
expression, the statements within the loop are executed.
 The programmer can use the FROM and TO parameters of the LOOP AT statement to
restrict processing of an internal table to a specific block of lines.
 When processing a table without a header line, the syntax of the LOOP statement is
LOOP AT <internal table> INTO <work area>.
System Field SY-TABIX
REPORT Y170DM46.
TABLES: EMPLOYEE.

TYPES: BEGIN OF EMP,


COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE.
screen output
PARAMETERS: START LIKE SY-TABIX DEFAULT 10,
END LIKE SY-TABIX DEFAULT 20.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
SY-TABIX
ENDSELECT.
LOOP AT EMPTAB FROM START TO END.
WRITE: / SY-TABIX, EMPTAB-COUNTRY, EMPTAB-NAME1.
ENDLOOP.
System Field SY-TABIX
 When an internal table is read, the system field SY-TABIX is set to the index value of
the internal table line which has been placed either in the header line or the work
area (depending on the type of table you are reading).
 Notice the use of the FROM and TO parameters in the LOOP AT statement to
restrict the processing of the internal table to a specific set of records.
 It is possible to have nested loops on internal tables. Within these loops SY-TABIX
is set in a similar manner to SY-INDEX.

 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:

 SELECT FROM <database table>


 FOR ALL ENTRIES IN <internal table>
 WHERE <field> = <internal table field>.
Accumulating Data within an
Internal Table
REPORT Y170DM43.
TABLES: EMPLOYEE.
COLLECT <EMPTAB>.
TYPES: BEGIN OF EMP, Country Sales
Header
COUNTRY LIKE EMPLOYEE-COUNTRY, D 400,000 Line
SALES LIKE EMPLOYEE-SALES,
USA 1,000,000
END OF EMP.
GB 500,000
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE. D 7,800,000
screen output
SELECT * FROM EMPLOYEE.
A 371,065.00
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. CH 45,305.00
COLLECT EMPTAB. D 8,200,000.00
ENDSELECT. F 0.00
LOOP AT EMPTAB. GB 500,000.00
WRITE: / EMPTAB-COUNTRY, EMPTAB-SALES. NL 577,000.00
NO 234.00
ENDLOOP.
USA 1,000,000.00
HK 0.00
Accumulating Data within an
Internal Table
 When the COLLECT statement is used, ABAP scans the internal table for entries
which correspond to the header line in fields which are not of type P, I or F (i.e., all of
the non-numeric fields become the ‘key’ of the header line).
 If such an entry is found, the system adds all P, I and F fields in the header line to
the corresponding fields in the matching entry.
 If no corresponding table entry is found, the contents of the header line are added to
the end of the table – the same effect as the APPEND statement.
 The COLLECT statement is also used to load a table with unique key fields. Before
adding a line to the table, the system checks to see if the table has an entry with the
same key value. If an entry already exists, the record is not added, otherwise the
COLLECT statement has the same effect as an APPEND statement. If COLLECT is
used to fill an internal table, duplicate entries cannot occur.
 When using an internal table without a header line, the syntax of the COLLECT
statement changes to: COLLECT <work area> INTO <internal table>.

 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.

DATA: EMPTAB TYPE STANDARD TABLE OF EMP


INITIAL SIZE 10 WITH HEADER LINE.

SELECT * FROM EMPLOYEE.


MOVE-CORRESPONDING EMPLOYEE TO
EMPTAB.
APPEND EMPTAB.
ENDSELECT.

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>.

COLLECT <work area> INTO <internal table>.

INSERT <work area> INTO <internal table>.

MODIFY <internal table> FROM <work area>.

READ TABLE <internal table> INTO <work area>.

LOOP AT <internal table> INTO <work area>.


Working with an Internal Table
without a Header Line
 A work area (staging area) is required when working with an internal table
without a header line. This work area is defined as a Structure with the
same structure as the internal table. The work area is loaded and the table
is processed from the work area.
 A summary of the statements used for internal tables without header lines is
the following:
◦ APPEND <work area> TO <internal table>
 Appends the contents of the work area to the end of the internal table
◦ COLLECT <work area> INTO <internal table>
 Accumulates the values on a field into the table
◦ INSERT <work area> INTO <internal table>
 Inserts a new line with the contents of the work area before the
current line
◦ MODIFY <internal table> FROM <work area>
 Overwrites a line in the table with the contents of the work area
◦ READ TABLE <internal table> INTO <work area>
 Reads a line from the table into the work area
◦ LOOP AT <internal table> INTO <work area>
 Processes an internal table. On each loop pass, a table entry is placed in the work area.
Deleting an Internal Table
CLEAR <internal table>
• Initialises the
header line.
REFRESH <internal table>
• Internal table lines • Deletes all
remain unchanged. table lines.
FREE <internal table>
• Storage space
• Deletes all
is not released.
table lines.
• Paging is released.
• Storage space
• Header line is released.
remains unchanged.
• Header line
remains
unchanged.
Deleting an Internal Table
 The CLEAR <internal table> statement initialises all single fields in
the header line of an internal table according to type.
 The REFRESH <internal table> statement deletes all table lines.
The table storage space is not released. The header line remains
unchanged.
 The FREE <internal table> statement releases the storage space
required for a table. The header line remains unchanged.
◦ This statement is particularly useful for very large internal tables. You can
improve a program’s performance by ‘freeing’ the memory space
allocated for the internal table.
◦ Internal tables that are local to a subroutine are automatically ‘freed’ upon
leaving the subroutine.

 WARNING: If you are working with an internal table with a


separate work area, and you accidentally say CLEAR <internal
table>rather than CLEAR <internal table work area >, you will
delete all the table lines. Use REFRESH to achieve this instead.
Information about an Internal Table
REPORT Y170DM49.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP,
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE,
LINE_COUNT TYPE I, DESCRIBE TABLE <internal table>
INITIAL_COUNT TYPE I. LINES <var1>
SELECT * FROM EMPLOYEE. OCCURS <var2>.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
DESCRIBE TABLE EMPTAB
LINES LINE_COUNT
screen output
OCCURS INITIAL_COUNT.
WRITE: / ‘ lines:’, LINE_COUNT,
/ ‘occurs:’, INITIAL SIZE_COUNT.
Information about an Internal Table
The DESCRIBE TABLE <internal table> statement provides information
about an internal table’s attributes. With this statement, the programmer
must use at least one of the two parameters available – LINES and
OCCURS.
The LINES parameter allows the programmer to find out the number of
existing table entries.
The OCCURS parameter contains the value of the OCCURS clause
specified in the internal table definition.
Notice in the example above the internal table can have more records in it
than the number specified in the OCCURS clause.

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.

Вам также может понравиться