Internal Tables in ABAP Lesson Twenty Two

INTERNAL TABLES IN ABAP:

There are two ways of accessing the records in an internal table,By copying individual records into a work area. The work area must be compatible with the line type of the internal table.You can access the work area in any way, as long as the component you are trying to access is not itself an internal table. If one of the components is an internal table, you must use a further work area, whose line type is compatible with that of the nested table.When you change the internal table, the contents of the work area are either written back to the table or added as a new record.

By assigning the individual data records to an appropriate field symbol. Once the system has read an entry, you can address its components directly via its address. There is no copying to and from the work area. This method is particularly appropriate for accessing large or complex tables.If you want to read more than one record, you must use a LOOP... ENDLOOP structure. You can then change or delete the line that has just been read, and the system applies the change to the table body. You can also change or delete lines using a logical condition.When you use the above statements with sorted tables, you must ensure that the sort sequence is maintained.

Within a loop, the INSERT statement adds the data record before the current record in the table. If you want to insert a set of lines from an internal table into another index table, you should use the INSERT LINES OF variant instead.When you read single data records, you can use two further additions:

In the COMPARING addition, the system compares the field contents of a data record with those in the work area for equality.In the TRANSPORTING addition, you can restrict the data transport to selected fields.Other statements for standard tables

SORT [ASCENDING|DESCENDING]
[BY [ASCENDING|DESCENDING] ..
[ASCENDING|DESCENDING]][AS TEXT] [STABLE].

These statements sort the table by the table key or the specified field sequence. If you do not use an addition, the system sorts ascending. If you use the AS TEXT addition, character fields are sorted in culture-specific sequence. The relative order of the data records with identical sort keys only remain constant if you use the STABLE addition.

APPEND INTO SORTED BY .

This statement appends the work area to the ranked list in descending order. The ranked list may not be longer than the specified INITIAL SIZE, and the work area must satisfy the sort order of the table.The statements listed here can be used freely with both standard and sorted tables.When you change a single line, you can specify the fields that you want to change using the TRANSPORTING addition. Within a loop, MODIFY changes the current data record.

If you want to delete a set of lines from an index table, use the variant DELETE FROM... TO.. or WHERE... instead of a loop. You can program almost any logical expression after WHERE.The only restriction is that the first field in each comparison must be a component of the line structure (see the corresponding Open SQL statements). You can pass component names dynamically.

If you want to delete the entire internal table , use the statement CLEAR .In the LOOP AT... ENDLOOP structure, the statements within the loop are applied to each data record in turn. The INTO addition copies entries one at a time into the work area.The system places the index of the current loop pass in the system field sy-tabix. When the loop has finished, sy-tabix has the same value that it had before the loop started.Inserting and deleting lines within a loop affects the following loop passes.Access to a hashed table is imple mented using a hash algorithm. Simplified, this means that the data records are distributed randomly but evenly over a particular memory area.. The addresses are stored in a special table called the hashing table .

There is a hash function, which determines the address at which the pointer to a data record with a certain key can be found. 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. Therefore, although the system still has to search sequentially within these areas, it only has to read a few data records (usually no more than three). The graphic illustrates the simplest case, that is, in which there is only one data record stored at each address.

Using a hash technique means that the access time no longer depends on the total number of entries in the table. On the contrary, it is always very fast. Hash tables are therefore particularly useful for large tables with which you use predominantly 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:

SORT [ASCENDING|DESCENDING]
[BY [ASCENDING|DESCENDING] ..
[ASCENDING|DESCENDING]][AS TEXT].

Sorting the table can be useful if you later want to use a loop to access the table.You can use the statements listed here with tables of all three types. Apart from a few special cases, you can recognize the statements from the extra keyword TABLE. The technical implementation of the statements varies slightly according to the table type.

As a rule, index access to an internal table is quickest. However, it sometimes makes more sense to access data using key values. A unique key is only possible with sorted and hashed tables. If you use the syntax displayed here, your program coding is independent of the table type (generic type specification, easier maintenance).With a standard table, inserting an entry has the same effect as appending. With sorted tables with a non-unique key, the entry is inserted before the first (if any) entry with the same key.

