0% found this document useful (0 votes)
15 views37 pages

Chapter 07 - Data Structures & Internal Tables

The document provides a comprehensive guide on creating and managing data structures and internal tables in ABAP programming. It covers various methods for declaring structures, populating internal tables, and processing data, including examples and syntax. Additionally, it discusses different types of internal tables, performance considerations, and key operations such as sorting and reading entries.

Uploaded by

Aniruddha_Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views37 pages

Chapter 07 - Data Structures & Internal Tables

The document provides a comprehensive guide on creating and managing data structures and internal tables in ABAP programming. It covers various methods for declaring structures, populating internal tables, and processing data, including examples and syntax. Additionally, it discusses different types of internal tables, performance considerations, and key operations such as sorting and reading entries.

Uploaded by

Aniruddha_Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

IBM Global Services

Data structures and Internal tables

Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

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

2 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Data Structures

Structure Internal Table

Address List Address List

LN FN City ST. LN FN City ST.

LN FN City ST.

LN FN City ST.

3 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Declaring a Structure - Method #1

1 REPORT YN1C0008.
2
3 DATA: BEGIN OF ADDRESS, Basic Syntax:
4 FLAG TYPE C, DATA: BEGIN OF <name>
5 ID TYPE TABNA-ID,
6 NAME1 TYPE TABNA-NAME1, <field1> . . .
7 CITY TYPE TABNA-CITY, <field2> . . .
8 END OF ADDRESS.
9 MOVE ‘X’ TO ADDRESS-FLAG. ...
10 MOVE ‘0001’ TO ADDRESS-ID. END OF <name>.
11 MOVE ‘Smith’ TO ADDRESS-NAME1.
12 MOVE ‘Philadelphia’ TO
13 ADDRESS- CITY.
14 WRITE: ADDRESS-FLAG,ADDRESS-ID. Address Structure
15 ADDRESS-NAME1,ADDRESS-CITY Flag ID Name1 City

4 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Declaring a Structure - Method #2


REPORT Yxxxxxxx. Basic Syntax:
TYPES: BEGIN OF ADDR, TYPES: BEGIN OF <name1>,
FLAG, <field1> . . . ,
ID TYPE EMPLOYEE-ID, <field2> . . . ,
NAME1 TYPE EMPLOYEE-NAME1, ... ,
CITY TYPE EMPLOYEE-CITY, END OF <name1>.
END OF ADDR. DATA: <name2> TYPE
<name1>.
DATA: ADDRESS TYPE ADDR.
MOVE: ‘X’ TO ADDRESS-FLAG,
‘00001’ TO ADDRESS-ID,
Address Structure
‘Smith’ TO ADDRESS-NAME1, Flag ID Name1 City

‘Philadelphia’ TO ADDRESS-CITY.
WRITE: ADDRESS-ID, ADDRESS-NAME1,
ADDRESS-CITY.

5 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Populating a Structure with Field-by-Field Transport


REPORT Y170DM37.
TYPES: BEGIN OF ADDRESS, EMPLOYEE
FLAG TYPE C, ID Name1 City
ID TYPE EMPLOYEE-ID, 000000001 Electronics Inc. Waldorf
NAME TYPE EMPLOYEE-NAME1,
Address MOVE-CORRESPONDING EMPLOYEE
TO ADDRESS.
CITY TYPE EMPLOYEE-CITY,
Flag ID Name City
END OF ADDRESS.
000000001 Waldorf
DATA: ADDRESS TYPE ADDR.
Clear <f1>.
DATA: WA_EMPL TYPE EMPLOYEE.
SELECT * FROM EMPLOYEE INTO
WA_EMPL.
ADDRESS-ID = WA_EMPL-ID.
ADDRESS-NAME = WA_EMPL-NAME.
ADDRESS-CITY = WA_EMPL-CITY.
WRITE: / ADDRESS-FLAG,
ADDRESS-ID, ADDRESS-NAME,
ADDRESS-CITY.
CLEAR: ADDRESS,WA_EMPL.
ENDSELECT.
6 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation
IBM Global Services

Demonstration

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

7 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Practice

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

8 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Internal Table Types

 Standard
 Sorted
 Hashed

9 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Creating an Internal Table without a Header Line

REPORT Y170DM40. The TYPES statement defines


TYPES: BEGIN OF EMP, the structure and data type for
the internal table and its work
ID TYPE ID,
area
NAME1 TYPE NAME1,
COUNTRY TYPE COUNTRY,
END OF EMP.

ID NAME1 COUNTRY
DATA: EMPTAB TYPE STANDARD TABLE
OF EMP,
EMPTAB_WA TYPE EMP.
Work Area

SELECT * FROM EMPLOYEE INTO EMPTAB_WA.


APPEND EMPTAB_WA TO EMPTAB.
APPEND <work area> to <EMPTAB>.
ENDSELECT.

10 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Creating an Internal Table without a Header Line


