Sorted Tables, Hashed Tables and Key access to internal tables were introduced after release 4.0. Prior to this internal tables were declared as follows.
All internal tables had a header line. The syntax was as follows.
Data: BEGIN of <itab> occurs <n>,
END of <itab>
The number <n> in the occurs had the same meaning as the initial size as in release 4.0 onwards.
Collect
To fill a standard internal table without creating duplicate entries and add up the Packed, Integer, and Floating Point fields at the same time, use:
COLLECT uses a hash algorithm and is independent of the number of entries in the table (it does not need to maintain a table index). Therefore COLLECT is faster than READ BINARY combined with INSERT. If you need the final data sorted, the table should be sorted after all data has been collected.
Caution: When you fill an internal table, do not use COLLECT in combination with any other table filling statements (APPEND, INSERT, MODIFY, SELECT * INTO TABLE, SELECT * APPENDING TABLE). If you mix COLLECT with other statements, COLLECT cannot use it's hash algorithm and resorts to a normal linear search which is considerably slower.
Copying an internal table
Internal tables can be copied by MOVE like any other data object. If an internal table (itab) has a header line, the table can be accessed by itab[].
Processing tables only once. Your program should be structured so that your internal tables are processed only once, if possible. Seek to combine operations on an internal table so that they can all be processed in the one loop. The only situation where this may not be possible is where subsequent processing depends on the whole table having been processed previously (if the operations are linearly dependent they can still be done within the same loop).
READ instead of LOOP. If you only wish to retrieve a certain record from a table it is much more efficient (especially if the table is sorted) to use a READ WITH KEY or a READ INDEX on the table than looping until the record is found.
Sorted and Hashed tables: Following on from above, if you wish to reference a table several times within a program it is good practice to keep it sorted or to use a hashed table. Reads from normal unsorted internal tables have an order N time dependency (where N is the number of records), whereas sorted tables read with the command BINARY-SEARCH have a logarithmic time dependency (Log N), and hashed tables have a constant time dependency regardless of the number of records.
LOOP AT … WHERE. If you only wish to process certain entries of your internal table, use the LOOP AT.. WHERE command instead of a loop and then an IF or a CHECK statement.
In loops, only perform operations as necessary. If you are to perform an action on a field, remember to only perform the action when the value of the field changes and store the result. This means performing the operation within the ON CHANGE OF and AT NEW or AT END statements. WARNING Be aware that when using the AT statements, the order of the fields in your table or field-group is extremely important, if a field ‘higher up’ (further to the left) changes it will also trigger the AT statement on any field ‘lower down’ (further to the right).
APPEND LINES OF. When combining tables of the same structure, instead of looping through one table and appending the records to the other table, use this command that transfers the whole table in one operation. This is particularly useful if you require the retrieval of the same data from two different database tables, in which case you can use two SELECT INTO and then an APPEND LINES OF to combine your data.
Syntax: APPEND LINES OF itab1 [FROM idx1] [TO idx2] TO itab2.
READ TABLE … TRANSPORTING NO FIELDS. This statement is the fastest way to find a particular record within an internal table (when combined with the most efficient searching technique for that table, such as a hashed table or a read using a binary search). This command is used to set the sy-tabix field and the sy-subrc field to indicate position of the record and if found successfully, these fields can then be used to insert new records or to continue processing knowing the record exists.
See Also:
SAP ABAP INTERNAL TABLE BASICS
INTERNAL TABLES IN SAP ABAP
1) COLLECT can be used to add up the Packed, Integer, and Floating Point fields at the same time. TRUE
2) COLLECT is faster than READ BINARY combined with INSERT TRUE
3) Mixing collect with other statements
(APPEND, INSERT, MODIFY, SELECT * INTO TABLE, SELECT * APPENDING TABLE) is a good practice FALSE
4) COLLECT uses a hash algorithm TRUE
5) ITAB2[] = ITAB1[]. will copy ITAB2[] into ITAB1[] FALSE
No comments:
Post a Comment