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