Application Development and Automation 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: 
Read only

sap

Former Member
0 Likes
765

wat is the difference between APPEND and COLLECT

1 ACCEPTED SOLUTION
Read only

Yogitha
Product and Topic Expert
Product and Topic Expert
744

Hi,

APPEND:

Append statement is used to add the data from default header area or content from a work area into body area of an internal table. The APPEND statement sets sy-tabix to the table index of the last appended row.

COLLECT:

This statement inserts the contents of a work area either as single row into an internal table or adds the values of its numeric components to the corresponding values of existing rows with the same key.

In contrast to COLLECT, APPEND does not check whether an entry with the same key exists. Therefore, it is considerably faster than COLLECT. If the COLLECT logic is not needed or lines with an identical default key cannot occur in a particular situation, you should always use APPEND instead of COLLECT.

Regards,

Yogitha

6 REPLIES 6
Read only

gopi_narendra
Active Contributor
0 Likes
744

Append will add new records whereas Collect will sum up the numeric fields for every new key filed

itab-matnr = '1' itab-qty = 10.

itab-matnr = '2' itab-qty = 5.

itab-matnr = '3' itab-qty = 9.

itab-matnr = '1' itab-qty = 2.

itab-matnr = '2' itab-qty = 3.

itab-matnr = '2' itab-qty = 6.

Append itab will have 6 records as above.

Collect itab into another itab will have 3 records as below

itab-matnr = '1' itab-qty = 12.

itab-matnr = '2' itab-qty = 14.

itab-matnr = '3' itab-qty = 9.

Regards

Gopi

Read only

Former Member
0 Likes
744

hi,

Append will add the records into the table.

Collect will also do the same but if there are same records it will sum all the records into one.

Read only

Former Member
0 Likes
744

Append - it will insert new record to internal table

Collect - if non numeric columns equl it will sum numeric columns ,if not it will work like append statement.

Read only

Yogitha
Product and Topic Expert
Product and Topic Expert
745

Hi,

APPEND:

Append statement is used to add the data from default header area or content from a work area into body area of an internal table. The APPEND statement sets sy-tabix to the table index of the last appended row.

COLLECT:

This statement inserts the contents of a work area either as single row into an internal table or adds the values of its numeric components to the corresponding values of existing rows with the same key.

In contrast to COLLECT, APPEND does not check whether an entry with the same key exists. Therefore, it is considerably faster than COLLECT. If the COLLECT logic is not needed or lines with an identical default key cannot occur in a particular situation, you should always use APPEND instead of COLLECT.

Regards,

Yogitha

Read only

Former Member
0 Likes
744

Hi,

Append is one where u add new records at the of the body n in collect it first checks if the record exists or not.The collect statement acts like append statement which creates field string data as well as it compares the records of the internal table. If the character fields of the internal table are matched with the field string data then the non character fields get added otherwise a new record is created at the end of the table..

reward if useful.

with regards,

madhuri.

Read only

Former Member
0 Likes
744

hi,

APPEND

Syntax

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 addition INITIAL SIZE, the newly created last row is deleted.

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

COLLECT:

COLLECT [wa INTO] itab.

Addition

... SORTED BY f

Effect

COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .

If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.

If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.

If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.

If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .

After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.

COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.

If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that the internal table will actually be unique or compressed, as described above and

COLLECT will run very efficiently.

If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.