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

COLLECT

Former Member
0 Likes
1,007

Can any one pls explain COLLECT keyword with example?

8 REPLIES 8
Read only

Former Member
0 Likes
961

HI

here is sample code

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

Ravish

Read only

Former Member
0 Likes
961

hi,

<u><b>Collect:</b></u>

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

Reshma

Read only

Former Member
0 Likes
961

Hi Debarshi,

please have a look at <a href="http://help.sap.com/saphelp_46c/helpdata/en/fc/eb36d5358411d1829f0000e829fbfe/frameset.htm">SAP Help on COLLECT statement</a>

I hope it helps. Best regards,

Alvaro

Read only

Former Member
0 Likes
961

Hi,

Syntax Diagram

COLLECT

Basic form

COLLECT [wa INTO] itab.

Extras:

1. ... ASSIGNING <fs>

2. ... REFERENCE INTO dref

3. ... SORTED BY f

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas. See Cannot Use Short Forms in Line Operations.

Effect

COLLECT allows you to create unique or summarized datasets. The system first tries to find a table entry corresponding to the table key. (See also Defining Keys for Internal Tables). The key values are taken either from the header line of the internal table itab, or from the explicitly-specified work area wa. The line type of itab must be flat - that is, it cannot itself contain any internal tables. All the components that do not belong to the key must be numeric types ( ABAP Numeric Types).

If the system finds an entry, the numeric fields that are not part of the table key (see ABAPNumeric Types) are added to the sum total of the existing entries. If it does not find an entry, the system creates a new entry instead.

The way in which the system finds the entries depends on the kind of the internal table:

STANDARD TABLE:

The system creates a temporary hash administration for the table to find the entries. This means that the runtime required to find them does not depend on the number of table entries. The administration is temporary, since it is invalidated by operations (such as DELETE, INSERT, MODIFY, or SORT). A subsequent COLLECT is then no longer independent of the table size, because the system has to use a linear search to find entries. For this reason, you should only use COLLECT to fill standard tables.

SORTED TABLE:

The system uses a binary search to find the entries. There is a logarithmic relationship between the number of table entries and the search time.

HASHED TABLE:

The system uses the internal hash administration of the table to find records. Since (unlike standard tables), this remains intact even after table modification operations, the search time is always independent of the number of table entries.

For standard tables and SORTED TABLEs, the system field SY-TABIX contains the number of the existing or newly-added table entry after the COLLECT. With HASHED TABLEs, SY-TABIX is set to 0.

Notes

COLLECT allows you to create a unique or summarized dataset, and you should only use it when this is necessary. If neither of these characteristics are required, or where the nature of the table in the application means that it is impossible for duplicate entries to occur, you should use INSERT [wa INTO] TABLE itab instead of COLLECT. If you do need the table to be unique or summarized, COLLECT is the most efficient way to achieve it.

If you use COLLECT with a work area, the work area must be compatible with the line type of the internal table.

If you edit a standard table using COLLECT, you should only use the COLLECT or MODIFY ... TRANSPORTING f1 f2 ... statements (where none of f1, f2, ... may be in the key). Only then can you be sure that:

-The internal table actually is unique or summarized

-COLLECT runs efficiently. The check whether the dataset

already contains an entry with the same key has a constant

search time (hash procedure).

If you use any other table modification statements, the check for entries in the dataset with the same key can only run using a linear search (and will accordingly take longer). You can use the function module ABL_TABLE_HASH_STATE to test whether the COLLECT has a constant or linear search time for a given standard table.

Example

Summarized sales figures by company:

TYPES: BEGIN OF COMPANY,

NAME(20) TYPE C,

SALES TYPE I,

END OF COMPANY.

DATA: COMP TYPE COMPANY,

COMPTAB TYPE HASHED TABLE OF COMPANY

WITH UNIQUE KEY NAME.

COMP-NAME = 'Duck'. COMP-SALES = 10. COLLECT COMP INTO COMPTAB.

COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB.

COMP-NAME = 'Duck'. COMP-SALES = 30. COLLECT COMP INTO COMPTAB.

Table COMPTAB now has the following contents:

NAME | SALES

-


Duck | 40

Tiger | 20

Addition 1

... ASSIGNING <fs>

Effect

If this statement is successfully executed, the field symbol <fs> is set to the changed or new entry. Otherwise the field symbol remains unchanged.

Addition 2

... REFERENCE INTO dref

Effect

If this statement is successfully executed the reference to the relevant line is placed in dref. Otherwise the data reference dref remains unchanged.

Addition 3

... SORTED BY f

Effect

COLLECT ... SORTED BY f is obsolete, and should no longer be used. It only applies to standard tables, and has the same function as APPEND ... SORTED BY f, which you should use instead. (See also Obsolete Language Elements).

Note

Performance:

If you are still using internal tables with headers but, as recommended, keep your data in work areas with a different name, you do not need to assign the data to the header first in order to pass it to the internal tables. Instead, you should use the work area directly as with tables without headers. For example, "APPEND wa TO itab." is roughly twice as fast as "itab = wa. APPEND itab.". The same applies to COLLECT and INSERT.

The runtime of a COLLECT increases with the width of the table key and the number of numeric fields whose contents are summated.

Exceptions

Catchable Exceptions

CX_SY_ARITHMETIC_OVERFLOW

Cause: Overflow in the integer field when forming totals

Runtime Error: COLLECT_OVERFLOW

Cause: overflow in type P field when forming totals

Runtime Error: COLLECT_OVERFLOW_TYPE_P

Non-Catchable Exceptions

Cause: COLLECT on non-numeric fileds

Runtime Error: TABLE_COLLECT_CHAR_IN_FUNCTION

check this:

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

Regards,

Priyanka.

Read only

Former Member
0 Likes
961

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.

Read only

Former Member
0 Likes
961

Hi,

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.

Regards

Sudheer

Read only

Former Member
0 Likes
961

Hi,

In short i would explain liek this,

suppose your Internal table is like this,

1. K 10 20

K 20 30

A 10 15

OutPut:

K 30 50

A 10 15

2. And even if u remove A and K

then

10 20

20 30

10 15

then

Output:

40 65

Reward if useful!

Read only

Former Member
0 Likes
961

Hi

COLLECT summarizes all numberic fields based on the non-numeric fields contents.

For Example

Summarized sales figures by company:

TYPES: BEGIN OF COMPANY,

NAME(20) TYPE C,

SALES TYPE I,

END OF COMPANY.

DATA: COMP TYPE COMPANY,

COMPTAB TYPE HASHED TABLE OF COMPANY

WITH UNIQUE KEY NAME.

COMP-NAME = 'Duck'. COMP-SALES = 10. COLLECT COMP INTO COMPTAB.

COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB.

COMP-NAME = 'Duck'. COMP-SALES = 30. COLLECT COMP INTO COMPTAB

.

Table COMPTAB now has the following contents:

NAME | SALES

-


Duck | 40

Tiger | 20

Also, check this thread

Regards

Raj