Sei sulla pagina 1di 31

Introduction to Internal Tables

- Kunal Behary

2009 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

Objectives
Introduce the structure of an Internal Table Explore ways of defining an Internal Table in ABAP Explore ways of filling an Internal Table in ABAP Examine how to sort an Internal Table Examine how to retrieve lines from an Internal Table

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

What is an internal table ?


Internal tables provide a replica of tables or structure we working on and then modifying, changing or creating values in the database table in SAP.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

Obsolete Version Of Internal Table i.e. with header line


Example: DATA : BEGIN OF itab1 OCCURS 10, f1, f2, f3, END OF itab1. DATA itab2 LIKE ztxlfa1 OCCURS 100. DATA itab3 LIKE ztxlfa1 OCCURS 100 with header line. BEGIN OF creates header LIKE does not create a header line LIKE add WITH HEADER LINE creates a header

line.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

Internal Table without a Header Line


WHY???

Separate Internal Table Work Area

Performance Issues

Nested Internal Tables

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

Filling an internal table Example 1


Tables can be filled with data by: Reading data from a database table e.g

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

Filling an internal table Example 2

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

Filling an internal table (contd)

Inserting lines at a specified position.

Examples : insert vendtab index 3. The contents of the header line will be inserted before line 3.

insert lines of new_vend from 2 to 5 into vendtab index 3. Lines 2 to 5 of the internal table new_vend will be inserted before line 3 of vendtab.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

Filling an internal table (contd)


Moving complete tables Examples : move new_vend to vendtab. Note : If an internal table with a header line is involved, this header line (but not the internal table itself) is copied by move.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

Sorting an Internal Table


Data in an internal table can be sorted in a number of ways: Examples: data stud_tab like student occurs 10.
1) sort stud_tab. 2) sort stud_tab by studname, studid.

3) sort stud_tab by stud_id descending.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

10

Retrieving lines
Once an internal table has been filled, data can be retrieved by reading each line of the table using a loop, or reading individual lines. Examples : 1) loop at stud_tab. write / stud_tab-studid. write stud_tab-studname. endloop. 2) loop at stud_tab where studname = Smith. write / stud_tab-studid. endloop.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

11

Retrieving lines (contd)


3) read table stud_tab index 5. if sy-subrc = 0. write / stud_tab-studid. else. write / Record not found. endif.

4)

read table stud_tab with key studid = 3064537. if sy-subrc .


read table stud_tab with key studname = Smith course = BGCC. read table stud_tab with key studid = 3165432 binary search.

5)

6)

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

12

Changing an internal table


The contents of a given line in an internal table can be updated using the modify command. For example : read table stud_tab with key studid = 3354631. if sy-subrc = 0. stud_tab-course = BBBC. modify stud_tab index sy-tabix. endif.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

13

Changing an internal table - Example.


report zchgtabex. TABLES: zcustomers. DATA:

t_cust_info LIKE
STANDARD TABLE OF zcustomers. SELECT * FROM zcustomers INTO TABLE cust_info. SORT cust_info BY cnum. READ TABLE cust_info WITH KEY cnum = 3456755 BINARY SEARCH. IF sy-subrc = 0. cust_info-cphone = 9654-2345. MODIFY cust_info INDEX sy-tabix. ENDIF. LOOP AT cust_info. WRITE: / cust_info-cnum, cust_info-cname. ENDLOOP.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

14

Other table commands


clear fs_area. Clears the work area of the internal table. refresh tablename. Removes all the line records in a table (not the header) the storage remains. free tablename. Deletes the internal table and releases the storage space.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

15

Proprietary Information. DCDM Consulting. Not be disclosed without express written consent.

Advanced Internal Tables

Objectives
Revise the structure of internal tables Review the attributes of internal tables Describe the three internal table kinds: standard, sorted, hashed Examine the features and application of STANDARD internal tables Examine the features and application of SORTED internal tables Examine the features and application of HASHED internal tables Distinguish between the use of work areas Vs header lines when processing internal tables

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

17

Internal Tables as Dynamic Data Objects


f1
Itab

f2 XX

2
3
data: BEGIN OF itab OCCURS 10, f1, f2, f3, END OF itab.

B
C

YY
YY

LOOP AT itab. write: / sy-tabix, itab-f1, itab-f2. ENDLOOP. WRITE: / 'done. sy-tabix =', sy-tabix, / ' sy-subrc =', sy-subrc. OUTPUTS: 1 A XX 2 B YY 3 C YY sy-tabix = 99 sy-subrc = 0

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

18

Attributes of an Internal Table

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

19

Internal Table Kinds


Internal Tables can be divided into three table kinds, depending on the possible access type required:

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

20

Defining Internal Tables - Standard

DATA: itab TYPE STANDARD TABLE OF sbook WITH NON-UNIQUE KEY bookid.

DATA:

itab TYPE TABLE OF scarr.


2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

21

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

22

Defining Internal Tables: Sorted & Hashed

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

23

Work Area or Header Line

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

24

Hashed Internal Table

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

25

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

26

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

27

Internal Table Restricting Rows


Restricting rows read from an internal table.
Using FROM, TO and WHERE - WHERE returns a subset but always performs a full table scan LOOP AT itab WHERE f2= YY LOOP AT itab from 2 to 3.

Use EXIT, CONTINUE or CHECK to control LOOP AT.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

28

Internal Table single-record access

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

29

Internal Table Kinds - Summary


Standard table
Standard tables are best when you access data using an index, that is, the order of the data records is important but the sorting and the uniqueness are not crucial. If you decide you need to sort the table or access it using the key or a binary search, you can always program these functions by hand.

Sorted table
When you choose to use a sorted table, it will normally be because you want to define and use a unique key. When a sorted table is created the data is first sorted and then inserted. If you have a table with few entries but lots of accesses that change the contents, a sorted table may be more efficient than a standard table in terms of runtime because of binary search access.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

30

Internal Table Kinds Summary (contd)

Hashed table
The hash algorithm calculates the address of an entry based on the key. This means that, with larger tables, the access time is reduced significantly in comparison with a binary search. But when you use a loop with a hashed table, the whole table has to be scanned. Because the data records are usually totally unsorted, a sorted table may be more useful if you have a loop over the beginning of a key. Alternatively, you could also sort the hashed table. Hashed table is only beneficial if you want to keep large amounts of data locally in the program and you mostly access it only to read it. You must ensure that you design your hashed table so that it is possible to specify the full key when you access it from your program. Typical use for hashed tables is for buffering or bundling large amounts of data from several database tables when an ABAP Dictionary view or a nested SELECT statement is not possible.

2010 DCDM. All Rights Reserved. Proprietary Information. Not to be disclosed without prior written agreement.

31

Potrebbero piacerti anche