lecture12 abap on line

12
Lecture 12 Advanced Internal Tables BCO5647 Applications Programming Techniques (ABAP)

Upload: mkpatil

Post on 31-Oct-2014

20 views

Category:

Technology


7 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Lecture12 abap on line

Lecture 12Advanced Internal Tables

BCO5647 Applications Programming Techniques (ABAP)

Page 2: Lecture12 abap on line

2BCO5647

Readings & Objectives

Readings

Keller & Keller Chapter 4Section 4.7.1 & 4.7.3

Objectives

This lecture will

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 processinginternal tables

Page 3: Lecture12 abap on line

3BCO5647

Internal Tables as Dynamic Data Objects

Internal tables are data objects that allow you to store datasets with a fixed structure in memory. The data is stored row by row in memory. Each row has the same structure. You can also refer to each component in a row as a column in the internal table. You refer to each row in the internal table as a table row or table entry.

Internal tables are dynamic data objects that can hold any number of rows of a fixed type. The number of rows in an internal table is limited only by the capacity of the specific system with which you are working.

Page 4: Lecture12 abap on line

4BCO5647

Attributes of an Internal Table

The data type of an internal table is fully specified by its:

Row type The row type defines the attributes of the individual columns. You normally enter a structure type, but any

data type is possible.

Key definition The key columns and their sequence determine the criteria by which the system identifies table rows. You can

define the key as either UNIQUE or NON-UNIQUE. The uniqueness of the key must be compatible with the access type you have chosen for the table. If the key is unique, there can be no duplicate entries in the table.

Access type Key access – like database tables – works via field contents.

Index access, unlike for database tables, the system assigns row numbers to certain kinds of internal tables. This means that you can sometimes use the index to access rows in the table.

Page 5: Lecture12 abap on line

5BCO5647

Internal Table Kinds

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

Standard Tables – the row number is maintained internally. Both index and key access are possible.

Sorted Tables – Data records are sorted by the key and stored in these tables. Their index is also maintained internally. Both index and key access are possible.

Hashed Tables – The runtime-optimized data records are maintained here. This requires a unique key. Hashed tables only allow for key access.

Page 6: Lecture12 abap on line

6BCO5647

Defining Internal Tables - Standard

Example 1:

Example 2:DATA: itab TYPE STANDARD TABLE OF scarr WITH UNIQUE KEY carrid.

Example 3:DATA: itab TYPE TABLE OF scarr.

Page 7: Lecture12 abap on line

7BCO5647

Defining Internal Tables: Sorted & Hashed

Page 8: Lecture12 abap on line

8BCO5647

Work Area or Header Line

You can define internal tables either with (addition WITH HEADER LINE) or without header lines. An internal table with a header line consists of a work area (header line) and the actual table body. You address both objects using the same name.

Reading an Internal Table using LOOP.

LOOP at itab INTO wa. LOOP at itab.

WRITE wa-carrid. WRITE itab-carrid.

ENDLOOP. ENDLOOP.

Page 9: Lecture12 abap on line

9BCO5647

Hashed Internal Table

Access to a hashed table is implemented using a hash algorithm. This means that the data records are distributed randomly but evenly over a particular memory area. Their addresses are stored in a separate table, the hash table. The hash function uses the key data to calculate the address where the hashed table entry is located.

If a single record is accessed using a fully specified key, the hash function can use the key immediately to determine the address of the hashed table entry. That is where the address of the actual data record is stored.

Using a hash technique means that the access time no longer depends on the total number of entries in the table. The access time is therefore very short. Hashed tables are therefore particularly useful for large tables with which you predominantly use read access.

Page 10: Lecture12 abap on line

10BCO5647

Internal Table – single-record access

Whenever you want to read individual table rows by declaring a complete key, use READ TABLE ... WITH TABLE KEY.

This achieves the fastest single-record access with a key.

If the table is a standard table the SAP system performs a sequential search.

If the table is a sorted table the SAP system performs a binary search.

If the table is a hashed table the SAP system applies the hashed algorithm.

Page 11: Lecture12 abap on line

11BCO5647

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 a

unique key. If you decide you need to sort the table or use a binary search, you can always program these functions by hand. A sorted table also defines a unique key, but unlike a standard table, the data is sorted and then inserted. Also, the initial data is built up faster. Thus, 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.

A typical use is the preparation and execution of database changes by mass processing. The most efficient way of doing this is to create a local copy of the data in the program, make the changes to the copy, and then write all of its data records back to the database table. When you are dealing with large amounts of data, this method both saves runtime and reduces the load on the database server. Consequently, when you access the internal table, you should already use the unique key to ensure that the data record is unique. Automatic sorting can also bring further advantages.

Page 12: Lecture12 abap on line

12BCO5647

Internal Table Kinds - Summary

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.