Abap-28 - Data Structures and Internal Tables
Abap-28 - Data Structures and Internal Tables
Objectives
The
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
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.
Declaring a Structure - Method Is this statement REPORT YN1C0008. necessary for the #1 code? 2
Basic Syntax: TABLES: TABNA. DATA: BEGIN OF ADDRESS, DATA: BEGIN OF <name> FLAG TYPE C, <field1> . . . ID LIKE TABNA-ID, NAME1 LIKE TABNA-NAME1, <field2> . . . CITY LIKE TABNA-CITY, ... END OF ADDRESS. MOVE X TO ADDRESS-FLAG. END OF <name>. MOVE 0001 TO ADDRESS-ID. MOVE Smith TO ADDRESS-NAME1. MOVE Philadelphia TO ADDRESS- CITY. Address Structure WRITE ADDRESS. Flag ID Name1 City
3 4 5 6 7 8 9 10 11 12 13 14 15
16 17
Declaring a Structure Method Basic Syntax: REPORT Yxxxxxxx. #2BEGIN OF ADDR, TYPES: BEGIN OF <name1>, TYPES:
FLAG, ID NAME1 CITY LIKE EMPLOYEE-ID, LIKE EMPLOYEE-NAME1, LIKE EMPLOYEE-CITY, <field1> . . . , <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, Philadelphia TO ADDRESS-CITY. WRITE ADDRESS.
Address Structure
Flag ID Name1 City
Populating a Structure with REPORT Y170DM37. Field-by-Field Transport EMPLOYEE TABLES: EMPLOYEE.
DATA: BEGIN OF ADDRESS, 000000001 FLAG, Address ID LIKE EMPLOYEE-ID, NAME LIKE EMPLOYEE-NAME1,
Flag ID Name1 Electronics Inc. ID 000000001 Name
City
Waldorf
City Waldorf
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.
Clear <f1>.
Demonstration
Declaring
Practice
Declaring
Creating an Internal Table with REPORT Y170DM38. TABLES: EMPLOYEE. The TYPES statement defines Header Line the structure and data type for
TYPES: BEGIN OF EMP, ID LIKE EMPLOYEE-ID, NAME1 LIKE EMPLOYEE-NAME1, COUNTRY LIKE EMPLOYEE-COUNTRY, END OF EMP. DATA: EMPTAB TYPE STANDARD TABLE the internal table. The DATA statement with an INITIAL SIZE 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. ID NAME1 COUNTRY
OF EMP INITIAL SIZE 10 WITH Header Line HEADER LINE. SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. APPEND EMPTAB. ENDSELECT.
10
Implicit
All
character fields
Explicit
User-defined
11
12
APPEND <int. table>. Department R&D MKTG SALES PROD IT HR Salary 400,000 1,000,000 500,000 7,800,000 50,000 140,000
1 2 3 4 5 6
APPEND <int. table> SORTED BY <field>. Department R&D PROD MKTG SALES HR IT Salary 400,000 7,800,000 1,000,000 500,000 140,000 50,000 Header
13
Loading an Internal Table with With both versions of the APPEND statement, a Header Line memory space for ten
REPORT Y170DM42. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, ID SALARY COUNTRY LIKE EMPLOYEE-COUNTRY, LIKE EMPLOYEE-ID, LIKE EMPLOYEE-SALARY,
records is allocated when the first record is written to the internal table. Example 1 More than ten entries can be saved in the internal table. Example 2 A maximum of ten entries can be saved in the internal table. Any entries that exceed the top ten will be deleted.
END OF EMP. DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH HEADER LINE. SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. APPEND EMPTAB.
OR
14
COUNTRY
ID
FORMA
NAME1
SORTL
. . .
ID
NAME1
COUNTRY
Header Line
15
COUNTRY USA
ID
FORMA
NAME1
SORTL
. . . . . .
ID
NAME1
16
COUNTRY USA
ID
FORMA
NAME1
SORTL
. . . . . .
00000001 Company Baker Distributors BAKER 2 ID NAME1 COUNTRY USA 00000001 Baker Distributors
Header Line
17
18
19
REPORT Y170DM40.
TABLES: EMPLOYEE.
Creating an Internal Table The TYPES statement defines without a Header Line the structure and data type for
LIKE EMPLOYEE-ID, LIKE EMPLOYEE-NAME1,
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
SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB_WA. APPEND EMPTAB_WA TO EMPTAB. ENDSELECT.
ID
NAME1
COUNTRY
20
Performance Issues
21
COUNTRY
ID
FORMA
NAME1
SORTL
. . .
ID B
NAME1
COUNTRY
Work Area
22
1 2 3 . . . 10
This work area is not attached to the body of the internal table.
23
The internal table EMPTAB will have the exact same structure as the dictionary table EMPLOYEE.
24
MOVE
DATA: EMPTAB LIKE STANDARD TABLE EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.
26
REPORT Y170DM45.
TABLES: EMPLOYEE.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH HEADER LINE. SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. APPEND EMPTAB. ENDSELECT. LOOP AT EMPTAB WHERE COUNTRY BETWEEN A AND D. WRITE: / EMPTAB-COUNTRY, EMPTAB-NAME1, EMPTAB-SALES. ENDLOOP. IF SY-SUBRC NE 0. WRITE: / NO ENTRIES. ENDIF.
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.
27
REPORT Y170DM46.
TABLES: EMPLOYEE.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH HEADER LINE. PARAMETERS: START LIKE SY-TABIX DEFAULT 10, END LIKE SY-TABIX DEFAULT 20. SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. APPEND EMPTAB. ENDSELECT. LOOP AT EMPTAB FROM START TO END. WRITE: / SY-TABIX, EMPTAB-COUNTRY, EMPTAB-NAME1. ENDLOOP.
28
Screen output
SY-TABIX
REPORT Y170DM43.
TABLES: EMPLOYEE.
Country
D USA GB D A CH D F GB NL NO USA HK
Sales
400,000 1,000,000 500,000 7,800,000
Header Line
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH HEADER LINE. SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. COLLECT EMPTAB. ENDSELECT. LOOP AT EMPTAB. WRITE: / EMPTAB-COUNTRY, EMPTAB-SALES. ENDLOOP.
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
29
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 INITIAL SIZE 10 WITH HEADER LINE. SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. APPEND EMPTAB. ENDSELECT. SORT EMPTAB BY SALES DESCENDING. LOOP AT EMPTAB.
screen output
30
AT
FIRST AT NEW < field > AT END < field > AT LAST
31
32
33
SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. MODIFY <EMPTAB> INDEX <i>. APPEND EMPTAB. ENDSELECT. READ TABLE EMPTAB INDEX 1. MOVE ABC TO EMPTAB-NAME1. MODIFY EMPTAB INDEX SY-TABIX. IF SY-SUBRC NE 0. WRITE / Attempt to modify failed.. ELSE. WRITE: / EMPTAB-COUNTRY, EMPTAB-NAME1. ENDIF. INSERT EMPTAB INDEX 1. DELETE EMPTAB INDEX SY-TABIX.
34
Working with an Internal Table APPEND <work area> TO Line <internal table>. without a Header
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>.
35
Deletes all table lines. Storage space is not released. Paging is released. Header line remains unchanged.
Deletes all table lines. Storage space is released. Header line remains unchanged
36
SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. APPEND EMPTAB. ENDSELECT. DESCRIBE TABLE EMPTAB LINES LINE_COUNT OCCURS INITIAL_COUNT. WRITE: / lines:, LINE_COUNT, / occurs:, INITIAL SIZE_COUNT.
screen output
37
screen output
38
Demonstration
Declaring
an internal table, populating it by selecting data from the table and then looping into it and displaying the data fetched.
39
Practice
Declaring
an internal table, populating it by selecting data from the table and then looping into it and displaying the data fetched.
40
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 header line to the end of the internal table. the system field SY-TABIX is set to the line number of the entry read.
Summary
41
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.
42
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.
Questions
43
Thank You!