Professional Documents
Culture Documents
2
Types of an internal Table
Standard Table
3
Internal Tables as Data Types
• The data type of an internal table is fully specified by its line type, key, and table type.
• Line Type: The line type of an internal table can be any data type. The data type of an
internal table is normally a structure.
4
Creating an Internal Table with Header Line
REPORT Y170DM38.
TABLES: EMPLOYEE. The TYPES statement defines
TYPES: BEGIN OF EMP, the structure and data type for
ID LIKE EMPLOYEE-ID, the internal table.
NAME1 LIKE EMPLOYEE-NAME1, The DATA statement with an
INITIAL SIZE creates the
COUNTRY LIKE actual internal table capable
EMPLOYEE-COUNTRY, of storing data. Because of
the WITH HEADER LINE
END OF EMP. addition, this internal table is
DATA: EMPTAB TYPE STANDARD TABLE created with a header line.
ID NAME1 COUNTRY
OF EMP INITIAL SIZE 10 WITH
Header Line
HEADER LINE.
5
Loading an Internal Table with a Header Line
REPORT Y170DM42.
With both versions of the
TABLES: EMPLOYEE.
APPEND statement,
TYPES: BEGIN OF EMP, memory space for ten
COUNTRY LIKE EMPLOYEE-COUNTRY, records is allocated when
ID LIKE EMPLOYEE-ID, the first record is written to
the internal table.
SALARY LIKE EMPLOYEE-SALARY,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE
Example 1
OF EMP INITIAL SIZE 10 WITH HEADER LINE. More than ten entries can be
saved in the internal table.
SELECT * FROM EMPLOYEE. Example 2
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB. A maximum of ten entries
can be saved in the
OR MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
internal table. Any
APPEND EMPTAB SORTED BY SALARY. entries that exceed the
ENDSELECT. top ten will
be deleted.
6
Append Statement
• Appends a new line to the end of the internal table itab. If you specify wa TO, the new
line is taken from the contents of the explicitly specified work area wa.
• If you use INITIAL LINE TO, a line filled with the correct value for the type is added. If
the specification before itab is omitted, the new line is taken from the internal table itab.
• After the APPEND, the system field SY-TABIX contains the index of the newly added
table entry.
7
Internal Table with Header Line
EMPLOYEE
A
COUNTRY ID FORMA NAME1 SORTL . . .
ID NAME1 COUNTRY
B Header Line
8
Internal Table with Header Line contd.
1 EMPLOYEE
ID NAME1 COUNTRY
Header Line
9
Internal Table with Header Line contd.
1 EMPLOYEE
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line
10
Internal Table with Header Line contd.
1 EMPLOYEE
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line
11
Internal Table with Header Line contd.
4 EMPLOYEE
5
ID NAME1 COUNTRY
00000002 Diversified Indust... USA Header Line
6 2
00000002 Diversified Indust... USA
3
. .
. .
. .
10
12
Creating an Internal Table without a Header Line
REPORT Y170DM40.
TABLES: EMPLOYEE. The TYPES statement defines
TYPES: BEGIN OF EMP, the structure and data type for
the internal table and its work
ID LIKE EMPLOYEE-ID,
area
NAME1 LIKE EMPLOYEE-NAME1,
The DATA statement with an
COUNTRY LIKE EMPLOYEE-COUNTRY, INITIAL SIZE creates the
END OF EMP. actual internal table without a
header line. The DATA
statement without the INITIAL
DATA: EMPTAB TYPE STANDARD TABLE SIZE creates the work area for
OF EMP INITIAL SIZE 10, the internal table.
EMPTAB_WA TYPE EMP.
13
Internal Table without a Header Line WHY???
Performance Issues
14
Internal Table without a Header Line
EMPLOYEE
A
COUNTRY ID FORMA NAME1 SORTL . . .
ID NAME1 COUNTRY
B Work Area
15
Internal Table without a Header Line contd.
1 EMPLOYEE
2 ID NAME1 COUNTRY
00000001 Baker Distributors USA Work Area
ID NAME1 COUNTRY
00000001 Baker Distributors USA 1
3 This work area
2 is not attached
to the body of
the internal
3
. table.
.
.
10
16
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.
17
Automatic Field Conversion
• MOVE
– Structure to structure
– Field to structure
– Structure to field
• Intermediate C type
18
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 <EMPTAB>.
EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.
19
Processing an Internal Table
Report - Retrieving Internal Tables
LOOP AT Statement
LOOP AT itab.
LOOP AT itab INTO wa.
• Processes an internal table (DATA) in a loop which begins with LOOP and ends with
ENDLOOP. Each of the internal table entries is sent to the output area in turn.
• When LOOP AT itab. is used, the header line of the internal table itab is used as output
area.
• In the case of LOOP AT itab INTO wa , there is an explicitly specified work area wa.
• If the internal table is empty, all the statements between LOOP and ENDLOOP are
ignored.
• In each loop pass, SY-TABIX contains the index of the current table entry. After leaving a
LOOP, SY-TABIX has the same value as it had before.
20
Processing an Internal Table
REPORT Y170DM45.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY, This LOOP AT <EMPTAB>
NAME1 LIKE EMPLOYEE-NAME1, statement allows for a logical
SALES LIKE EMPLOYEE-SALES, expression in a WHERE clause
to limit the processing of the
END OF EMP.
internal table.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10
WITH HEADER LINE.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
LOOP AT EMPTAB WHERE COUNTRY BETWEEN ‘A’ AND ‘D’. If no internal table
WRITE: / EMPTAB-COUNTRY, EMPTAB-NAME1, entries qualify under the
EMPTAB-SALES. logical expression, the
ENDLOOP. statement within the
IF SY-SUBRC NE 0. loop is not executed and
WRITE: / ‘NO ENTRIES’.
SY-SUBRC is set to 4.
ENDIF.
21
System Field SY-TABIX
REPORT Y170DM46.
TABLES: EMPLOYEE.
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.
22
Accumulating Data within an Internal Table
COLLECT Statement
23
Accumulating Data within an Internal Table
REPORT Y170DM43.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COLLECT <EMPTAB>.
COUNTRY LIKE EMPLOYEE-COUNTRY, Country Sales
Header
SALES LIKE EMPLOYEE-SALES, D 400,000
Line
END OF EMP. USA 1,000,000
DATA: EMPTAB TYPE STANDARD TABLE OF EMP GB 500,000
INITIAL SIZE 10 WITH HEADER LINE. D 7,800,000
Screen output
SELECT * FROM EMPLOYEE.
A 371,065.00
MOVE-CORRESPONDING EMPLOYEE TO CH 45,305.00
EMPTAB.
D 8,200,000.00
COLLECT EMPTAB. F 0.00
ENDSELECT. GB 500,000.00
LOOP AT EMPTAB. NL 577,000.00
NO 234.00
WRITE: / EMPTAB-COUNTRY, EMPTAB-SALES.
USA 1,000,000.00
ENDLOOP. HK 0.00
24
Sorting an Internal Table
SORT Statement
SORT itab DESCENDING.
SORT itab ASCENDING.
SORT itab BY f1 f2 ... fi.
• Sorts the entries of the internal table itab in ascending order. The default key is used
as the sort key for internal tables.
• Sorts itab by the sub-fields f1, f2 , ..., fi which form the sort key. These fields can be
any type (even number fields or tables).
• Unless you specify otherwise, the sort is in ascending order. You can also use
additions 1 and 2 before BY if you want all sub-fields to apply.
• To change the sort sequence for each individual field, specify DESCENDING or
ASCENDING after each of the sub-fields f1 , f2 , ..., fi.
25
Sorting an Internal Table
REPORT Y170DM44.
TABLES: EMPLOYEE.
Sorting options:
TYPES: BEGIN OF EMP,
1) SORT <EMPTAB> - sorts the
COUNTRY LIKE EMPLOYEE-COUNTRY,
entries of the internal table
NAME1 LIKE EMPLOYEE-NAME1, <EMPTAB> in ascending
SALES LIKE EMPLOYEE-SALES, order.
END OF EMP. 2) SORT <EMPTAB> BY <field> -
DATA: EMPTAB TYPE STANDARD TABLE OF EMP sorts the table on one or more
INITIAL SIZE 10 WITH HEADER LINE. fields within the table.
SELECT * FROM EMPLOYEE.
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.
26
Control Level Processing
• All these structures begin with AT and end with ENDAT. The sequence of statements which
lies between them is then executed if a control break occurs.
• AT FIRST
• AT LAST
27
Control Level Processing contd.
AT FIRST AT LAST
• Statements are executed before any • Statements are executed after all records
records are processed while looping are processed while looping at Internal
at Internal table. table.
• Example : Example :
LOOP AT itab. LOOP AT itab.
AT FIRST. AT LAST.
WRITE : SY-ULINE. WRITE : SY-ULINE.
ENDAT. ENDAT.
ENDLOOP. ENDLOOP.
28
Control Level Processing contd.
Example :
Example : LOOP AT itab.
LOOP AT itab. AT END OF I_LIFNR.
AT NEW I_LIFNR. WRITE : SY-ULINE.
WRITE : SY-ULINE ENDAT.
ENDAT. ENDLOOP.
ENDLOOP.
29
Reading an Internal Table
READ Statement
READ TABLE itab INDEX idx [INTO WA].
READ TABLE itab WITH KEY <k1> = <f1>
<k2> = <f2>…
<kn> = <fn> [INTO wa]
[BINARY SEARCH].
• Reads an internal table entry. An entry can be chosen using a key or its index idx.
• With "READ TABLE itab.", the header line of the internal table itab is used as the output
area; with "READ TABLE itab INTO wa." the explicitly specified work area wa is used for
this purpose.
• For BINARY SEARCH, the internal table itab should be sorted by the keys k1,k2…kn.
30
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 ….
31
Reading a Single Table Entry - Options
32
Filling Internal Tables
INSERT Statement
• Inserts a new line into an internal table. If you specify wa INTO , the new line is taken
from the contents of the explicitly specified work area wa.
• When using INITIAL LINE INTO , a line containing the appropriate initial value for its
type is inserted into the table. If you omit the specification before itab , the new line is
taken from the header line of the internal table itab.
• INDEX idx specifies the table index before which the line is inserted into the table itab .
33
Filling Internal Tables
Modify Statement
• If you specify FROM wa , the line is replaced by the explicitly specified work area wa . If
the FROM specification is omitted, the line is replaced by the header line from itab .
• With INDEX idx, you can specify the table index of the line to be changed. The index
specification can be omitted in a LOOP on an internal table.
• The INDEX specification can also appear before the FROM specification.
34
Deleting Internal Tables
DELETE Statement
1. DELETE itab.
• The current entry of the internal table itab is deleted in a LOOP loop.
35
Deleting Internal Tables contd.
• Deletes the line area from index idx1 to idx2 from internal table itab. At least one of
the two parameters FROM idx1 or TO idx2 should be specified.
• If parameter FROM is missing, the area from the start of the table to line idx2 is
deleted.
• If parameter TO is missing, the area from line idx1 to the end of the table is deleted.
• Start index idx1 must be greater than 0. The return code value is set as follows:
36
Maintaining Internal Tables
37
Deleting an Internal Table
38
Retrieving Internal Table attributes
DESCRIBE Statement
DESCRIBE TABLE itab.
• Returns the attributes of the internal table itab. You must use at least one of the
additions listed below.
Additions :
• Places the number of filled lines of the table itab in the field lin.
2. ... OCCURS n
• Transfers the size of the OCCURS parameter from the table definition to the variable
n.
39
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,
DESCRIBE TABLE <internal table>
LINE_COUNT TYPE I,
LINES <var1>
INITIAL_COUNT TYPE I.
OCCURS <var2>.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
DESCRIBE TABLE EMPTAB
LINES LINE_COUNT
OCCURS INITIAL_COUNT.
WRITE: / ‘ lines:’, LINE_COUNT, screen output
/ ‘occurs:’, INITIAL SIZE_COUNT.
40
Operations on Internal Tables
41
ABAPDOCU
42
Thank You!