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
Address List LN FN City ST.
Internal Table Address List LN LN LN FN FN FN City City City ST. ST. ST.
Dec-2008
REPORT YN1C0008. DATA: BEGIN OF ADDRESS, FLAG TYPE C, ID TYPE TABNA-ID, NAME1 TYPE TABNA-NAME1, CITY TYPE TABNA-CITY, END OF ADDRESS. MOVE X TO ADDRESS-FLAG. MOVE 0001 TO ADDRESS-ID. MOVE Smith TO ADDRESS-NAME1. MOVE Philadelphia TO ADDRESS- CITY. WRITE: ADDRESS-FLAG,ADDRESS-ID. ADDRESS-NAME1,ADDRESS-CITY Basic Syntax: DATA: BEGIN OF <name> <field1> . . .
2 3 4 5 6 7 8 9 10 11 12 13 14 15
<field2> . . .
... END OF <name>.
Address Structure
Flag ID Name1 City
Dec-2008
FLAG,
ID NAME1 CITY
<field2> . . . ,
... , END OF <name1>. DATA: <name2> TYPE <name1>.
END OF ADDR.
DATA: ADDRESS TYPE ADDR. MOVE: X TO ADDRESS-FLAG, 00001 TO ADDRESS-ID,
Smith TO ADDRESS-NAME1,
Address Structure
Flag ID Name1 City
EMPLOYEE
ID 000000001 Name1 Electronics Inc. City Waldorf
Address
Flag
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
Sorted
Hashed
Dec-2008
The TYPES statement defines the structure and data type for the internal table and its work area
Work Area
10
Dec-2008
ID
NAME1
TYPE ID,
TYPE NAME1,
The TYPES statement defines the structure and data type for the internal table and its work area The DATA statement with an INITIAL SIZE creates the actual internal table without a header line. The DATA statement without the INITIAL SIZE creates the work area for the internal table.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10, EMPTAB_WA TYPE EMP.
Work Area
ID
NAME1
COUNTRY
11
Performance Issues
12
Dec-2008
EMPLOYEE
COUNTRY
ID
FORMA
NAME1
SORTL
. . .
ID B
NAME1
COUNTRY
Work Area
13
Dec-2008
EMPLOYEE
COUNTRY ID FORMA NAME1 SORT . . . USA 00000001 Company Baker Distributors BAKER . . .
2 ID NAME1 COUNTRY 00000001 Baker Distributors USA
Work Area
1 2 3 . . . 10
This work area is not attached to the body of the internal table.
14
Dec-2008
REPORT Y170DM41.
DATA: EMPTAB TYPE STANDARD TABLE OF EMPLOYEE, WA_EMP TYPE EMPLOYEE.
The internal table EMPTAB will have the exact same structure as the dictionary table EMPLOYEE.
SELECT * FROM EMPLOYEE INTO WA_EMP. MOVE WA_EMP TO EMPTAB. APPEND WA_EMP TO EMPTAB. ENDSELECT.
15
Dec-2008
MOVE
Structure to structure Field to structure Structure to field
Intermediate C type Followed by adoption of new types
16
Dec-2008
17
Dec-2008
This LOOP AT <EMPTAB> statement allows for a logical expression in a WHERE clause to limit the processing of the internal table.
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
18
PARAMETERS:
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB. LOOP AT EMPTAB INTO WA_EMP FROM START TO END. WRITE: / SY-TABIX, WA_EMP-COUNTRY, WA_EMP-NAME1. ENDLOOP.
Screen output
SY-TABIX
19
Dec-2008
A CH D F GB NL NO USA HK
Dec-2008
20
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.
END OF EMP. DATA: EMPTAB TYPE STANDARD TABLE OF EMP , WA_EMP TYPE EMP. SELECT * FROM EMPLOYEE INTO TABLE EMPTAB. SORT EMPTAB BY SALES DESCENDING. LOOP AT EMPTAB INTO WA_EMP. WRITE: / WA_EMP-COUNTRY, WA_EMP-NAME1, WA_EMP-SALES. ENDLOOP.
21 Data Structure & Internal Tables |
screen output
Dec-2008
Header Line
22
Dec-2008
Explicit Key
User-defined e.g. WITH [ UNIQUE/NON-UNIQUE ] KEY FIELD1 FIELD2 ...
23
Dec-2008
24
Dec-2008
25
Dec-2008
26
Dec-2008
27
Dec-2008
28
Dec-2008
MODIFY <internal table> FROM <work area>. READ TABLE <internal table> INTO <work area>. LOOP AT <internal table> INTO <work area>.
29
Dec-2008
CLEAR <internal table> Initialises the header line. REFRESH <internal table>
30
Dec-2008
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB. DESCRIBE TABLE EMPTAB LINES LINE_COUNT OCCURS INITIAL_COUNT. WRITE: / lines:, LINE_COUNT, / occurs:, INITIAL SIZE_COUNT.
screen output
31
Dec-2008
ENDIF.
screen output
32 Data Structure & Internal Tables | 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.
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