Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Difference between Append & Collect statement

Former Member
0 Kudos

Hi,

can some explain me the Difference between Append & Collect statement.

Thanks,

Lee

5 REPLIES 5

former_member194152
Contributor
0 Kudos

Hi,

Both statement used to populate data in internal table whan u use append statement then a new record has been appended to internal table and when u use collect statement then program will check key for character fields and for same character fields it will add all numeric fields.

Regards

Gagan

Former Member
0 Kudos

append will simply add the entry to the end of internal table irrespective of whether the filed exists or not

collect will modify the field if the corresponding entry is found

former_member386202
Active Contributor
0 Kudos

APPEND line_spec TO itab [SORTED BY comp] [result].

Addition:

... SORTED BY comp

Effect

This statement appends one or more rows line_spec to an internal index table itab. If itab is a standard table, you can use SORTED BY to sort the table in a specified way. Use result when appending a single row as of release 6.10 to set a reference to the appended row in the form of a field symbol or a data reference.

For the individual table types, appending is done as follows:

To standard tables, rows are appended directly and without checking the content of the internal table.

To sorted tables, rows are appended only if they correspond to the sort sequence and do not create duplicate entries with unique table key. Otherwise, an untreatable exception is triggered.

To hashed tables, no rows can be appended.

The APPEND statement sets sy-tabix to the table index of the last appended row.

Addition

... SORTED BY comp

Effect

This addition is allowed only if you specify a workarea wa and if you use a standard table, where wa must be compatible to the row type of the table. You can specify component comp as shown in section Specifying Components, however, you can access only one single component and no attributes of classes using the object component selector.

The statement is executed in two steps:

Starting at the last row, the table is searched for a row, in which the value of component comp is greater than or equal to the value of component comp of wa. If such a row exists, the workarea wa is included after this row. If no such row exists, the workarea wa is included before the first row. The table index of all rows following the included rows increases by one.

If the number of rows before the statement is executed is greater than or equal to the number specified in the definition of the internal table in the INITIAL SIZE addition, the newly-created last row is deleted.

Note

When using only the statement APPEND with addition SORTED BY to fill an internal table, this rule results in an internal table that contains no more than the number of rows specified in its definition after INITIAL SIZE and that is sorted in descending order by component comp (ranking).

The SORT statement should usually be used instead of APPEND SORTED BY.

Example

Creating a ranking of the three flights of a connection showing the most free seats.

PARAMETERS: p_carrid TYPE sflight-carrid,

p_connid TYPE sflight-connid.

DATA: BEGIN OF seats,

fldate TYPE sflight-fldate,

seatsocc TYPE sflight-seatsocc,

seatsmax TYPE sflight-seatsmax,

seatsfree TYPE sflight-seatsocc,

END OF seats.

DATA seats_tab LIKE STANDARD TABLE OF seats

INITIAL SIZE 3.

SELECT fldate seatsocc seatsmax

FROM sflight

INTO seats

WHERE carrid = p_carrid AND

connid = p_connid.

seats-seatsfree = seats-seatsmax - seats-seatsocc.

APPEND seats TO seats_tab SORTED BY seatsfree.

ENDSELECT.

COLLECT

Syntax

COLLECT wa INTO itab [result].

Effect

This statement inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key. As of Release 6.10, you can use result to set a reference to the inserted or changed row in the form of a field symbol or data reference.

Prerequisite for the use of this statement is that wa is compatible with the row type of itab and all components that are not part of the table key must have a numeric data type (i, p, f).

In standard tables that are only filled using COLLECT, the entry is determined by a temporarily created hash administration. The workload is independent of the number of entries in the table. The hash administration is temporary and is generally invalidated when the table is accessed for changing. If further COLLECT statements are entered after an invalidation, a linear search of all table rows is performed. The workload for this search increases in a linear fashion in relation to the number of entries.

In sorted tables, the entry is determined using a binary search. The workload has a logarithmic relationship to the number of entries in the table.

In hashed tables, the entry is determined using the hash administration of the table and is always independent of the number of table entries.

If no line is found with an identical key, a row is inserted as described below, and filled with the content of wa:

In standard tables the line is appended.

In sorted tables, the new line is inserted in the sort sequence of the internal table according to its key values, and the table index of subsequent rows is increased by 1.

In hashed tables, the new row is inserted into the internal table by the hash administration, according to its key values.

If the internal table already contains one or more rows with an identical key, those values of the components of work area wa that are not part of the key, are added to the corresponding components of the uppermost existing row (in the case of index tables, this is the row with the lowest table index).

The COLLECT statement sets sy-tabix to the table index of the inserted or existing row, in the case of standard tables and sorted tables, and to the value 0 in the case of hashed tables.

Outside of classes, you can omit wa INTO if the internal table has an identically-named header line itab. The statement then implicitly uses the header line as the work area.

COLLECT should only be used if you want to create an internal table that is genuinely unique or compressed. In this case, COLLECT can greatly benefit performance. If uniqueness or compression are not required, or the uniqueness is guaranteed for other reasons, the INSERT statement should be used instead.

The use of COLLECT for standard tables is obsolete. COLLECT should primarily be used for hashed tables, as these have a unique table key and a stable hash administration.

If a standard table is filled using COLLECT, it should not be edited using any other statement with the exception of MODIFY. If the latter is used with the addition TRANSPORTING, you must ensure that no key fields are changed. This is the only way to guarantee that the table entries are always unique and compressed, and that the COLLECT statement functions correctly and benefits performance. The function module ABL_TABLE_HASH_STATE can be used to check whether a standard table is suitable for editing using COLLECT.

Example

Compressed insertion of data from the database table sflight into the internal table seats_tab. The rows in which the key components carrid and connid are identical are compressed by adding the number of occupied seats to the numeric component seatsocc.

DATA: BEGIN OF seats,

carrid TYPE sflight-carrid,

connid TYPE sflight-connid,

seatsocc TYPE sflight-seatsocc,

END OF seats.

DATA seats_tab LIKE HASHED TABLE OF seats

WITH UNIQUE KEY carrid connid.

SELECT carrid connid seatsocc

FROM sflight

INTO seats.

COLLECT seats INTO seats_tab.

ENDSELECT.

Regards,

Prashant

former_member194797
Active Contributor
0 Kudos

APPEND creates always a new row in the internal table;

COLLECT checks the key fields (= the non numeric fields); if there is already a row with the same key fields, COLLECT will change this row by adding the values in the numeric fields (see documentation); if there is no row with the same key fields, COLLECT will have the same effect as APPEND.

Vijay
Active Contributor
0 Kudos

hi

APPEND :append statement will just append a new row into the table

collect: collect statement serve dual purpose . if the key field is already present in the table then it will add the integer field of new record to the exist record otherwise it will create a new record.

regards

vijay

reward points if helpfull