Lecture 12 Advanced Internal Tables BCO5647 Applications Programming Techniques (ABAP)
Readings & Objectives <ul><li>Readings </li></ul><ul><li>Keller & Keller   Chapter 4 Section 4.7.1 & 4.7.3    </li></ul><u...
Internal Tables as Dynamic Data Objects <ul><li>Internal tables are data objects that allow you to store datasets with a f...
Attributes of an Internal Table <ul><li>The data type of an internal table is fully specified by its: </li></ul><ul><li>Ro...
Internal Table Kinds <ul><li>Internal Tables can be divided into three table kinds, depending on the possible access type ...
Defining Internal Tables - Standard Example 1 : Example 2 : DATA:  itab TYPE STANDARD TABLE OF scarr   WITH UNIQUE KEY car...
Defining Internal Tables: Sorted & Hashed
Work Area or Header Line <ul><li>You can define internal tables either with (addition WITH HEADER LINE) or without header ...
Hashed Internal Table <ul><li>Access to a hashed table is implemented using a hash algorithm. This means that the data rec...
Internal Table – single-record access <ul><li>Whenever you want to read individual table rows by declaring a complete key,...
Internal Table Kinds - Summary <ul><li>Standard table </li></ul><ul><ul><li>Standard tables are best when you access data ...
Internal Table Kinds - Summary <ul><li>Hashed table </li></ul><ul><ul><li>The hash algorithm calculates the address of an ...
Upcoming SlideShare
Loading in …5
×

Lecture12 abap on line

