Sie sind auf Seite 1von 44

Data structures and Internal tables

SAP - ABAP Click to edit Master subtitle style

3/18/13

Topics

Data structures and Internal tables

22

3/18/13

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

3/18/13

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.

3/18/13

Is this statement necessary for the 1 REPORT YN1C0008. code? 2 Basic Syntax: 3 TABLES: TABNA. DATA: BEGIN OF <name> 4 DATA: BEGIN OF <field1> . . . ADDRESS, <field2> . . . 5 FLAG TYPE C, ... 6 ID LIKE TABNA-ID, END OF <name>. 7 NAME1 LIKE TABNANAME1, 8 CITY LIKE TABNA-CITY, 9 END OF ADDRESS. 10 MOVE X TO ADDRESS-FLAG. 11 MOVE 0001TO ADDRESS-ID. 12 MOVE Smith TO ADDRESSAddress NAME1. Structure 13 MOVE Philadelphia TO Flag ID Name1 City 14 ADDRESS- CITY. 15 WRITE ADDRESS.
16 17 5 55

Declaring a Structure - Method #1

3/18/13

Declaring a Structure - Method #2


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>.

Address Structure
Flag ID Name1 City

6 66

Data Structure & Inter3.07

3/18/13

Populating a Structure with Field-by-Field Transport


REPORT Y170DM37. TABLES: EMPLOYEE. EMPLOYEE DATA: BEGIN OF ADDRESS, ID Name1 City FLAG, 000000001 Electronics Inc. ID LIKE EMPLOYEEAddress Waldorf MOVE-CORRESPONDING ID, EMPLOYEE NAME LIKE EMPLOYEETO Flag ID Name City NAME1, ADDRESS. CITY LIKE EMPLOYEE000000001 Waldorf CITY, Clear END OF ADDRESS. <f1>. SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO ADDRESS. WRITE: / ADDRESS-FLAG, ADDRESS-ID, ADDRESSNAME, ADDRESS-CITY. CLEAR ADDRESS. ENDSELECT.
Data Structure & Internal Tables | 3.07

7 77

3/18/13

Demonstration
w

Declaring a structure and populating the structure with values inside a program.

8 88

Data Structure & Internal Tables | 3.07

3/18/13

Practice
w

Declaring a structure and populating the structure with values inside a program.

9 99

Data Structure & Internal Tables | 3.07

3/18/13

Internal Table Types


w

Standard Sorted Hashed

10 1010

Data Structure & Internal Tables | 3.07

3/18/13

Creating an Internal Table with Header Line


REPORT Y170DM38. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, ID LIKE EMPLOYEE-ID, NAME1 LIKE EMPLOYEE-NAME1, COUNTRY LIKE EMPLOYEECOUNTRY, END OF EMP. DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH HEADER LINE. SELECT * FROM EMPLOYEE. Header MOVE-CORRESPONDING EMPLOYEE Line TO EMPTAB. APPEND EMPTAB. ENDSELECT.
11 1111

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 NAME1 line. ID COUNTRY

Data Structure & Internal Tables | 3.07

3/18/13

Size of an Internal Table

12 1212

Data Structure & Internal Tables | 3.07

3/18/13

Loading an Internal Table with a Header Line

APPEND <int. table>. Department Salary R&D MKTG SALES PROD IT HR 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 Salary Department <field>. R&D 400,000 PROD 7,800,000 MKTG 1,000,000 SALES 500,000 HR 140,000 IT 50,000

Hea der

13 1313

Data Structure & Internal Tables | 3.07

3/18/13

Loading an Internal Table with a Header Line


REPORT Y170DM42. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, COUNTRY ID LIKE EMPLOYEE-COUNTRY, LIKE EMPLOYEE-ID, END OF EMP. DATA: EMPTAB TYPE STANDARD TABLE LINE. OF EMP INITIAL SIZE 10 WITH HEADER

SALARY LIKE EMPLOYEE-SALARY,

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 More than ten entries can be saved in the internal table. Example 2

SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.

A maximum of ten APPEND EMPTAB. entries can be saved O in the internal table. MOVE-CORRESPONDING EMPLOYEE TO R Any entries that EMPTAB. exceed the top ten APPEND EMPTAB SORTED BY SALARY. 14 3/18/13 will ENDSELECT. be deleted. Data Structure & Internal Tables | 3.07 1414

Loading an Internal Table with a Header Line


REPORT Y170DM42. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, COUNTRY ID LIKE EMPLOYEE-COUNTRY, LIKE EMPLOYEE-ID, END OF EMP. DATA: EMPTAB TYPE STANDARD TABLE LINE. OF EMP INITIAL SIZE 10 WITH HEADER