To read individual data records using the first variant, all fields of that are key fields of must be filled. and can be identical. If you use the WITH TABLE KEY addition in the second variant, you must also specify the key fully. Otherwise, the system searches according to the sequence of fields that you have specified, using a binary search where possible. You can force the system to use a binary search with a standard table using the BINARY SEARCH addition.In this case, you must sort the table by the corresponding fields first. The system returns the first entry that meets the selection criteria.Similarly to when you read entries, when you change and delete entries using the key and a work area, you must specify all of the key fields.

You can prevent fields from being transported into the work area during loop processing by using the TRANSPORTING NO FIELDS addition in the WHERE condition. (You can use this to count the number of a particular kind of entry.)

Other statements for all table types

DELETE ADJACENT DUPLICATES FROM
[COMPARING .. | A L L F I E L }|ALL FIELDS}].
The system deletes all adjacent entries with the same key field contents apart from the first entry. You can prevent the system from only comparing the key field using the COMPARING addition. If you sort the table by the required fields beforehand, you can be sure that only unique entries will remain in the table after the DELETE ADJACENT DUPLICATES statement.

Searches all lines of the table for the string . If the search is successful, the system sets the fields sy-tabix and sy-fdpos.

FREE .
Unlike CLEAR, which only deletes the contents of the table, FREE releases the memory occupied by it as well.

If you want to access your data using the index and do not need your table to be kept in sorted order or to have a unique key, that is, when the sequence of the entries is the most important thing, not sorting by key or having unique entries, you should use standard tables. (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.)This example is written to manage a waiting list.

Typical functions are:

Adding a single entry,

Deleting individual entries according to certain criteria,

Displaying and then deleting the first entry from the list,

Displaying someone's position in the list.

For simplicity, the example does not encapsulate the functions in procedures.
The first thing we do in the example is to declare line and table type, from which we can then declare a work area and our internal table. We also require an elementary field for passing explicit index values.

Adding new entries

The data record for a waiting customer is only added to the table if it does not already exist in it. If the table had a unique key, you would not have had to have programmed this check yourself.

Deleting single entries according to various criteria

The criterion is the key field. However, other criteria would be possible - for example, deleting data records older than a certain insertion date reg_date.

Displaying and deleting the first entry from the list
Once a customer comes to the top of the waiting list, you can delete his or her entry. If the waiting list is empty, such an action has no effect. Consequently, you do not have to check whether there are entries in the list before attempting the deletion.

Displaying the position of a customer in the waiting list
As above, you do not need to place any data in the work area. We are only interested in the values of sy-subrc and sy-tabix. If the entry is not in the table, sy-tabix is set to zero.

At this stage, let us return to the special case of the restricted ranked list:
DATA {TYPE|LIKE} STANDARD TABLE OF ... INITIAL SIZE . ... APPEND INTO SORTED BY .
When you choose to use a sorted table, it will normally be because you want to define a unique key.

The mere fact that the table is kept in sorted order is not that significant, since you can sort any kind of internal table. However, with sorted tables (unlike hashed tables), new data records are inserted in the correct sort order. 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 hashed table in terms of runtime.

The aim of the example here is to modify the contents of a database table. The most efficient way of doing this is to create a local copy of the table in the program, make the changes to the copy, and then write all of its data 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. Since the internal table represents a database table in this case, you should ensure that its records have unique keys.

This is assured by the key definition. Automatic sorting can also bring further advantages.When you change a group of data records, only the fields price and currency are copied from the work area.This means that, with larger tables, the access time is reduced significantly in comparison with a binary search. In a loop, however, the hashed table has to search the entire table (full table scan). Since the table entries are stored unsorted, it would be better to use a sorted table if you needed to run a loop through a left-justified portion of the key.

