+ All Categories
Home > Technology > Sap abap-data structures and internal tables

Sap abap-data structures and internal tables

Date post: 14-Aug-2015
Category:
Upload: mustafa-nadim
View: 104 times
Download: 9 times
Share this document with a friend
44
Data structures and Internal tables SAP - ABAP
Transcript

Data structures and Internal tables

SAP - ABAP

Topics

2

Data structures and Internal tables

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

Data Structures

4

Address List

Structure

Address List

Internal Table

LN FN City ST. LN FN City ST.

LN FN City

ST.

LN FN City ST.

Declaring a Structure - Method #1

55

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

Basic Syntax:DATA: BEGIN OF <name>

<field1> . . . <field2> . . . . . .

END OF <name>.

Flag ID Name1 City

Address Structure

Is this statement necessary for the code?

Declaring a Structure - Method #2

66 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

Populating a Structure with Field-by-Field Transport

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

Demonstration

88 Data Structure & Internal Tables | 3.07

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

Practice

99 Data Structure & Internal Tables | 3.07

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

Internal Table Types

1010 Data Structure & Internal Tables | 3.07

Standard

Sorted

Hashed

Creating an Internal Table with Header Line

1111 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

Size of an Internal Table

1212 Data Structure & Internal Tables | 3.07

Loading an Internal Table with a Header Line

1313 Data Structure & Internal Tables | 3.07

APPEND <int. table> SORTED BY <field>.

APPEND <int. table>.

Department Salary

123456

Department SalaryHeader

R&D 400,000PROD 7,800,000MKTG 1,000,000SALES 500,000HR 140,000IT 50,000

R&D 400,000MKTG 1,000,000SALES 500,000PROD 7,800,000IT 50,000HR 140,000

Loading an Internal Table with a Header Line

1414 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

Loading an Internal Table with a Header Line

1515 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

Internal Table with Header Line

1616 Data Structure & Internal Tables | 3.07

EMPLOYEE

COUNTRY ID FORMA NAME1 SORTL . . .

ID NAME1 COUNTRY

Header Line

A

B

Internal Table with Header Line

1717 Data Structure & Internal Tables | 3.07

ID NAME1 COUNTRY

EMPLOYEE

USA 00000001CompanyBaker DistributorsBAKER . . .

COUNTRY ID FORMA NAME1 SORTL . . .

Header Line

1

Internal Table with Header Line

1818 Data Structure & Internal Tables | 3.07

ID NAME1 COUNTRY00000001 Baker Distributors USA

EMPLOYEE

Header Line

2

1

USA 00000001CompanyBaker DistributorsBAKER . . .

COUNTRY ID FORMA NAME1 SORTL . . .

Internal Table with Header Line

1919 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

Internal Table with Header Line

2020 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

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.

Internal Table without a Header Line WHY???

2222 Data Structure & Internal Tables | 3.07

Separate Internal Table Work Area

Performance Issues

Nested Internal Tables

Internal Table without a Header Line

2323 Data Structure & Internal Tables | 3.07

EMPLOYEE

COUNTRY ID FORMA NAME1 SORTL . . .

Work Area

A

B

ID NAME1 COUNTRY

Internal Table without a Header Line

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

Automatic Field Conversion

2525 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

Mass Reading from Database Tables into Internal Tables

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

Processing an Internal Table

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

System Field SY-TABIX

2828 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

Accumulating Data within an Internal Table

2929 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.00CH 45,305.00D 8,200,000.00F 0.00GB 500,000.00NL 577,000.00NO 234.00USA 1,000,000.00HK 0.00

Screen output

Sorting an Internal Table

3030 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

Control Level Processing

3131 Data Structure & Internal Tables | 3.07

AT FIRST

AT NEW < field >

AT END < field >

AT LAST

Reading a Single Table Entry

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

Reading a Single Table Entry - Options

3333 Data Structure & Internal Tables | 3.07

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.

Maintaining Internal Tables

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

Working with an Internal Table without a Header Line

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

Deleting an Internal Table

3636 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

Information about an Internal Table

3737 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 EMPTABLINES LINE_COUNTOCCURS INITIAL_COUNT.

WRITE: / ‘ lines:’, LINE_COUNT,

/ ‘occurs:’, INITIAL SIZE_COUNT.

DESCRIBE TABLE <internal table> LINES <var1>

OCCURS <var2>.

screen output

Calling the SAP Table Editor

3838 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

Demonstration

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

Practice

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

Summary

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

Summary (Contd.)

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

Questions

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

44

Thanks


Recommended