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

append & collect

Former Member
0 Likes
1,537

what is diff between append and collect ?

Can we use append statement after using collect statement ?

thanks

9 REPLIES 9
Read only

anversha_s
Active Contributor
0 Likes
1,252

hi,


DATA: BEGIN OF COMPANIES OCCURS 10,
        NAME(20),
        SALES TYPE I,
      END   OF COMPANIES.
COMPANIES-NAME = 'Duck'.  COMPANIES-SALES = 10.
COLLECT COMPANIES.
COMPANIES-NAME = 'Tiger'. COMPANIES-SALES = 20.
COLLECT COMPANIES.
COMPANIES-NAME = 'Duck'.  COMPANIES-SALES = 30.
COLLECT COMPANIES.

The table COMPANIES now has the following appearance:

Duck 40

Tiger 20

When the line is inserted, the system checks whether there is already a table entry that matches the key.

If there is no corresponding entry already in the table,

the COLLECT statement has the same effect as inserting the new line.

If an entry with the same key already exists,

the COLLECT statement does not append a new line,

but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.

You should only use the COLLECT statement if you want to create summarized tables.

If you use other statements to insert table entries, you may end up with duplicate entries.

GO THROUGH THIS LINK

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/collect.htm

1. Can i use a collect statement here

We can use collect statement in all internal tables.

2. The only MAIN CONCEPT is

3. that it will SUM up the Quantity fields,

based upon

the COMBINATION of all Character Fields.

rgds

Anver

Read only

Former Member
0 Likes
1,252

chk this thread

Read only

Former Member
0 Likes
1,252

Hi,

If an entry with the same key already exists, the COLLECT statement does not append a new line, but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.

Append – Duplicate entries occurs.

Regards

Yamini

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,252

Hi,

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.

Check this for more information.

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/collect.htm

No need of using append after collect.

LOOP AT I_LIPS INTO W_LIPS."Calculating total quantity

W_TOTAL-VBELN = W_LIPS-VBELN.

W_TOTAL-LFIMG = W_LIPS-LFIMG.

COLLECT W_TOTAL INTO I_TOTAL .

CLEAR W_TOTAL.

ENDLOOP.

Message was edited by:

Jayanthi Jayaraman

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
1,252

Hi,

Collect is used as means for summation of numeric data. When you use collect if there is a record with same 'KEY' as that of your workarea, in the internal table then this workarea's numeric fields will be added to the numeric fields of the record in the internal table with the same KEY.

If the record does not exist then the record is appended to the internal table. Also you need to see if there is any INITIAL SIZE for the internal table if the number of records will be more than the INITIAL SIZE then some the new record will replace some existing record.

You should either use COLLECT or APPEND not both.

Regards,

Sesh

Read only

Former Member
0 Likes
1,252

Append statement is used to add the data from default header area into body area of an internal table. While adding the records, this statement will not verify whether the record is already existing or not. Even the record already exists... The new record will be added again.

Collect statement is also used in Internal table to add header data to the body area. While adding this statement verifies whether the entering record already exists or not? If it founds already, The numeric fields will only get added to the existing record, but not the another duplicate record.

Ex:

Data: begin of itab occurs 1,

eid(10),

ename(20),

sal type i,

end of itab.

itab-eid = '1001'.

itab-ename = 'abcd'.

itab-sal = '25000'.

append itab.

append itab.

append itab.

collect itab.

collect itab.

loop at itab.

write: / itab-eid, itab-ename, itab-sal.

endloop.

Read only

Former Member
0 Likes
1,252

hi,

The collect 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. You can use result to set a reference to the inserted or changed row in form of a field symbol or data reference.

Append statement inserts the content of the work area into the table without adding the values of its numeric components.

Ex if ther are four records.

abc def xyz 10

abc def xyz 15

abc def xyz 20

azz def xyz 30

if you use append four records will be appended to the internal table

if you use collect only two records will be appended as-

abc def xyz 45

azz def xyz 30

Hope this explaination helps.

Regards,

Richa

Read only

Former Member
0 Likes
1,252

HI mahya k,

Say suppose an Internal table has a Work area and a Body

Append --> simply appends/adds the current record u are working on (in the workarea) at the end of the internal table. ....

but collect will check all the fields of that record till the numberic field in the internal table body and just adds the numeral to the numeral of the work area and modifies the internal table

Eg ...

Internal table = ITAB

Fields = Name, Subject, mark, city

Here Name, subject and city are character fields

and mark is a numeric field .....

WA has -->

AAA MAths 50 City1

Int Body -->

BBB English 40 City2

AAA English 60 City2

CCC Maths 30 City1

AAA Maths 40 City1

No of records = 4

<b>Append will result in</b>

Int body with 5 records

BBB English 40 City2

AAA English 60 City2

CCC Maths 30 City1

AAA Maths 40 City2

AAA MAths 50 City1 <b><-- Work area</b>

<b>Collect will result in</b>

Int body now contains only 4 records ....

BBB English 40 City2

AAA English 60 City2

CCC Maths 30 City1

AAA Maths <b>90</b> City2 <b><-- The numeral got added in the same record </b>

Note: IT will not check for the last character field (which is city)

Hope this helps ...

Cheers

Kripa Rangachari....

Read only

Former Member
0 Likes
1,252

Taken it from help....

<b>COLLECT</b>

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.

<b>APPEND</b>

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