It can also be worth using a hashed table but sorting it. A typical use for hashed tables is to buffer detailed information that you need repeatedly and can identify using a unique key. You should bear in mind that you can also set up table buffering for a table in the ABAP Dictionary to cover exactly the same case. However, whether the tables are buffered on the application table depends on the size of the database table.Buffering in the program using hashed tables also allows you to restrict the dataset according to your own needs, or to buffer additional data as required.In this example, we want to allow the user to enter the name of a city, and the system to display its geographical coordinates.


First, we fill our "buffer table" city_list with values from the database table sgeocity. Then, we read an entry from the hashed table, specifying the full key.The details are displayed as a simple list. At this point, it is worth repeating that you should only use this buffering technique if you want to keep large amounts of data locally in the program. 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.

You can define internal tables either with (WITH HEADER LINE addition) or without header lines. An internal table with header line consists of a work area (header line) and the actual table body. You address both objects using the same name. The way in which the system interprets the name depends on the context. For example, the MOVE statement applies to the header line, but the SEARCH statement applies to the body of the table.

To avoid confusion, you are recommended to use internal tables without header lines. This is particularly important when you use nested tables. However, internal tables with header line do offer a shorter syntax in several statements (APPEND, INSERT, MODIFY, COLLECT, DELETE, READ, LOOP).

Within ABAP Objects, you can only use internal tables without a header line. You can always address the body of an internal table explicitly by using the following syntax: []. This syntax is always valid, whether the internal table has a header line or not.The COLLECT statement adds the work area or header line to an internal entry with the same type or, if there is none, adds a new entry to the table. It searches for the entry according to the table type and defined key. If it finds an entry in the table, it adds all numeric fields that are not part of the key to the existing values. If there is not already an entry in the table, it appends the contents of the work area or header line to the end of the table.

When you read a table line using READ or a series of table lines using LOOP AT, you can assign the lines of the internal table to a field symbol using the addition ... ASSIGNING <>. The field symbol <> then points to the line that you assigned, allowing you to access it directly. Consequently, the system does not have to copy the entry from the internal table to the work area and back again.The field symbol <> must have the same type as the line type of the internal table. However, there are also certain restrictions when you use this technique:

You can only change the contents of key fields if the table is a standard table .

You cannot use the SUM statement in control level processing. (The SUM statement adds all of the numeric fields in the work area.)This technique is particularly useful for accessing a lot of table lines or nested tables within a loop. Copying values to and from the work area in this kind of operation is particularly runtime-intensive. In this example, the user should be able to enter a departure city, for which all possible flights are then listed.To do this, we decla re an innner table (cofl_list) and an outer table( travel_list) with corresponding work areas.



In order to allow loop access using field symbols, the buffer table and the inner internal table must have the same type. Furthermore, we want to sort the table by various criteria later on. Consequently, we are using standard tables, not sorted tables.

It would have been possible to solve this problem using nested SELECT statements. However, this is not a realistic proposition because of the excessive load that we would then place on the database server.

Index access (APPEND, INSERT ... INDEX, LOOP ... FROM TO and so on) is possible for standard and sorted tables. A possible consequence of this is that you may cause a runtime error by violating the sort sequence if you use INSERT with an index or APPEND on a sorted table.

SORT can only apply to standard and hashed tables. It has no positive effect in a sorted table, and can lead to a syntax or runtime error if the attempted sort violates the key sequence.You can use key access for any table type, but the runtime required varies according to the table type.

The run time depends on whether the values are part of the key (so the sequence in which you pass them is also significant). INSERT for a standard table or hashed table using the key has the same effect as the APPEND statement.The system supports control level processing for all table types. Hashed and standard tables must be sorted beforehand.

RELATED POSTS

LESSON 23 SUB ROUTIENS IN ABAP

My SAP Project safety and security

1 comment :

  1. HI, this is shaiksha. I have a question regarding Internal table content. My requirement is "There is an internal table and has 2 fields named emno and salary." My question is " how to display 3rd highers salary from it." kindly send answer to my email. My email is shaiksha.it@gmail.com...
    Thanking you.

    ReplyDelete