3. Objectives
3
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
5. Declaring a Structure - Method #1
5
5
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
14 ADDRESS- CITY.
15 WRITE ADDRESS.
16
17
Basic Syntax:
DATA: BEGIN OF <name>
<field1> . . .
<field2> . . .
. . .
END OF <name>.
Flag ID Name1 City
Address Structure
Is this statement
necessary for the
code?
6. Declaring a Structure - Method #2
6
6 Data Structure & Inter3.07
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
7. Populating a Structure with Field-by-Field Transport
7
7 Data Structure & Internal Tables | 3.07
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 <f1>.
8. Demonstration
8
8 Data Structure & Internal Tables | 3.07
Declaring a structure and populating the structure with values inside a program.
9. Practice
9
9 Data Structure & Internal Tables | 3.07
Declaring a structure and populating the structure with values inside a program.
11. Creating an Internal Table with Header Line
11
11 Data Structure & Internal Tables | 3.07
REPORT Y170DM38.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
ID LIKE EMPLOYEE-ID,NAME1
LIKE EMPLOYEE-NAME1,
COUNTRY LIKE
EMPLOYEE-COUNTRY,
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.
The TYPES statement defines the
structure and data type for 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.
Header Line
ID NAME1 COUNTRY
12. Size of an Internal Table
12
12 Data Structure & Internal Tables | 3.07
13. Loading an Internal Table with a Header Line
13
13 Data Structure & Internal Tables | 3.07
APPEND <int. table>
SORTED BY <field>.
APPEND <int. table>.
Department Salary
1
2
3
4
5
6
Department Salary
Header
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
14. Loading an Internal Table with a Header Line
14
14 Data Structure & Internal Tables | 3.07
REPORT Y170DM42.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
ID LIKE EMPLOYEE-ID,
SALARY LIKE EMPLOYEE-SALARY,
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.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB SORTED BY SALARY.
ENDSELECT.
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
15. Loading an Internal Table with a Header Line
15
15 Data Structure & Internal Tables | 3.07
REPORT Y170DM42.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
ID LIKE EMPLOYEE-ID,
SALARY LIKE EMPLOYEE-SALARY,
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.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB SORTED BY SALARY.
ENDSELECT.
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
16. Internal Table with Header Line
16
16 Data Structure & Internal Tables | 3.07
EMPLOYEE
COUNTRY ID FORMA NAME1 SORTL . . .
ID NAME1 COUNTRY
Header Line
A
B
17. Internal Table with Header Line
17
17 Data Structure & Internal Tables | 3.07
ID NAME1 COUNTRY
EMPLOYEE
USA 00000001 Company Baker Distributors BAKER . . .
COUNTRY ID FORMA NAME1 SORTL . . .
Header Line
1
18. Internal Table with Header Line
18
18 Data Structure & Internal Tables | 3.07
ID NAME1 COUNTRY
00000001 Baker Distributors USA
EMPLOYEE
Header Line
2
1
USA 00000001 Company Baker Distributors BAKER . . .
COUNTRY ID FORMA NAME1 SORTL . . .
19. Internal Table with Header Line
19
19 Data Structure & Internal Tables | 3.07
USA 00000001 Company Baker Distributors BAKER . . .
ID NAME1 COUNTRY
00000001 Baker Distributors USA
00000001 Baker Distributors USA
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
20. Internal Table with Header Line
20
20 Data Structure & Internal Tables | 3.07
ID NAME1 COUNTRY
00000001 Baker Distributors USA
00000002 Diversified Indust... USA
00000002 Diversified Indust... USA
USA 00000002 Company Diversified Indust.. DIVERS . . .
EMPLOYEE
COUNTRY ID FORMA NAME1 SORTL . . .
Header Line
5
6
1
2
3
10
.
.
.
.
.
.
4
21. 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: EMPTAB TYPE STANDARD TABLE
OF EMP INITIAL SIZE 10,
EMPTAB_WA TYPE EMP.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB_WA.
APPEND EMPTAB_WA TO 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
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.
22. Internal Table without a Header Line WHY???
22
22 Data Structure & Internal Tables | 3.07
Separate Internal Table Work Area
Performance Issues
Nested Internal Tables
23. Internal Table without a Header Line
23
23 Data Structure & Internal Tables | 3.07
EMPLOYEE
COUNTRY ID FORMA NAME1 SORTL . . .
Work Area
A
B
ID NAME1 COUNTRY
24. Internal Table without a Header Line
24
24 Data Structure & Internal Tables | 3.07
ID NAME1 COUNTRY
ID NAME1 COUNTRY
00000001 Baker Distributors USA
00000001 Baker Distributors USA
USA 00000001 Company Baker Distributors BAKER . . .
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.
25. Automatic Field Conversion
25
25 Data Structure & Internal Tables | 3.07
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
26. Mass Reading from Database Tables into Internal Tables
26
26 Data Structure & Internal Tables | 3.07
REPORT Y170DM69.
TABLES: EMPLOYEE.
DATA: EMPTAB LIKE STANDARD TABLE EMPLOYEE INITIAL
SIZE 10 WITH HEADER LINE.
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB
WHERE COUNTRY = ‘USA’.
SELECT * FROM <table> . . .
1. INTO TABLE <EMPTAB>.
2. APPENDING TABLE <EMPTAB>.
Notice no ENDSELECT is
needed here because no loop
processing occurs.
27. Processing an Internal Table
27
27 Data Structure & Internal Tables | 3.07
REPORT Y170DM45.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
SALES LIKE EMPLOYEE-SALES,
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.
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.
28. System Field SY-TABIX
28
28 Data Structure & Internal Tables | 3.07
REPORT Y170DM46.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP.
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.
Screen output
SY-TABIX
29. Accumulating Data within an Internal Table
29
29 Data Structure & Internal Tables | 3.07
REPORT Y170DM43.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
SALES LIKE EMPLOYEE-SALES,
END OF EMP.
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.
COLLECT <EMPTAB>.
Country Sales
D 400,000
USA 1,000,000
GB 500,000
D 7,800,000
Header Line
A 371,065.00
CH 45,305.00
D 8,200,000.00
F 0.00
GB 500,000.00
NL 577,000.00
NO 234.00
USA 1,000,000.00
HK 0.00
Screen output
30. Sorting an Internal Table
30
30 Data Structure & Internal Tables | 3.07
REPORT Y170DM44.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
SALES LIKE EMPLOYEE-SALES,
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.
WRITE: / ITAB-COUNTRY, ITAB-NAME1, ITAB-SALES.
ENDLOOP.
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
31. Control Level Processing
31
31 Data Structure & Internal Tables | 3.07
AT FIRST
AT NEW < field >
AT END < field >
AT LAST
32. Reading a Single Table Entry
32
32 Data Structure & Internal Tables | 3.07
REPORT Y170DM47.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMPTAB.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH
HEADER LINE.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
READ TABLE ….
34. Maintaining Internal Tables
34
34 Data Structure & Internal Tables | 3.07
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
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.
INSERT <EMPTAB> INDEX <i>.
MODIFY <EMPTAB> INDEX <i>.
DELETE <EMPTAB> INDEX <i>.
Check SY-SUBRC after
every attempt to change
an internal table entry.
35. Working with an Internal Table without a Header Line
35
35 Data Structure & Internal Tables | 3.07
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>.
36. Deleting an Internal Table
36
36 Data Structure & Internal Tables | 3.07
CLEAR <internal table>
Initialises the header line.
Internal table lines remain
unchanged.
REFRESH <internal table>
Deletes all table lines.
Storage space is not released.
Paging is released.
Header line remains unchanged.
FREE <internal table>
Deletes all table lines.
Storage space is released.
Header line remains
unchanged
37. Information about an Internal Table
37
37 Data Structure & Internal Tables | 3.07
REPORT Y170DM49.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH
HEADER LINE,
LINE_COUNT TYPE I,
INITIAL_COUNT TYPE I.
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.
DESCRIBE TABLE <internal table>
LINES <var1>
OCCURS <var2>.
screen output
38. Calling the SAP Table Editor
38
38 Data Structure & Internal Tables | 3.07
REPORT Y170DM50.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
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.
EDITOR-CALL FOR EMPTAB.
CHECK SY-SUBRC EQ 0.
LOOP AT EMPTAB WHERE NAME1 EQ ‘Maurice Cheeks’.
WRITE: / EMPTAB-COUNTRY, EMPTAB-NAME1.
ENDLOOP.
IF SY-SUBRC NE 0. WRITE: / ‘No records.’. ENDIF.
screen output
39. Demonstration
39
39 Data Structure & Internal Tables | 3.07
Declaring an internal table, populating it by selecting data from the table and then
looping into it and displaying the data fetched.
40. Practice
40
40 Data Structure & Internal Tables | 3.07
Declaring an internal table, populating it by selecting data from the table and then
looping into it and displaying the data fetched.
41. Summary
41
41 Data Structure & Internal Tables | 3.07
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.
42. Summary (Contd.)
42
42 Data Structure & Internal Tables | 3.07
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.
43. Questions
43
43 Data Structure & Internal Tables | 3.07
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.