Sap-Abap Basic
Sap-Abap Basic
tables
SAP - ABAP
Topics
2
Objectives
The participants will be able to:
3
Data Structures
LN FN City ST.
LN FN City ST.
4
Declaring a Structure - Method #1
Is this statement
1 REPORT YN1C0008. necessary for the
2 code?
3 TABLES: TABNA. Basic Syntax:
4 DATA: BEGIN OF ADDRESS, DATA: BEGIN OF <name>
5 FLAG TYPE C, <field1> . . .
6 ID LIKE TABNA-ID, <field2> . . .
7 NAME1 LIKE TABNA-NAME1, ...
8 CITY LIKE TABNA-CITY, END OF <name>.
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. Address Structure
15 WRITE ADDRESS. Flag ID Name1 City
16
17
5
5
Declaring a Structure - Method #2
REPORT Yxxxxxxx. Basic Syntax:
TYPES: BEGIN OF ADDR, TYPES: BEGIN OF <name1>,
FLAG, <field1> . . . ,
ID LIKE EMPLOYEE-ID, <field2> . . . ,
NAME1 LIKE EMPLOYEE-NAME1, ... ,
CITY LIKE EMPLOYEE-CITY, END OF <name1>.
END OF ADDR. DATA: <name2> TYPE
DATA: ADDRESS TYPE ADDR. <name1>.
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
6
6 Data Structure & Inter3.07
Populating a Structure with Field-by-Field Transport
REPORT Y170DM37.
TABLES: EMPLOYEE. EMPLOYEE
DATA: BEGIN OF ADDRESS, ID Name1 City
FLAG,
000000001 Electronics Inc. Waldorf
ID LIKE EMPLOYEE-ID,
Address MOVE-CORRESPONDING EMPLOYEE
NAME LIKE EMPLOYEE-NAME1, TO ADDRESS.
CITY LIKE EMPLOYEE-CITY,
Flag ID Name City
END OF ADDRESS.
SELECT * FROM EMPLOYEE. 000000001 Waldorf
MOVE-CORRESPONDING EMPLOYEE Clear <f1>.
TO ADDRESS.
WRITE: / ADDRESS-FLAG,
ADDRESS-ID, ADDRESS-NAME,
ADDRESS-CITY.
CLEAR ADDRESS.
ENDSELECT.
7
7 Data Structure & Internal Tables | 3.07
Demonstration
Declaring a structure and populating the structure with values inside a program.
8
8 Data Structure & Internal Tables | 3.07
Practice
Declaring a structure and populating the structure with values inside a program.
9
9 Data Structure & Internal Tables | 3.07
Internal Table Types
Standard
Sorted
Hashed
10
10 Data Structure & Internal Tables | 3.07
Creating an Internal Table with Header Line
REPORT Y170DM38.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP, The TYPES statement defines the
ID LIKE EMPLOYEE-ID,NAME1 structure and data type for the
LIKE EMPLOYEE-NAME1, internal table.
COUNTRY LIKE The DATA statement with an
EMPLOYEE-COUNTRY, INITIAL SIZE creates the actual
END OF EMP. internal table capable of storing
DATA: EMPTAB TYPE STANDARD TABLE data. Because of the WITH
OF EMP INITIAL SIZE 10 WITH HEADER LINE addition, this
HEADER LINE. internal table is created with a
header line.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO ID NAME1 COUNTRY
EMPTAB. Header Line
APPEND EMPTAB.
ENDSELECT.
11
11 Data Structure & Internal Tables | 3.07
Size of an Internal Table
12
12 Data Structure & Internal Tables | 3.07
Loading an Internal Table with a Header Line
13
13 Data Structure & Internal Tables | 3.07
Loading an Internal Table with a Header Line
REPORT Y170DM42.
TABLES: EMPLOYEE. With both versions of the
TYPES: BEGIN OF EMP, APPEND statement, memory
space for ten records is
COUNTRY LIKE EMPLOYEE-COUNTRY,
allocated when the first record
ID LIKE EMPLOYEE-ID, is written to the internal table.
SALARY LIKE EMPLOYEE-SALARY,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE Example 1
OF EMP INITIAL SIZE 10 WITH HEADER LINE.
More than ten entries can be
SELECT * FROM EMPLOYEE. saved in the internal table.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
Example 2
APPEND EMPTAB.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
A maximum of ten entries
APPEND EMPTAB SORTED BY SALARY. can be saved in the internal
OR
ENDSELECT. table. Any entries that
exceed the top ten will
be deleted.
14
14 Data Structure & Internal Tables | 3.07
Loading an Internal Table with a Header Line
REPORT Y170DM42.
TABLES: EMPLOYEE. With both versions of the
TYPES: BEGIN OF EMP, APPEND statement, memory
space for ten records is
COUNTRY LIKE EMPLOYEE-COUNTRY,
allocated when the first record
ID LIKE EMPLOYEE-ID, is written to the internal table.
SALARY LIKE EMPLOYEE-SALARY,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE Example 1
OF EMP INITIAL SIZE 10 WITH HEADER LINE.
More than ten entries can be
SELECT * FROM EMPLOYEE. saved in the internal table.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
Example 2
APPEND EMPTAB.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
A maximum of ten entries
APPEND EMPTAB SORTED BY SALARY. can be saved in the internal
OR
ENDSELECT. table. Any entries that
exceed the top ten will
be deleted.
15
15 Data Structure & Internal Tables | 3.07
Internal Table with Header Line
EMPLOYEE
A
COUNTRY ID FORMA NAME1 SORTL . . .
ID NAME1 COUNTRY
B Header Line
16
16 Data Structure & Internal Tables | 3.07
Internal Table with Header Line
1 EMPLOYEE
ID NAME1 COUNTRY
Header Line
17
17 Data Structure & Internal Tables | 3.07
Internal Table with Header Line
1 EMPLOYEE
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line
18
18 Data Structure & Internal Tables | 3.07
Internal Table with Header Line
1 EMPLOYEE
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line
19
19 Data Structure & Internal Tables | 3.07
Internal Table with Header Line
4 EMPLOYEE
5
ID NAME1 COUNTRY
00000002 Diversified Indust... USA Header Line
6 2
00000002 Diversified Indust... USA
3
. .
. .
. .
10
20
20 Data Structure & Internal Tables | 3.07
Creating an Internal Table without a Header Line
The TYPES statement defines the
structure and data type for the
REPORT Y170DM40. internal table and its work area
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
ID LIKE EMPLOYEE-ID,
NAME1 LIKE EMPLOYEE-NAME1, The DATA statement with an INITIAL
COUNTRY LIKE EMPLOYEE-COUNTRY, SIZE creates the actual internal table
END OF EMP. without a header line. The DATA
statement without the INITIAL SIZE
DATA: EMPTAB TYPE STANDARD TABLE creates the work area for the internal
OF EMP INITIAL SIZE 10, table.
EMPTAB_WA TYPE EMP.
Performance Issues
22
22 Data Structure & Internal Tables | 3.07
Internal Table without a Header Line
EMPLOYEE
A
COUNTRY ID FORMA NAME1 SORTL . . .
ID NAME1 COUNTRY
B Work Area
23
23 Data Structure & Internal Tables | 3.07
Internal Table without a Header Line
1 EMPLOYEE
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Work Area
ID NAME1 COUNTRY
00000001 Baker Distributors USA 1
3
This work area is
2 not attached to
the body of the
3 internal table.
.
.
.
10
24
24 Data Structure & Internal Tables | 3.07
Automatic Field Conversion
– Structure to structure
– Field to structure
– Structure to field
• Intermediate C type
• Followed by adoption of new types
25
25 Data Structure & Internal Tables | 3.07
Mass Reading from Database Tables into Internal Tables
REPORT Y170DM69.
TABLES: EMPLOYEE.
SELECT * FROM <table> . . .
DATA: EMPTAB LIKE STANDARD TABLE EMPLOYEE INITIAL 1. INTO TABLE <EMPTAB>.
SIZE 10 WITH HEADER LINE. 2. APPENDING TABLE <EMPTAB>.
Notice no ENDSELECT is
needed here because no loop
processing occurs.
26
26 Data Structure & Internal Tables | 3.07
Processing an Internal Table
REPORT Y170DM45.
TABLES: EMPLOYEE.
This LOOP AT <EMPTAB>
TYPES: BEGIN OF EMP, statement allows for a logical
COUNTRY LIKE EMPLOYEE-COUNTRY, expression in a WHERE clause to
NAME1 LIKE EMPLOYEE-NAME1,
SALES LIKE EMPLOYEE-SALES,
limit the processing of the internal
END OF EMP. table.
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, If no internal table entries
EMPTAB-SALES. qualify under the logical
ENDLOOP.
IF SY-SUBRC NE 0. expression, the statement
WRITE: / ‘NO ENTRIES’. within the loop is not executed
ENDIF. and SY-SUBRC is set to 4.
27
27 Data Structure & Internal Tables | 3.07
System Field SY-TABIX
REPORT Y170DM46.
TABLES: EMPLOYEE.
AT FIRST
AT NEW < field >
AT END < field >
AT LAST
31
31 Data Structure & Internal Tables | 3.07
Reading a Single Table Entry
REPORT Y170DM47.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMPTAB.
READ TABLE ….
32
32 Data Structure & Internal Tables | 3.07
Reading a Single Table Entry - Options
READ TABLE <EMPTAB> options:
1) READ TABLE <EMPTAB>.
2) READ TABLE <EMPTAB> WITH KEY <k1> = <v1>…
<kn> = <vn>.
3) READ TABLE <EMPTAB> WITH TABLE KEY <k1> = <v1> ...
<kn> = <vn>.
4) READ TABLE <EMPTAB> WITH KEY = <value>.
5) READ TABLE <EMPTAB> WITH KEY . . . BINARY SEARCH.
6) READ TABLE <EMPTAB> INDEX <i>.
7) READ TABLE <EMPTAB> COMPARING <f1> <f2> . . . .
8) READ TABLE <EMPTAB> COMPARING ALL FIELDS.
9) READ TABLE <EMPTAB> TRANSPORTING <f1> <f2> . . . .
10) READ TABLE <EMPTAB> TRANSPORTING NO FIELDS.
33
33 Data Structure & Internal Tables | 3.07
Maintaining Internal Tables
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. INSERT <EMPTAB> INDEX <i>.
APPEND EMPTAB. MODIFY <EMPTAB> INDEX <i>.
ENDSELECT. DELETE <EMPTAB> INDEX <i>.
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, Check SY-SUBRC after
EMPTAB-NAME1. every attempt to change
ENDIF. an internal table entry.
INSERT EMPTAB INDEX 1.
DELETE EMPTAB INDEX SY-TABIX.
34
34 Data Structure & Internal Tables | 3.07
Working with an Internal Table without a Header Line
35
35 Data Structure & Internal Tables | 3.07
Deleting an Internal Table
36
36 Data Structure & Internal Tables | 3.07
Information about an Internal Table
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,
DESCRIBE TABLE <internal table>
LINE_COUNT TYPE I,
LINES <var1>
INITIAL_COUNT TYPE I.
OCCURS <var2>.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
DESCRIBE TABLE EMPTAB
LINES LINE_COUNT
OCCURS INITIAL_COUNT.
WRITE: / ‘ lines:’, LINE_COUNT, screen output
/ ‘occurs:’, INITIAL SIZE_COUNT.
37
37 Data Structure & Internal Tables | 3.07
Calling the SAP Table Editor
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
38
38 Data Structure & Internal Tables | 3.07
Demonstration
Declaring an internal table, populating it by selecting data from the table and then
looping into it and displaying the data fetched.
39
39 Data Structure & Internal Tables | 3.07
Practice
Declaring an internal table, populating it by selecting data from the table and then
looping into it and displaying the data fetched.
40
40 Data Structure & Internal Tables | 3.07
Summary
41
41 Data Structure & Internal Tables | 3.07
Summary (Contd.)
42
42 Data Structure & Internal Tables | 3.07
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.
43
43 Data Structure & Internal Tables | 3.07
Thanks
44