1,174 views

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,174
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
147
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • 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. Example: Using the search term UA 0007 for read access to an internal table with the unique key CARRID CONNID and data as specified above will result in exactly one data record. 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. Example: Read-access to the data record with index 5 returns the fifth data record from the internal table.
  • Standard tables are the most flexible kind. The flexibility occurs because the sort sequence of a standard table is not fixed. You can change it several times and resort the internal table’s contents accordingly. They are usually used with index access, however when used with key access, the system searches all lines sequentially for the key value. The average search time is proportional to the number of lines. Sorted tables are ideally used with unique keys and fixed sorting. When a sorted table is defined, either a unique or non-unique key must be defined. By definition, this key also determines the sorting order, it cannot be changed. When a key access is made using this key, the SAP system will automatically use a binary search technique. Sorted tables are especially useful for the loop processing of an entire internal table’s content in a fixed order. Hashed tables are not intended for loop processing. Instead, you would use it when you need to access one specific entry at a time of an internal table’s contents. In a key access to a hashed table, the SAP system calculates the position of the line from the key value using a hash function . The search time is not affected by the number of lines and always constant. Can be used for buffering or bundling large amounts of data from several DB tables. Refer to access times for 3 table kinds - Keller &amp; Kruger Fig 4.17 Pg 177
  • Example 1: The example shows the definition of a STANDARD internal table (TT_DISTANCE) using a local table kind (TT_DISTANCE_TY), which itself uses a local row type (S_DISTANCE_TY). Therefore, the defined internal standard table has the columns CARRID, CONNID, DISTANCE, and DISTID. The DEFAULT KEY means that all non-numeric fields are used to create the key. They are used in the sequence in which they occur in the row type. In this case, the keys CARRID, CONNID, and DISTID are defined. The key is non-unique. Example 2: The example shows the definition of a STANDARD internal table (itab) using a global row type (scarr). The defined internal standard table has the same columns as the DB table scarr. The KEY is defined as CARRID and is unique. Example 3: The example defaults to a STANDARD internal table WITH NON-UNIQUE DEFAULT KEY NOTE: No work area has been defined in these examples. This will be demonstrated later.
  • The above example shows the definition of a sorted table and a hashed table. Both tables have the same row type as the standard table on the previous slide. Note that the contents of the tables are in a different order. For sorted tables, the sequence of the entries in the internal table is determined by the sequence of the fields in the key definition. For table kinds defined in a program, you can use the optional INITIAL SIZE addition to specify the number of rows that the runtime system should reserve when it instantiates the data object. INITIAL SIZE replaces the OCCURS statement.
  • 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 . Note 1: Work area can be also defined as DATA: wa LIKE LINE OF scarr. Note 2: Reading an Internal Table using LOOP and WA
  • Access to a hashed table is implemented using a hash algorithm. Simplified, 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. The function is not injective, that is, there can be several data records stored at a single address. This is implemented internally as a chained list. Thus, the system may still have to search through such an area sequentially. However, the chained list can contain no more than two entries. If a new key leads to the same hashed table address for a third time, the system uses a changed hash function and rebuilds the management from scratch. The slide image illustrates the simplest case, that is, in which there is only one data record stored at each address. 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. Data records are not inserted into the table in a sorted order. As with standard tables, you can sort hashed tables using the SORT statement - Sorting the table can be useful if you later want to use a loop to access the table.
  • Demo zbmcLec009a zbmcLec009b zbmcLec009c
  • Demo Workshop Exercises
  • Lecture12 abap on line

    1. 1. Lecture 12 Advanced Internal Tables BCO5647 Applications Programming Techniques (ABAP)
    2. 2. Readings & Objectives <ul><li>Readings </li></ul><ul><li>Keller & Keller Chapter 4 Section 4.7.1 & 4.7.3 </li></ul><ul><li>Objectives This lecture will </li></ul><ul><li>Revise the structure of internal tables </li></ul><ul><li>Review the attributes of internal tables </li></ul><ul><li>Describe the three internal table kinds: standard, sorted, hashed </li></ul><ul><li>Examine the features and application of STANDARD internal tables </li></ul><ul><li>Examine the features and application of SORTED internal tables </li></ul><ul><li>Examine the features and application of HASHED internal tables </li></ul><ul><li>Distinguish between the use of work areas Vs header lines when processing internal tables </li></ul>
    3. 3. Internal Tables as Dynamic Data Objects <ul><li>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. </li></ul><ul><li>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. </li></ul>
    4. 4. Attributes of an Internal Table <ul><li>The data type of an internal table is fully specified by its: </li></ul><ul><li>Row type </li></ul><ul><ul><li>The row type defines the attributes of the individual columns. You normally enter a structure type, but any data type is possible. </li></ul></ul><ul><li>Key definition </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>Access type </li></ul><ul><ul><li>Key access – like database tables – works via field contents. </li></ul></ul><ul><ul><li>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. </li></ul></ul>
    5. 5. Internal Table Kinds <ul><li>Internal Tables can be divided into three table kinds, depending on the possible access type required: </li></ul><ul><ul><li>Standard Tables – the row number is maintained internally. Both index and key access are possible. </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>Hashed Tables – The runtime-optimized data records are maintained here. This requires a unique key. Hashed tables only allow for key access. </li></ul></ul>
    6. 6. 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.
    7. 7. Defining Internal Tables: Sorted & Hashed
    8. 8. Work Area or Header Line <ul><li>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 . </li></ul><ul><li>Reading an Internal Table using LOOP. </li></ul><ul><li>LOOP at itab INTO wa. LOOP at itab. </li></ul><ul><li> WRITE wa-carrid. WRITE itab-carrid. </li></ul><ul><li>ENDLOOP. ENDLOOP. </li></ul>
    9. 9. Hashed Internal Table <ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>
    10. 10. Internal Table – single-record access <ul><li>Whenever you want to read individual table rows by declaring a complete key, use READ TABLE ... WITH TABLE KEY. </li></ul><ul><li>This achieves the fastest single-record access with a key. </li></ul><ul><li>If the table is a standard table the SAP system performs a sequential search. </li></ul><ul><li>If the table is a sorted table the SAP system performs a binary search. </li></ul><ul><li>If the table is a hashed table the SAP system applies the hashed algorithm. </li></ul>
    11. 11. Internal Table Kinds - Summary <ul><li>Standard table </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>Sorted table </li></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>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. </li></ul></ul>
    12. 12. Internal Table Kinds - Summary <ul><li>Hashed table </li></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>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. </li></ul></ul>

    ×