BC170_2.07.1
Data Structures and Creating
Internal Tables
BC170_2.07.2
Objectives
• The participants will be able to:
–
Create a Structure in an ABAP Program
–
Create an Internal Table in an ABAP program
–
Populate an Internal Table with data
–
Read Database information into an Internal Table
BC170_2.07.3
Address List
Structure
Structure
Data Structures
Address List
Internal
Internal
Table
Table
LN
LN
FN
FN
City
City
ST.
ST.
LN
LN
FN
FN
City
City
ST.
ST.
LN
LN
FN
FN
City
City
ST.
ST.
LN
LN
FN
FN
City
City
ST.
ST.
BC170_2.07.4
1
REPORT YN1C0008.
2
3
TABLES: TABNA.
4
DATA:
BEGIN OF ADDRESS,
5
FLAG
TYPE C,
6
ID
LIKE TABNA-ID,
7
NAME1
LIKE TABNA-NAME1,
8
CITY
LIKE TABNA-CITY,
9
END OF ADDRESS.
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
15
WRITE ADDRESS.
16
17
18
19
20
21
22
23
Basic Syntax:
DATA: BEGIN OF
<name>
<field1> . . .
<field2> . . .
. . .
END OF
<name>.
Flag
ID
Name1
City
Address Structure
Is this
statement
necessary for
the code?
Declaring a Structure - Method
#1
BC170_2.07.5
REPORT Yxxxxxxx.
TYPES: BEGIN OF ADDR,
FLAG,
ID
LIKE EMPLOYEE-ID,
NAME1 LIKE EMPLOYEE-NAME1,
CITY
LIKE EMPLOYEE-CITY,
END OF ADDR.
DATA: ADDRESS TYPE ADDR.
MOVE: ‘X’ TO ADDRESS-FLAG,
‘00001’ TO ADDRESS-ID,
‘Smith’ TO ADDRESS-NAME1,
‘Philadelphia’ TO ADDRESS-CITY.
WRITE ADDRESS.
Basic Syntax:
TYPES: BEGIN OF
<name1>,
<field1> . . . ,
<field2> . . . ,
. . . ,
END OF
<name1>.
DATA: <name2> TYPE
<name1>.
Flag
ID
Name1
City
Address Structure
Declaring a Structure - Method
#2
BC170_2.07.6
Populating a Structure with
Field-by-Field Transport
REPORT Y170DM37.
TABLES: EMPLOYEE.
DATA: BEGIN OF ADDRESS,
FLAG,
ID
LIKE EMPLOYEE-ID,
NAME
LIKE EMPLOYEE-
NAME1,
CITY
LIKE EMPLOYEE-CITY,
END OF ADDRESS.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO
ADDRESS.
WRITE: / ADDRESS-FLAG, ADDRESS-
ID,
ADDRESS-NAME, ADDRESS-
CITY.
CLEAR ADDRESS.
ENDSELECT.
EMPLOYEE
Address
ID
Name1
City
000000001 Electronics Inc. Waldorf
MOVE-CORRESPONDING EMPLOYEE TO
ADDRESS.
Flag
ID
Name
City
000000001
Waldorf
Clear <Address>.
BC170_2.07.7
Internal Table Types
Table type Search Technique Access time in
relation to the
number of table
entries
Standard
Linear
Linear
Sorted
Binary
Logarithmic
Hashed
Hash Algorithm
Constant (number
of entries has no
effect)
BC170_2.07.8
REPORT Y170DM38.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
ID
LIKE EMPLOYEE-ID,
NAME1
LIKE EMPLOYEE-NAME1,
COUNTRY LIKE EMPLOYEE-COUNTRY,
END OF EMP.
DATA: IT_EMPTAB TYPE STANDARD TABLE
OF EMP INITIAL SIZE 10 WITH HEADER
LINE.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO
IT_EMPTAB.
APPEND IT_EMPTAB.
ENDSELECT.
The TYPES statement
defines the structure and
data type for the internal
table.
The DATA statement with
TYPE STANDARD TABLE
creates the actual
internal table capable of
storing data. Because of
the WITH HEADER LINE
addition, this internal
table is created with a
header line.
Header Line
ID NAME1 COUNTRY
Creating an Internal Table
with Header Line
BC170_2.07.9
Internal Table Keys
•
Implicit Key
–
All character fields
•
Explicit Key
–
User-defined
•
e.g. … WITH [ UNIQUE/NON-
UNIQUE ] KEY FIELD1 FIELD2 ...
BC170_2.07.10
Size of an Internal Table
8K
8K
8K
8K
8K
8K
BC170_2.07.11
Creating an Internal Table
With OCCURS Clause
REPORT YINTRODEMO6.
TABLES: EMPLOYEE.
DATA: BEGIN OF IT_EMP OCCURS 0,
ID
LIKE EMPLOYEE-ID,
NAME1
LIKE EMPLOYEE-NAME1,
COUNTRY LIKE EMPLOYEE-COUNTRY,
END OF IT_EMP.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO
IT_EMPTAB.
APPEND IT_EMPTAB.
ENDSELECT.
The TYPES statement is
not used.
The DATA statement is
used with the OCCURS
clause creates the
internal table object. The
header line is created
automatically. The
structure of the internal
table is defined with
BEGIN OF … and END OF
<itab>.
ID NAME1 COUNTRY
BC170_2.07.12
APPEND <int. table>
SORTED BY <field>.
APPEND <int. table>.
Department Salary
1
2
3
4
5
6
Department Salary
Header
Loading an Internal Table with a
Header Line
R&D
400,000
PROD
7,800,000
MKTG
1,000,000
SALES
500,000
HR
140,000
IT
50,000
R&D
400,000
MKTG
1,000,000
SALES
500,000
PROD
7,800,000
IT
50,000
HR
140,000
BC170_2.07.13
REPORT Y170DM42.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
ID
LIKE EMPLOYEE-ID,
SALARY
LIKE EMPLOYEE-SALARY,
END OF EMP.
DATA: IT_EMPTAB TYPE STANDARD TABLE
OF EMP INITIAL SIZE 10 WITH HEADER LINE.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO
IT_EMPTAB.
APPEND IT_EMPTAB.
MOVE-CORRESPONDING EMPLOYEE TO
IT_EMPTAB.
APPEND IT_EMPTAB SORTED BY SALARY.
ENDSELECT.
Loading an Internal Table with a
Header Line
More than ten entries
can be saved in the
internal table.
A maximum of ten
entries can be saved
in the internal table.
Any entries that
exceed the top ten
will
be deleted.
With both versions of the
APPEND statement,
memory space for ten
records is allocated when
the first record is written
to the internal table.
Example
1
Example 2
OR
BC170_2.07.14
Internal Table with Header Line
EMPLOYEE
EMPLOYEE
COUNTRY ID FORMA NAME1 SORTL
. . .
ID NAME1
COUNTRY
Header Line
A
B
BC170_2.07.15
Internal Table with Header Line
ID NAME1
COUNTRY
EMPLOYEE
EMPLOYEE
USA 00000001 Company Baker Distributors
BAKER
. . .
COUNTRY ID FORMA NAME1 SORTL
. . .
Header Line
1
BC170_2.07.16
Internal Table with Header Line
ID NAME1
COUNTRY
00000001 Baker Distributors
USA
EMPLOYEE
EMPLOYEE
Header Line
2
1
USA 00000001Company Baker DistributorsBAKER
. . .
COUNTRY ID FORMA NAME1 SORTL
. . .
BC170_2.07.17
Internal Table with Header Line
USA 00000001 Company Baker Distributors BAKER
. . .
ID NAME1 COUNTRY
00000001 Baker Distributors USA
00000001 Baker Distributors USA
EMPLOYEE
EMPLOYEE
COUNTRY ID FORMA NAME1 SORTL .
. .
Header Line
2
3
1
2
3
10
.
.
.
.
.
.
This header
line is
attached to
the body of
the internal
table.
1
BC170_2.07.18
Internal Table with Header Line
ID NAME1
COUNTRY
00000001 Baker Distributors
USA
00000002 Diversified Indust... USA
00000002 Diversified Indust... USA
USA 00000002 Company Diversified Indust..
DIVERS . . .
EMPLOYEE
EMPLOYEE
COUNTRY ID FORMA NAME1 SORTL . .
.
Header Line
5
6
1
2
3
10
.
.
.
.
.
.
4
BC170_2.07.19
Creating an Internal Table without a
Header Line
REPORT Y170DM40.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
ID
LIKE EMPLOYEE-ID,
NAME1
LIKE EMPLOYEE-NAME1,
COUNTRY LIKE EMPLOYEE-COUNTRY,
END OF EMP.
DATA: IT_EMPTAB TYPE STANDARD TABLE
OF EMP INITIAL SIZE 10,
IT_EMPTAB_WA TYPE EMP.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB_WA.
APPEND IT_EMPTAB_WA TO IT_EMPTAB.
ENDSELECT.
ID NAME1 COUNTRY
APPEND <work area> to
<EMPTAB>.
The TYPES statement
defines the structure and
data type for the internal
table and its work area.
Work Area
Not including the WITH
HEADER LINE addition
creates the actual internal
table without a header
line. Instead, the WORK
AREA for the internal table
is created by defining a
structure that uses the
user-defined data TYPE
EMP.
BC170_2.07.20
Separate Internal Table Work
Area
Performance Issues
Nested Internal Tables
Internal Table without a Header
Line WHY???
BC170_2.07.21
EMPLOYEE
EMPLOYEE
COUNTRY ID FORMA NAME1
SORTL . . .
Work Area
A
B
ID NAME1
COUNTRY
Internal Table without a Header
Line
BC170_2.07.22
00000001
Baker Distributors USA
Internal Table without a Header
Line
ID NAME1
COUNTRY
ID NAME1
COUNTRY
00000001 Baker Distributors USA
USA 00000001 Company Baker Distributors BAKER . . .
EMPLOYEE
EMPLOYEE
COUNTRY ID FORMA NAME1
SORT . . .
Work Area
1
2
3
1
2
3
10
.
.
.
This work
area is not
attached to
the body of
the internal
table.
BC170_2.07.23
Transferring ABAP Dictionary
Table Structures
REPORT Y170DM41.
TABLES: EMPLOYEE.
DATA: IT_EMPTAB LIKE STANDARD TABLE
OF EMPLOYEE INITIAL SIZE 10 WITH HEADER
LINE.
SELECT * FROM EMPLOYEE.
MOVE EMPLOYEE TO IT_EMPTAB.
APPEND IT_EMPTAB.
ENDSELECT.
The internal table
IT_EMPTAB will have the
exact same structure as
the dictionary table
EMPLOYEE.
Notice the MOVE
statement instead of a
MOVE-CORRESPONDING.
BC170_2.07.24
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
BC170_2.07.25
Mass Reading from Database
Tables into Internal Tables
REPORT Y170DM69.
TABLES: EMPLOYEE.
DATA: IT_EMPTAB LIKE STANDARD TABLE
OF EMPLOYEE INITIAL SIZE 10 WITH HEADER
LINE.
SELECT * FROM EMPLOYEE INTO TABLE
IT_EMPTAB
WHERE COUNTRY = ‘USA’.
SELECT * FROM <table> . . .
1. INTO TABLE IT_EMPTAB.
2. APPENDING TABLE
IT_EMPTAB.
Notice no ENDSELECT
is needed here
because no loop
processing occurs.
BC170_2.07.26
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>.
BC170_2.07.27
Summary
• The participants will be able to:
–
Create a Structure in an ABAP Program
–
Create an Internal Table in an ABAP program
–
Populate an Internal Table with data
–
Read Database information into an Internal
Table