What Is A Collect Statement in ABAP?
A collect statement is one that can be used to summarize datasets or create new ones. A collect statement will sum up work area details or components into an internal table or to the existing row values if the key used is the same. On the other hand, if the work area record with a similar key does not exist, the collect statement will add or append a new record entirely.
How to Collect Statement in ABAP
In order for you to ensure that summation in SAP systems becomes a much easier job, then you need to learn how to use the collect statement in ABAP.
There are a number of approaches that can be used when it comes to using the collect statement.
Collect <WA> INTO <itab>
What happens when the syntax is used?
When the above syntax is used, a few things will occur within the system. They are outlined below:
- The statement will check to see whether any <WA> record with a key, matching the sale key in the work record area, already exists. This check will be done within the internal table. The key field should be of type C, N, D, or T.
- If there is no record that matches the key specification, the statement will add the data for the existing work area, into the internal table. The numerical fields will be added.
- On the other hand, if there is a record <WA> that exists already, the statement will add the numerical field values in the work area into the corresponding fields of the matching record. The internal table will then be updated. The new record does not need to be added in that case.
Working With the Collect Statement: Terms and Conditions
You need to ensure that the <WA> record is compatible with the row type of the internal table (itab) before you use the collect statement above.
If the components are not part of the primary table key, then they need to have a numeric data type. Otherwise, the statement may not be used effectively.
When working with the collect statement only in standard tables, you can use a hash administrator to determine entry. The hash administrator however, is temporary. When the table is accessed for changes, the administrator will become invalid. Once you obtain entry, you can fill the tables using the collect statement. The table row searches will be done in a linear manner, which means that workload searches will increase in the same way. The workload is not dependent on the number of table entries.
A binary search is what will determine entry into sorted tables. The relationship between the workload and the number of table entries in this case is logarithmic in nature.
The table has administrators will determine the entry for the hash tables. The number of table entries does not affect the workload.
Working With the Collect Statement: How the Data is Added
The collect statement in ABAP works in different ways to add new content to an internal table with no matching primary key. The rows will be inserted in various ways prior to <WA> contents being added to them.
- When the collect statement is used for standard tables, the new row will be added as the last row within the primary table index.
- For sorted tables, the new row will be added based on the sorting order of the internal tables according to the values of the key. The values of any subsequent rows within the primary table index will increase by one to accommodate the new changes.
- For hashed tables, the hash administrator will determine where the new row will be placed depending on the values of the keys.
Where a primary key already exists within the internal table, the work area components, whose values do not match that key will be added to the corresponding components in the existing record at the uppermost level. This means that the uppermost row with the lowest index on the primary table, will take precedence.
Working With the Collect: Things to Bear in Mind
You will need to bear a few other things in mind when working with the collect statement in ABAP. Some of those things are outlined below.
- When the collect statement is executed, the SY-TABIX system fields will have the internal table line index where the data has been stored. For hashed tables, the value will stand at zero. For sorted and standard tables however, that value will be the existing or inserted row number within the primary table index.
- The collect statement is usually the best option if you are looking to compress your datasets for efficiency or make them unique. If that is not the case, the APPEND statement could work just as well. Therefore, ensure that you only use the collect statement to fill a table that was processed using collect. Otherwise, you may not be able to guarantee uniqueness. MODIFY however, can be used in standard tables to edit tables filled using the collect statement.
- To assign field symbols into inserted lines, you may want to consider using ASSIGNING as an addition to the collect statement. The REFERENCE INTO can also be used in addition to the collect statement.
- Standard tables in general are not suited to the collect statement because they sometimes do not have a stable and separate key that can be used by the collect statement. Hashed and sorted tables are much more suited to the statement though. If you want to determine whether a standard table can be edited using the collect statement, you may want to utilize the function module: ABL_TABLE_HASH_STATE.
Exceptions When Using Collect Statement
When using the collect statement, you may see some exceptions.
The run time error COLLECT_OVERFLOW may occur when creating totals should there be an overflow in the integer field.
The run time error COLLECT_OVERFLOW_TYPE_P may occur when creating totals should there be an overflow in type p fields.
Collect statement allows for the creation of unique or compressed data sets which help greatly in enhancing system performance. It is therefore in your best interest to learn how to utilize the collect statement in ABAP as effectively as possible.