Intro To ABAP - Chapter 07
Intro To ABAP - Chapter 07
Intro To ABAP - Chapter 07
Internal Tables
BC170_2.07.1
Objectives
BC170_2.07.2
Data Structures
LN FN City ST.
LN FN City ST.
BC170_2.07.3
Declaring a Structure - Method #1
Is this statement
1 REPORT YN1C0008.
2 necessary for the Basic Syntax:
3 TABLES: TABNA. code?
4 DATA: BEGIN OF ADDRESS, DATA: BEGIN OF <name>
5 FLAG TYPE C,
6 ID LIKE TABNA-ID,
7 NAME1 LIKE TABNA-NAME1, <field1> . . .
8 CITY LIKE TABNA-CITY,
9 END OF ADDRESS. <field2> . . .
10 MOVE ‘X’ TO ADDRESS-FLAG.
11 MOVE ‘0001’ TO ADDRESS-ID.
12 MOVE ‘Smith’ TO ADDRESS-NAME1. ...
13 MOVE ‘Philadelphia’ TO ADDRESS-CITY.
14 END OF <name>.
15 WRITE ADDRESS.
16
17
18
19 Address Structure
20 Flag ID Name1 City
21
22
23
BC170_2.07.4
Declaring a Structure - Method #2
REPORT Yxxxxxxx. Basic Syntax:
TYPES: BEGIN OF ADDR,
FLAG, TYPES: BEGIN OF <name1>,
ID LIKE EMPLOYEE-ID,
NAME1 LIKE EMPLOYEE-NAME1, <field1> . . . ,
CITY LIKE EMPLOYEE-CITY,
END OF ADDR. <field2> . . . ,
DATA: ADDRESS TYPE ADDR.
... ,
MOVE: ‘X’ TO ADDRESS-FLAG,
‘00001’ TO ADDRESS-ID, END OF <name1>.
‘Smith’ TO ADDRESS-NAME1,
‘Philadelphia’ TO ADDRESS-CITY. DATA: <name2> TYPE
<name1>.
WRITE ADDRESS.
Address Structure
Flag ID Name1 City
BC170_2.07.5
Populating a Structure with
Field-by-Field Transport
REPORT Y170DM37.
EMPLOYEE
TABLES: EMPLOYEE. ID Name1 City
DATA: BEGIN OF ADDRESS, 000000001 Electronics Inc. Waldorf
FLAG,
MOVE-CORRESPONDING EMPLOYEE TO ADDRESS.
ID LIKE EMPLOYEE-ID,
NAME LIKE EMPLOYEE-NAME1, Address
CITY LIKE EMPLOYEE-CITY, Flag ID Name City
END OF ADDRESS. 000000001 Waldorf
Clear <Address>.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO
ADDRESS.
WRITE: / ADDRESS-FLAG, ADDRESS-ID,
ADDRESS-NAME, ADDRESS-CITY.
CLEAR ADDRESS.
ENDSELECT.
BC170_2.07.6
Internal Table Types
BC170_2.07.7
Creating an Internal Table
with Header Line
REPORT Y170DM38.
The TYPES statement defines
the structure and data type for
TABLES: EMPLOYEE.
the internal table.
TYPES: BEGIN OF EMP,
The DATA statement with
ID LIKE EMPLOYEE-ID,
TYPE STANDARD TABLE
NAME1 LIKE EMPLOYEE-NAME1,
creates the actual internal
COUNTRY LIKE EMPLOYEE-COUNTRY, table capable of storing data.
END OF EMP. Because of the WITH HEADER
LINE addition, this internal
DATA: IT_EMPTAB TYPE STANDARD TABLE table is created with a header
OF EMP INITIAL SIZE 10 WITH HEADER LINE. line.
ID NAME1 COUNTRY
SELECT * FROM EMPLOYEE.
Header Line
MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB.
APPEND IT_EMPTAB.
ENDSELECT.
BC170_2.07.8
Internal Table Keys
• Implicit Key
– All character fields
• Explicit Key
– User-defined
• e.g. … WITH [ UNIQUE/NON-UNIQUE ]
KEY FIELD1 FIELD2 ...
BC170_2.07.9
Size of an Internal Table
8K
8K 8K
8K 8K 8K
BC170_2.07.10
Creating an Internal Table
With OCCURS Clause
REPORT YINTRODEMO6. The TYPES statement is not
TABLES: EMPLOYEE. used.
DATA: BEGIN OF IT_EMP OCCURS 0,
The DATA statement is used
ID LIKE EMPLOYEE-ID,
with the OCCURS clause
NAME1 LIKE EMPLOYEE-NAME1,
creates the internal table
COUNTRY LIKE EMPLOYEE-COUNTRY, object. The header line is
END OF IT_EMP. created automatically. The
structure of the internal table
SELECT * FROM EMPLOYEE. is defined with BEGIN OF …
MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB. and END OF <itab>.
APPEND IT_EMPTAB. ID NAME1 COUNTRY
ENDSELECT.
BC170_2.07.11
Loading an Internal Table with a
Header Line
BC170_2.07.12
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
COUNTRY LIKE EMPLOYEE-COUNTRY, record is written to the
internal table.
ID LIKE EMPLOYEE-ID,
SALARY LIKE EMPLOYEE-SALARY,
END OF EMP. Example 1
DATA: IT_EMPTAB TYPE STANDARD TABLE More than ten entries can be
OF EMP INITIAL SIZE 10 WITH HEADER LINE. saved in the internal table.
BC170_2.07.13
Internal Table with Header Line
EMPLOYEE
A
COUNTRY ID FORMA NAME1 SORTL . . .
ID NAME1 COUNTRY
B Header Line
BC170_2.07.14
Internal Table with Header Line
1 EMPLOYEE
ID NAME1 COUNTRY
Header Line
BC170_2.07.15
Internal Table with Header Line
EMPLOYEE
1
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line
BC170_2.07.16
Internal Table with Header Line
EMPLOYEE
1
2 ID NAME1 COUNTRY
5 ID NAME1 COUNTRY
00000002 Diversified Indust... USA Header Line
3.
. .
. .
.
10
BC170_2.07.18
Creating an Internal Table without a
Header Line
REPORT Y170DM40. The TYPES statement defines
TABLES: EMPLOYEE. the structure and data type for
TYPES: BEGIN OF EMP,
the internal table and its work
area.
ID LIKE EMPLOYEE-ID,
NAME1 LIKE EMPLOYEE-NAME1, Not including the WITH HEADER
COUNTRY LIKE EMPLOYEE-COUNTRY,
LINE addition creates the actual
internal table without a header
END OF EMP.
line. Instead, the WORK AREA
for the internal table is created
DATA: IT_EMPTAB TYPE STANDARD TABLE by defining a structure that uses
OF EMP INITIAL SIZE 10, the user-defined data TYPE
IT_EMPTAB_WA TYPE EMP. Work Area EMP.
Performance Issues
BC170_2.07.20
Internal Table without a Header Line
EMPLOYEE
ID NAME1 COUNTRY
B Work Area
BC170_2.07.21
Internal Table without a Header Line
1 EMPLOYEE
ID NAME1 COUNTRY
This work area
3 00000001 Baker Distributors USA 1 is not attached
2 to the body of
3. the internal
. table.
.
10
BC170_2.07.22
Transferring ABAP Dictionary Table
Structures
REPORT Y170DM41.
TABLES: EMPLOYEE.
DATA: IT_EMPTAB LIKE STANDARD TABLE OF
EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.
The internal table IT_EMPTAB
will have the exact same
SELECT * FROM EMPLOYEE. structure as the dictionary
MOVE EMPLOYEE TO IT_EMPTAB.
table EMPLOYEE.
APPEND IT_EMPTAB.
ENDSELECT.
BC170_2.07.24
Mass Reading from Database Tables
into Internal Tables
REPORT Y170DM69.
SELECT * FROM <table> . . .
TABLES: EMPLOYEE. 1. INTO TABLE IT_EMPTAB.
2. APPENDING TABLE
DATA: IT_EMPTAB LIKE STANDARD TABLE OF IT_EMPTAB.
EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.
BC170_2.07.25
Working with an Internal Table
without a Header Line
APPEND <work area> TO <internal table>.
BC170_2.07.27