REPORT Y170DM40.
The TYPES statement defines
TYPES: BEGIN OF EMP,
the structure and data type for
ID TYPE ID, the internal table and its work
NAME1 TYPE NAME1, area
COUNTRY TYPE COUNTRY, The DATA statement with an
END OF EMP. INITIAL SIZE creates the
actual internal table without a
header line. The DATA
DATA: EMPTAB TYPE STANDARD TABLE statement without the INITIAL
OF EMP INITIAL SIZE 10, SIZE creates the work area for
the internal table.
EMPTAB_WA TYPE EMP.

SELECT * FROM EMPLOYEE. Work Area ID NAME1 COUNTRY


MOVE-CORRESPONDING EMPLOYEE TO EMPTAB_WA.
APPEND EMPTAB_WA TO EMPTAB.
ENDSELECT. APPEND <work area> to <EMPTAB>.

11 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Internal Table without a Header Line WHY???

Separate Internal Table Work Area

Performance Issues

Nested Internal Tables

12 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Internal Table without a Header Line

EMPLOYEE

A
COUNTRY ID FORMA NAME1 SORTL . . .

ID NAME1 COUNTRY
B Work Area

13 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Internal Table without a Header Line


1 EMPLOYEE

COUNTRY ID FORMA NAME1 SORT . . .


USA 00000001 Company Baker Distributors BAKER . . .

2 ID NAME1 COUNTRY
00000001 Baker Distributors USA Work Area

ID NAME1 COUNTRY
00000001 Baker Distributors USA 1
3 This work area
2 is not attached
to the body of
the internal
3
. table.
.
.
10

14 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Transferring ABAP Dictionary Table Structures

REPORT Y170DM41.
DATA: EMPTAB TYPE STANDARD TABLE OF The internal table EMPTAB
EMPLOYEE, will have the exact same
WA_EMP TYPE EMPLOYEE. structure as the dictionary
table EMPLOYEE.

SELECT * FROM EMPLOYEE INTO WA_EMP.


MOVE WA_EMP TO EMPTAB.
APPEND WA_EMP TO EMPTAB.
ENDSELECT.

Notice the MOVE statement


instead of a MOVE-
CORRESPONDING.

15 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Automatic Field Conversion

 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

16 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Mass Reading from Database Tables into Internal Tables

REPORT Y170DM69.
SELECT * FROM <table> . . .
1. INTO TABLE <EMPTAB>.
DATA: EMPTAB TYPE STANDARD TABLE OF 2. APPENDING TABLE
EMPLOYEE.
<EMPTAB>.

SELECT * FROM EMPLOYEE INTO TABLE EMPTAB


WHERE COUNTRY = ‘USA’.
Notice no ENDSELECT is
needed here because no
loop processing occurs.

17 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Processing an Internal Table


This LOOP AT <EMPTAB>
REPORT Y170DM45.
statement allows for a logical
TYPES: BEGIN OF EMP,
expression in a WHERE clause
COUNTRY TYPE COUNTRY, to limit the processing of the
NAME1 TYPE NAME1, internal table.
SALES TYPE SALES,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP,
WA_EMP TYPE EMP
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.
LOOP AT EMPTAB INTO WA_EMP WHERE COUNTRY BETWEEN ‘A’
AND ‘D’.
WRITE: / WA_EMP-COUNTRY, WA_EMP-NAME1,
WA_EMP-SALES.
CLEAR WA_EMP. If no internal table
ENDLOOP. entries qualify under the
IF SY-SUBRC NE 0. logical expression, the
WRITE: / ‘NO ENTRIES’. statement within the
ENDIF. loop is not executed and
SY-SUBRC is set to 4.

18 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

System Field SY-TABIX


REPORT Y170DM46.

TYPES: BEGIN OF EMP,


COUNTRY TYPE COUNTRY,
NAME1 TYPE NAME1,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP .

PARAMETERS: START TYPE SY-TABIX DEFAULT 10,


END TYPE SY-TABIX DEFAULT 20.
Screen output
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.

LOOP AT EMPTAB INTO WA_EMP FROM START TO END.


WRITE: / SY-TABIX, WA_EMP-COUNTRY, WA_EMP-NAME1.
ENDLOOP. SY-TABIX

19 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Accumulating Data within an Internal Table


REPORT Y170DM43.
TYPES: BEGIN OF EMP,
COUNTRY TYPE COUNTRY,
COLLECT <EMPTAB>.
SALES TYPE SALES, Country Sales
END OF EMP. D 400,000
DATA: EMPTAB TYPE STANDARD TABLE OF USA 1,000,000
EMP, GB 500,000
WA_EMP TYPE EMP. D 7,800,000

Screen output
SELECT * FROM EMPLOYEE INTO WA_EMP.
A 371,065.00
MOVE WA_EMP TO EMPTAB. CH 45,305.00
COLLECT WA_EMP INTO EMPTAB. D 8,200,000.00
ENDSELECT. F 0.00
LOOP AT EMPTAB INTO WA_EMP.
GB 500,000.00
NL 577,000.00
WRITE: / WA_EMP-COUNTRY, WA_EMP- NO 234.00
SALES.
USA 1,000,000.00
ENDLOOP. HK 0.00
20 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation
IBM Global Services

