Chapter 07 - Data Structures & Internal Tables
Chapter 07 - Data Structures & Internal Tables
Dec-2008
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
Dec-2008
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.
Dec-2008
REPORT YN1C0008.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Basic Syntax:
DATA: BEGIN OF <name>
<field1> . . .
<field2> . . .
...
END OF <name>.
Address Structure
Flag
ID
Dec-2008
Name1
City
Basic Syntax:
FLAG,
<field2> . . . ,
ID
TYPE EMPLOYEE-ID,
NAME1
TYPE EMPLOYEE-NAME1,
CITY
TYPE EMPLOYEE-CITY,
END OF <name1>.
DATA: <name2> TYPE
<name1>.
END OF ADDR.
DATA: ADDRESS TYPE ADDR.
MOVE:
... ,
X TO ADDRESS-FLAG,
00001 TO ADDRESS-ID,
Address Structure
Smith TO ADDRESS-NAME1,
Flag
ID
Name1
City
Philadelphia TO ADDRESS-CITY.
WRITE: ADDRESS-ID, ADDRESS-NAME1,
ADDRESS-CITY.
5
Dec-2008
EMPLOYEE
ID
Name1
000000001
Address
Flag
City
Electronics Inc.
Waldorf
MOVE-CORRESPONDING EMPLOYEE
TO ADDRESS.
ID
000000001
Name
City
Waldorf
Clear <f1>.
Dec-2008
Demonstration
Declaring a structure and populating the structure with values inside a program.
Dec-2008
Practice
Declaring a structure and populating the structure with values inside a program.
Dec-2008
Dec-2008
REPORT Y170DM40.
TYPES: BEGIN OF EMP,
ID
TYPE ID,
NAME1
TYPE NAME1,
NAME1
COUNTRY
OF EMP,
EMPTAB_WA TYPE EMP.
Work Area
10
Dec-2008
TYPE ID,
NAME1
TYPE NAME1,
END OF EMP.
Work Area
ID
NAME1
COUNTRY
11
Performance Issues
12
Dec-2008
EMPLOYEE
COUNTRY
ID
FORMA
ID
NAME1
NAME1
SORTL
COUNTRY
Work Area
13
. . .
Dec-2008
EMPLOYEE
COUNTRY
ID
FORMA
NAME1
SORT
. . .
USA
00000001 Company Baker Distributors BAKER
. . .
ID
NAME1
COUNTRY
00000001 Baker Distributors USA
ID
NAME1
COUNTRY
00000001 Baker Distributors USA
Work Area
1
2
3
.
.
.
10
14
Dec-2008
REPORT Y170DM41.
DATA: EMPTAB TYPE STANDARD TABLE OF
EMPLOYEE,
WA_EMP TYPE EMPLOYEE.
15
Dec-2008
MOVE
Structure to structure
Field to structure
Structure to field
16
Intermediate C type
Dec-2008
2. APPENDING TABLE
<EMPTAB>.
17
Notice no ENDSELECT is
needed here because no
loop processing occurs.
Dec-2008
TYPE COUNTRY,
NAME1
TYPE NAME1,
SALES
TYPE SALES,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP,
WA_EMP TYPE EMP
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.
LOOP AT EMPTAB INTO WA_EMP WHERE COUNTRY BETWEEN A AND
D.
WRITE: / WA_EMP-COUNTRY, WA_EMP-NAME1,
WA_EMP-SALES.
CLEAR WA_EMP.
ENDLOOP.
IF SY-SUBRC NE 0.
WRITE: / NO ENTRIES.
ENDIF.
18
If no internal table
entries qualify under the
logical expression, the
statement within the
loop is not executed and
SY-SUBRC is set to 4.
Dec-2008
TYPE NAME1,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP .
PARAMETERS:
Screen output
SY-TABIX
ENDLOOP.
19
Dec-2008
TYPE SALES,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP,
WA_EMP TYPE EMP.
COLLECT <EMPTAB>.
Country
Sales
D
USA
GB
D
20
A
CH
D
F
GB
NL
NO
USA
HK
Dec-2008
400,000
1,000,000
500,000
7,800,000
Screen output
371,065.00
45,305.00
8,200,000.00
0.00
500,000.00
577,000.00
234.00
1,000,000.00
0.00
IBM Corporation 2013
TYPE NAME1,
SALES
TYPE SALES,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP ,
WA_EMP TYPE EMP.
Sorting options:
1) SORT <EMPTAB> - sorts the
entries of the internal table
<EMPTAB> in ascending
order.
2) SORT <EMPTAB> BY <field> sorts the table on one or more
fields within the table.
screen output
ENDLOOP.
21
Dec-2008
22
ID
Dec-2008
NAME1
COUNTRY
Explicit Key
User-defined
e.g. WITH [ UNIQUE/NON-UNIQUE ] KEY FIELD1 FIELD2 ...
23
Dec-2008
24
Dec-2008
25
Dec-2008
TYPE NAME1,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP.
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.
READ TABLE .
26
Dec-2008
27
Dec-2008
28
Dec-2008
29
Dec-2008
Paging is released.
Storage space is
released.
30
Dec-2008
screen output
31
Dec-2008
TYPE COUNTRY,
NAME1
TYPE NAME1,
END OF EMP,
DATA:
screen output
32
Dec-2008
Demonstration
Declaring an internal table, populating it by selecting data from the table and then
looping into it and displaying the data fetched.
33
Dec-2008
Practice
Declaring an internal table, populating it by selecting data from the table and then
looping into it and displaying the data fetched.
34
Dec-2008
Summary
Structures in code are temporary objects in program memory.
A structure can be defined using a combination of the TYPES and DATA
statements.
The statement MOVE-CORRESPONDING transports values field by field
between the ABAP data structures.
Internal table, that can store records of data temporarily during the
processing of a program.
3 different types of internal tables: Standard, Sorted, and Hashed.
An internal table object is created with the DATA statement by referring to an
internal table type using the TYPE parameter
APPEND statement adds the contents of the work area to the internal table.
The system field SY-TABIX is set to the line number of the entry read.
35
Dec-2008
Summary (Contd.)
The CLEAR statement resets all fields to their initial value.
The REFRESH statement deletes all table lines.
The FREE statement releases the storage space required for a table.
36
Dec-2008
Questions
What is a Structure?
What is an internal table?
What are the different types of internal tables are there?
Explain the following statements :
Move corresponding
Append
Clear
Refresh
Free.
37
Dec-2008