Intro To ABAP - Chapter 07

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Data Structures and Creating

Internal Tables

BC170_2.07.1
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.2
Data Structures

Structure Internal Table

Address List Address List

LN FN City ST. LN FN City ST.

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

Table type Search Tec hnique Acce s s time in


relation to the
number of table
entries
Standard Linear Linear

Sorted Binary Logarithmic

Has hed Has h Algorithm Cons tant (num ber


of entries has no
effec t)

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

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
SALES 500,000 2 MKTG 1,000,000
3 SALES 500,000
PROD 7,800,000
4 HR 140,000
IT 50,000
5
HR 140,000 IT 50,000
6

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.

SELECT * FROM EMPLOYEE. Example 2


MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB. A maximum of ten entries
APPEND IT_EMPTAB. can be saved in the
OR internal table. Any entries
MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB.
that exceed the top ten will
APPEND IT_EMPTAB SORTED BY SALARY.
ENDSELECT. be deleted.

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

COUNTRY ID FORMA NAME1 SORTL . . .


USA 00000001 Company Baker Distributors BAKER . . .

ID NAME1 COUNTRY
Header Line

BC170_2.07.15
Internal Table with Header Line

EMPLOYEE
1

COUNTRY ID FORMA NAME1 SORTL . . .


USA 00000001 Company Baker Distributors BAKER . . .

2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line

BC170_2.07.16
Internal Table with Header 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
BC170_2.07.17
Internal Table with Header Line
EMPLOYEE
4

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

SELECT * FROM EMPLOYEE.


ID NAME1 COUNTRY
MOVE-CORRESPONDING EMPLOYEE TO IT_EMPTAB_WA.
APPEND IT_EMPTAB_WA TO IT_EMPTAB.
ENDSELECT.
APPEND <work area> to <EMPTAB>.
BC170_2.07.19
Internal Table without a Header Line
WHY???

Separate Internal Table Work Area

Performance Issues

Nested Internal Tables

BC170_2.07.20
Internal Table without a Header Line

EMPLOYEE

A COUNTRY ID FORMA NAME1 SORTL . . .

ID NAME1 COUNTRY
B Work Area

BC170_2.07.21
Internal Table without a Header Line
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
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.

Notice the MOVE statement


instead of a MOVE-
CORRESPONDING.
BC170_2.07.23
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.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.

SELECT * FROM EMPLOYEE INTO TABLE


IT_EMPTAB Notice no ENDSELECT is
needed here because no
WHERE COUNTRY = ‘USA’. loop processing occurs.

BC170_2.07.25
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.26
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

BC170_2.07.27

You might also like