SALARY LIKE EMPLOYEE-SALARY,

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 More than ten entries can be saved in the internal table. Example 2

SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.

A maximum of ten APPEND EMPTAB. entries can be saved O in the internal table. MOVE-CORRESPONDING EMPLOYEE TO R Any entries that EMPTAB. exceed the top ten APPEND EMPTAB SORTED BY SALARY. 15 3/18/13 will ENDSELECT. be deleted. Data Structure & Internal Tables | 3.07 1515

Internal Table with Header Line

EMPLOYE E

COUNTRY .

ID

FORMA

NAME1

SORTL

. .

ID

NAME1COUNTRY

Header Line

16 1616

Data Structure & Internal Tables | 3.07

3/18/13

Internal Table with Header Line

EMPLOYE E

COUNTRY ID FORMA NAME1 SORTL . . . USA 00000001CompanyBaker DistributorsBAKER

. . .

ID COUNTRY

NAME1 Header Line

17 1717

Data Structure & Internal Tables | 3.07

3/18/13

Internal Table with Header Line

EMPLOYE E

COUNTRY ID FORMA NAME1 SORTL . . . USA 00000001 Company Baker Distributors BAKER 2 ID NAME1 COUNTRY USA 00000001 Baker Distributors

. . .

Header Line

18 1818

Data Structure & Internal Tables | 3.07

3/18/13

Internal Table with Header Line


1 EMPLOYE E COUNTRY ID FORMA NAME1 SORTL . . . USA 00000001 Company Baker Distributors BAKER . . . 2 ID NAME1 COUNTRY Header Line 00000001 Baker Distributors 3 USA 00000001 Baker Distributors USA 1 2 3 . . . 10

. . .
19 1919

This header line is attached to the body of the internal table.

Data Structure & Internal Tables | 3.07

3/18/13

Internal Table with Header Line


4 COUNTRY ID FORMA . . USA . 00000002 Company . . . 5 ID COUNTRY 00000002 NAME1 Diversified Indust... 1 2 3 . . . 10 Header Line EMPLOYE E NAME1 SORTL Diversified Indust.. DIVERS

USA 00000001 Baker Distributors 6 USA 00000002 Diversified Indust... USA . . .


20 2020

Data Structure & Internal Tables | 3.07

3/18/13

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. Work ENDSELECT.

Creating an Internal Table without a Header Line


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.

Area

ID

NAME1

COUNTRY

APPEND <work area> to <EMPTAB>.

3/18/13

Internal Table without a Header Line WHY???

Separate Internal Table Work Area

Performance Issues

Nested Internal Tables

22 2222

Data Structure & Internal Tables | 3.07

3/18/13

Internal Table without a Header Line

EMPLOYE E A COUNTRY . . ID FORMA NAME1 SORTL .

ID COUNTRY

NAME1

Work Area

23 2323

Data Structure & Internal Tables | 3.07

3/18/13

Internal Table without a Header Line


1 EMPLOYE E COUNTRY ID FORMA NAME1 SORT . 00000001 . . USA Company Baker Distributors BAKER . . . ID NAME1 2 COUNTRY Work Area 00000001 Baker Distributors USA ID 00000001 USA NAME1 COUNTRY Baker Distributors 1 2 3 . . . 10
24 2424 Data Structure & Internal Tables | 3.07

This work area is not attached to the body of the internal table.

3/18/13

Automatic Field Conversion


w

MOVE-CORRESPONDING or MOVE field to field

Individual field type conversion Structure to structure Field to structure Structure to field

MOVE

Intermediate C type Followed by adoption of new types

25 2525

Data Structure & Internal Tables | 3.07

3/18/13

Mass Reading from Database Tables into Internal Tables

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.

26 2626

Data Structure & Internal Tables | 3.07

3/18/13

Processing an Internal Table


REPORT Y170DM45. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, COUNTRY LIKE EMPLOYEE-COUNTRY, NAME1 LIKE EMPLOYEE-NAME1, SALES LIKE EMPLOYEE-SALES, END OF EMP.

This LOOP AT <EMPTAB> statement allows for a logical expression in a WHERE clause to limit the processing of the internal 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, EMPTAB-SALES. ENDLOOP. IF SY-SUBRC NE 0. WRITE: / NO ENTRIES. ENDIF.

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 2727

Data Structure & Internal Tables | 3.07

3/18/13

System Field SY-TABIX


REPORT Y170DM46. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, COUNTRY NAME1 LIKE EMPLOYEE-COUNTRY, LIKE EMPLOYEE-NAME1,

END OF EMP. DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH HEADER LINE. PARAMETERS: 10, START LIKE SY-TABIX DEFAULT