Sorting an Internal Table


REPORT Y170DM44.
Sorting options:
TYPES: BEGIN OF EMP,
1) SORT <EMPTAB> - sorts the
COUNTRY TYPE COUNTRY,
entries of the internal table
NAME1 TYPE NAME1, <EMPTAB> in ascending
SALES TYPE SALES, order.
END OF EMP. 2) SORT <EMPTAB> BY <field> -
DATA: EMPTAB TYPE STANDARD TABLE OF EMP , sorts the table on one or more
WA_EMP TYPE EMP.
fields within the table.

SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.

SORT EMPTAB BY SALES DESCENDING.

LOOP AT EMPTAB INTO WA_EMP.


WRITE: / WA_EMP-COUNTRY, WA_EMP-NAME1,
WA_EMP-SALES. screen output
ENDLOOP.
21 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation
IBM Global Services

Creating an Internal Table with Header Line


REPORT Y170DM38.
TYPES: BEGIN OF EMP, The TYPES statement defines
ID TYPE ID, the structure and data type for
NAME1 TYPE NAME1, the internal table.
COUNTRY TYPE COUNTRY,
END OF EMP. The DATA statement with an
INITIAL SIZE creates the
DATA: EMPTAB TYPE STANDARD TABLE actual internal table capable
of storing data. Because of
OF EMP INITIAL SIZE 10 WITH the WITH HEADER LINE
HEADER LINE. addition, this internal table is
created with a header line.
SELECT * FROM EMPLOYEE INTO TABLE ID NAME1 COUNTRY
EMPTAB.
Header Line

22 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Internal Table Keys

 Implicit Key
 All character fields
 Explicit Key
 User-defined
e.g. WITH [ UNIQUE/NON-UNIQUE ] KEY FIELD1 FIELD2 ...

23 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Size of an Internal Table

24 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Control Level Processing

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

25 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Reading a Single Table Entry

REPORT Y170DM47.
TYPES: BEGIN OF EMP,
COUNTRY TYPE COUNTRY,
NAME1 TYPE NAME1,
END OF EMP.

DATA: EMPTAB TYPE STANDARD TABLE OF EMP.

SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.

READ TABLE ….

26 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

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.

27 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Maintaining Internal Tables


SELECT * FROM EMPLOYEE. INSERT <EMPTAB> INDEX <i>.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
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. Check SY-SUBRC after
WRITE: / EMPTAB-COUNTRY, every attempt to change
EMPTAB-NAME1. an internal table entry.
ENDIF.
INSERT EMPTAB INDEX 1.
DELETE EMPTAB INDEX SY-TABIX.

28 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

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

29 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Deleting an Internal Table

CLEAR <internal table>


 Initialises the header line.

 Internal table lines REFRESH <internal table>


remain unchanged.
FREE <internal table>
 Deletes all table lines.
 Storage space is not
released.  Deletes all table lines.
 Paging is released.  Storage space is
released.
 Header line remains
unchanged.  Header line remains
unchanged

30 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Information about an Internal Table


REPORT Y170DM49.
TYPES: BEGIN OF EMP,
COUNTRY TYPE COUNTRY,
NAME1 TYPE NAME1,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP,
LINE_COUNT TYPE I,
INITIAL_COUNT TYPE I.
DESCRIBE TABLE <internal table>
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.
LINES <var1>
OCCURS <var2>.
DESCRIBE TABLE EMPTAB
LINES LINE_COUNT
OCCURS INITIAL_COUNT.
WRITE: / ‘ lines:’, LINE_COUNT,
/ ‘occurs:’, INITIAL SIZE_COUNT.

screen output

31 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Calling the SAP Table Editor


REPORT Y170DM50.

TYPES: BEGIN OF EMP,


COUNTRY TYPE COUNTRY,
NAME1 TYPE NAME1,
END OF EMP,
DATA: EMPTAB TYPE STANDARD TABLE OF EMP ,
WA_EMP TYPE EMP.
SELECT * FROM EMPLOYEE INTO TABLE EMPTAB.

EDITOR-CALL FOR EMPTAB.


CHECK SY-SUBRC EQ 0.
LOOP AT EMPTAB INTO WA_EMP WHERE NAME1 EQ ‘Maurice Cheeks’.
WRITE: / WA_EMP-COUNTRY, WA_EMP-NAME1.
ENDLOOP.
IF SY-SUBRC NE 0.
WRITE: / ‘No records.’.
ENDIF.
screen output

32 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Demonstration

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

33 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Practice

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

34 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

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

35 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

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.

36 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation


IBM Global Services

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.

37 Data Structure & Internal Tables | Dec-2008 © 2005 IBM Corporation

You might also like