Screen output

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, 28 EMPTAB-NAME1. Data Structure & Internal Tables | 3.07 2828 ENDLOOP.

SY-TABIX

3/18/13

Accumulating Data within an Internal Table


REPORT Y170DM43. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, COUNTRY COUNTRY, SALES LIKE EMPLOYEE-

COLLECT <EMPTAB>. Countr yD


US A G B D

LIKE EMPLOYEE-SALES,

END OF EMP. DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH HEADER LINE. SELECT * FROM EMPLOYEE.

A CH MOVE-CORRESPONDING EMPLOYEE TO 45,305.00 EMPTAB. D COLLECT EMPTAB. 8,200,000.00 ENDSELECT. F 0.00 GB 500,000.00 LOOP AT EMPTAB. NL 577,000.00 WRITE: / EMPTAB-COUNTRY, EMPTABNO 234.00 SALES. USA 29 3/18/13 ENDLOOP. 1,000,000.00 Data Structure & Internal Tables | 3.07 2929

0 1,000,00 0 500,00 0 7,800,00 0 Screen output 371,065.00

Sale s 400,00

Header Line

Sorting an Internal Table


REPORT Y170DM44. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, COUNTRY NAME1 SALES LIKE EMPLOYEE-COUNTRY, LIKE EMPLOYEE-NAME1, LIKE EMPLOYEE-SALES,

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 DATA: EMPTAB TYPE STANDARD TABLE OF EMP one or more fields within INITIAL SIZE 10 WITH HEADER LINE. the table.
END OF EMP. SELECT * FROM EMPLOYEE. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. APPEND EMPTAB. ENDSELECT. SORT EMPTAB BY SALES DESCENDING. LOOP AT EMPTAB. WRITE: / ITAB-COUNTRY, ITAB-NAME1, ITAB30 SALES. 3/18/13 Data Structure & Internal Tables | 3.07 3030 ENDLOOP.

screen output

Control Level Processing


w

AT FIRST AT NEW < field > AT END < field > AT LAST

31 3131

Data Structure & Internal Tables | 3.07

3/18/13

Reading a Single Table Entry


REPORT Y170DM47. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, COUNTRY LIKE EMPLOYEE-COUNTRY, 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 . LIKE EMPLOYEE-NAME1,

32 3232

Data Structure & Internal Tables | 3.07

3/18/13

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 3333

Data Structure & Internal Tables | 3.07

3/18/13

Maintaining Internal Tables


INSERT <EMPTAB> INDEX SELECT * FROM EMPLOYEE. <i>. MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. MODIFY <EMPTAB> INDEX APPEND EMPTAB. <i>. ENDSELECT. DELETE <EMPTAB> INDEX READ TABLE EMPTAB INDEX 1. <i>. 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.

Check SY-SUBRC after every attempt to change an internal table entry.

34 3434

Data Structure & Internal Tables | 3.07

3/18/13

Working with an Internal Table without a Header Line

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>.
35 3535 Data Structure & Internal Tables | 3.07

3/18/13

Deleting an Internal Table


CLEAR <internal table>

Initialises the header line. Internal table lines remain unchanged. REFRESH <internal table> FREE <internal table>

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 3636

Data Structure & Internal Tables | 3.07

3/18/13

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 LINE_COUNT TYPE I, table> INITIAL_COUNT TYPE I. LINES <var1> SELECT * FROM EMPLOYEE. OCCURS <var2>. 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. 37 3737

screen output

Data Structure & Internal Tables | 3.07

3/18/13

Calling the SAP Table Editor


REPORT Y170DM50. TABLES: EMPLOYEE. TYPES: BEGIN OF EMP, COUNTRY 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. LIKE EMPLOYEE-COUNTRY, LIKE EMPLOYEE-NAME1,

screen output

38 3838

Data Structure & Internal Tables | 3.07

3/18/13

Demonstration
w

Declaring an internal table, populating it by selecting data from the table and then looping into it and displaying the data fetched.

39 3939

Data Structure & Internal Tables | 3.07

3/18/13

Practice
w

Declaring an internal table, populating it by selecting data from the table and then looping into it and displaying the data fetched.

40 4040

Data Structure & Internal Tables | 3.07

3/18/13

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 header line to the end of the internal table. the system field SY-TABIX is set to the line number of the entry read.

41 4141

Data Structure & Internal Tables | 3.07

3/18/13

Summary (Contd.)
w

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 4242

Data Structure & Internal Tables | 3.07

3/18/13

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 4343

Data Structure & Internal Tables | 3.07

3/18/13

Thanks

44

3/18/13

Das könnte Ihnen auch gefallen