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,insert,collect

Former Member
0 Kudos

frens let me know the diff between append,insert and collect keywords.

8 REPLIES 8

Former Member
0 Kudos

Hi RajaReddy.....

<b>APPEND:</b>

Append statement adds a set of statements to an internal table at the end.

<b>APPEND <line> TO <itab>.</b>

this statement adds the <line> to internal table <itab> at the end..

<b>APPEND LINES OF <itab1> TO <itab2>.</b>

this adds the contents if <itab1> to <itab2>.

<b>INSERT:</b>

Insert works like this:

<b>Standard tables</b>

The line is appended to the end of the internal table. This has the same effect as the explicit APPEND statement.

<b>Sorted tables</b>

The line is inserted into the table according to the table key. If the key is non-unique, duplicates are inserted above the existing entry with the same key. The runtime for the operation increases logarithmically with the number of existing table entries.

<b>Hashed tables</b>

The table is inserted into the internal hash administration according to the table key.

<b>COLLECT:</b>

COLLECT <wa> INTO <itab>.

<itab> must have a flat line type, and all of the fields that are not part of the table key must have a numeric type (F, I, or P).

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.

Reward points if useful.....

Suresh

Former Member
0 Kudos

APPEND : This command inserts a new record at the end of the table.

INSERT : This command inserts a new record at the given index,

COLLECT : This command checks that if a row has 6 fields then for 3 fields if there is no value then it searches for the fileds an the modifies that particular line

former_member188829
Active Contributor

By Using APPEND u can add the record at the end of internal table.

eg:APPEND ITAB.

By using INSERT u can add a record before or after the existing record in Internal table

eg:INSERT ITAB INDEX 3.

By using COLLECT it can't allow the duplicate keys in internal table.

eg:COLLECT ITAB.

ferry_lianto
Active Contributor
0 Kudos

Hi,

APPEND:

Simply adds record at the end of the internal table....

INSERT:

Inserts new lines in a database table .

You can specify the name of the database table either in the program itself in the form dbtab or at runtime as the contents of the field dbtabname . In both cases, the database table must be defined in the ABAP/4 Dictionary . If the program contains the name of the database table, it must also include a corresponding TABLES statement. Normally, lines are inserted only in the current client. Data can only be inserted using a view if the view refers to a single table and was defined in the ABAP/4 Dictionary with the maintenance status "No restriction".

INSERT belongs to the Open SQL command set.

COLLECT:

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

Regards,

Ferry Lianto

Former Member
0 Kudos

Hi!

Not to confuse you, in simple terms:

APPEND: This adds a new record to the internal table in the last position.

INSERT: This adds a new record to the internal table at the Index position specified.

COLLECT: The table is first checked for an entry of the key-fields in the table with the comparision of the coming entry. If the key-fields' entry is present, this adds up all the numeric fields of the record with the existing record. If the record is totally new, then it is appended to the table.

Hope you got the answer.

Reward points if I am helpful to you

Regards

Former Member
0 Kudos

hi,

1. APPEND simply

inserts one record at the end.

2. While Collect is Intelligent.

(it will not simply add one record.

Instead.

3. It will check all the alpha-numeric field combinations

in the internal table

and if found, then it will simply SUM up the numeric values in that record,

else it will insert one record).

INSERT:

This command inserts a new record at the given index.

____________________________________________________________________

APPEND

Simply adds record at the end of the internal table....

COLLECT: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,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.

sri

Former Member
0 Kudos

hi,

i am giving clear idea oabut these see example also,

INSERT itab

Syntax

INSERT line_spec INTO itab_position [result].

Effect

This statement adds one or more lines line_spec to a position itab_position of an internal table. The position can be specified using the table key or the table index. As of release 6.10, result can be used to set a reference as a field symbol or data reference to the inserted line when inserting a single line.

System Fields

sy-subrc Meaning

0 One or more lines were inserted.

4 No line was inserted because either a line of the same unique key already existed when inserting single lines using the table key or the specified index was greater than the current number of lines plus one when inserting the lines using the table index.

Example

Insertion of single lines using the table index in a standard table int_tab and insertion of references to these lines using the table key in a hashed table ref_tab . The output in the LOOP loops result in the numbers 10 to 1 for int_tab and the numbers 1 to 10 for ref_tab.

DATA: int TYPE i,

dref TYPE REF TO i.

DATA: int_tab LIKE STANDARD TABLE OF int,

ref_tab LIKE HASHED TABLE OF dref

WITH UNIQUE KEY table_line.

DO 10 TIMES.

INSERT sy-index

INTO int_tab INDEX 1

REFERENCE INTO dref.

INSERT dref

INTO TABLE ref_tab.

ENDDO.

LOOP AT int_tab INTO int.

WRITE / int.

ENDLOOP.

SKIP.

LOOP AT ref_tab INTO dref.

WRITE / dref->*.

ENDLOOP.

INSERT - line_spec

Syntax

... | {INITIAL LINE} | {LINES OF jtab [FROM idx1] [TO idx2]} ... . Alternatives: 1. ... wa 2. ... INITIAL LINE 3. ... LINES OF jtab [FROM idx1] [TO idx2] Effect You can insert a work area wa, an initial line INITIAL LINE or multiple lines of an internal table jtab. Alternative 1 ... wa Effect A new line is generated and filled with the content of the work area wa. wa must be compatible with the line type of the internal table when you insert the lines or work area using the table key. When you insert the lines or work area using the table index, wa can be incompatible with the line type of the internal table and may be converted to the line type according to the conversion rules, if necessary. Note Outside of classes, you can omit the specification of wa INTO if the internal table contains a header line itab with the same name. In this case, the statement implicitly uses the header line as the work area. Alternative 2 ... INITIAL LINE Effect A new line is inserted in which each component contains the type-specific initial value from the table of value ranges of built-in ABAP types. Alternative 3 ... LINES OF jtab [FROM idx1] [TO idx2] Effect The lines of an internal table jtab are inserted one after another according to the same rules as for the insertion of a work area. The line types of itab and jtab must be compatible. If jtab is an index table, you can specify FROM idx1 and TO idx2 to restrict the lines to be inserted. In this case, only the table lines from the table idx1 onward and/or up to the table index idx2 are inserted from jtab. Data objects of the type i are expected for idx1 and idx2. If idx1 or idx2 is less than or equal to 0, an exception that cannot be handled is raised. If idx1 is greater than idx2 or greater than the number of table lines, no lines are inserted. If idx2 is greater than the number of table lines, it is set to the number of table lines. Note If a conflict with an already existing unique table key occurs, an exception that cannot be handled is caused when inserting multiple lines from an internal table. ... {TABLE itab} | {itab [INDEX idx]} ... . Alternatives: 1. ... TABLE itab 2. ... itab INDEX idx Effect These alternatives specify at which position of the internal table itab lines are to be inserted. If you use the option with the addition TABLE, the position at which the line is inserted, is specified using the table key. If you use the option with the addition INDEX, the table index is used for the specification. The latter option can only be used for index tables. Alternative 1 ... TABLE itab Effect The lines line_spec to be inserted must be compatible with the line type of the internal table. Depending on the table type, each line is inserted as follows: For standard tables, each new line is appended as the last line in the internal table regardless of the table key. For sorted tables, each new line is inserted into the sort order of the internal table according to its key values and the table index of the following lines is increased by one. If the internal table does not have a unique key, duplicate entries are inserted before the existing line. For hashed tables, each new line is inserted into the internal table by the hash adminstration according to its key values. If the internal table has a unique key, duplicate entries are not inserted. When inserting single lines, sy-subrc is set to 4. When inserting multiple lines, an exception that cannot be handled is triggered. Example Filling an internal table connection_tab with data of the database table spfli. Single lines are inserted using the table key and are filled with the content of the work area connection. Since the internal table has a unique key, duplicate entries are rejected. The better performing SELECT statement for which the internal table is specified directly after INTO TABLE could raise an exception due to the uniqueness of the table key in the WHERE condition used here. DATA: BEGIN OF connection, cityfrom TYPE spfli-cityfrom, cityto TYPE spfli-cityto, distid TYPE spfli-distid, distance TYPE spfli-distance, END OF connection. DATA connection_tab LIKE SORTED TABLE OF connection WITH UNIQUE KEY cityfrom cityto distid distance. SELECT cityfrom cityto distid distance FROM spfli INTO connection. INSERT connection INTO TABLE connection_tab. ENDSELECT. Alternative 2 ... itab INDEX idx Effect This variant can only be used for standard tables and sorted tables. Each line line_spec to be inserted into the line before the table index idx and the table index of the following lines is increased by one. A data object of the type i is expected for idx. If idx contains a value equal to the number of the existing table lines plus one, the new line is appended as the last line in the internal table. If idx contains a greater value, no line is inserted and sy-subrc is set to 4. An exception that cannot be handled is raised when: idx contains a value less than or equal to 0 A line to be inserted would cause a duplicate entry in tables with a unique table key A line to be inserted would disrupt the sort order of sorted tables Within a LOOP loop, you can omit the addition INDEX. Each line to be inserted is inserted before the current table line of the LOOP loop. However, if the current line is deleted in the same loop pass, the response is undefined. INSERT - result Syntax ... { ASSIGNING <fs> } | { REFERENCE INTO dref }... . Effect These additions can only be used when inserting single lines. If they are inserted successfully, the inserted line is assigned to a field symbol <fs> using the addition ASSIGNING and a reference is set to the inserted line in the reference variable. using the addition REFERENCE INTO. Syntax and meaning are the same as for the specification of the output response in the statement READ TABLE and the same restrictions apply for the manipulation of key fields for sorted tables and hashed tables. 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 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. The row type must be flat and all components that are not part of the table key must have a numeric data type ( i, p, f). If the internal table does not already contain a row with an identical key, the COLLECT statement has the same effect as the following form of the INSERT statement: INSERT wa INTO TABLE itab [result]. A row, whose position depends on the table key and the table type, is inserted and filled with the contents of wa. 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. Note 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. 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. COLLECT - result Syntax ... { ASSIGNING <fs> } | { REFERENCE INTO dref }... . Effect With the ASSIGNING addition, the inserted or existing row is assigned to a field symbol <fs> and with the REFERENCE INTO addition, a reference to the inserted or existing row is set in a reference variable. The syntax and meaning are the same as when specifying the output behavior in the READ TABLE statement and the same restrictions regarding the manipulation of key fields for sorted and hashed tables apply. 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. 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). 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. APPEND - line_spec Syntax ...

| {INITIAL LINE}

| {LINES OF jtab [FROM idx1] [TO idx2]} ... .

Alternatives:

1. ... wa

2. ... INITIAL LINE

3. ... LINES OF jtab [FROM idx1] [TO idx2]

Effect

You can append either a workarea wa, an inital row INITIAL LINE or several rows of an internal table jtab.

Alternative 1

... wa

Effect

A new row is appended to which the content of the workarea wa is assigned. wa may be incompatible to the row type of the internal table; if so, it is converted according to the conversion rules into the row type.

Note

Outside of classes, you can omit wa TO if the internal table has a header line itab of the same name. The statement implicitly uses the header line as the workarea.

Alternative 2

... INITIAL LINE

Effect

A new row is appended, for which each component contains the initial value of correct type from the table of the value ranges of built-in ABAP types.

Alternative 3

... LINES OF jtab [FROM idx1] [TO idx2]

Effect

The rows of an internal table jtab are appended according to the same rules that apply for appending a workarea, in the sequence in which they exist in jtab. If jtab is an index table, you can restrict the rows to be appended by specifying FROM idx1 and TO idx2. In this case, only the table rows starting at table index idx1 or ending at table index idx2 from jtab are appended. For idx1 and idx2, data objects of type i are expected. If the value idx1 or idx2 is less than 0, an untreatable exception occurs. If idx1 is greater than idx2 or greater than the number of table rows, no rows are appended. If idx2 is greater than the number of table rows, it is set to the number of table rows.

Example

Appending square numbers to a sorted table with elementary row type.

DATA: int TYPE i,

itab LIKE SORTED TABLE OF int

WITH UNIQUE KEY table_line.

DO 10 TIMES.

int = sy-index ** 2.

APPEND int TO itab.

ENDDO.

APPEND - result

Syntax

... { ASSIGNING <fs> }

| { REFERENCE INTO dref }... .

Effect

You can use these additions only to append single rows. If the append was successful, you use the addition ASSIGNING to assign the appended row to a field symbol <fs>; you use addition REFERENCE INTO to set a reference to the appended row in a reference variable.

Syntax and function are similar to the specification of the output behavior in the statement READ TABLE and the same restrictions apply as far as manipulation of key fields of sorted tables and hashed tables are concerned.

this is clear explanation about ur question. if u have any more questions please ask me regarding my explanation.

Former Member
0 Kudos

hi

i think the explanation along with the following examples is self explanatory.

read that once n complie the code 2 understand it in a better way.

plz let me know if the following stuff helps u

APPEND

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

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.

DATA:

BEGIN OF FS_AIRLINE,

CARRID(5) TYPE C, " Carrier id

FLICONNID(4) TYPE N, " Connection id

FLITDATE LIKE SY-DATUM, " Flight date

END OF FS_AIRLINE.

"----


  • Internal table to hold Flight details *

"----


DATA:

T_AIRLINE LIKE

STANDARD TABLE

OF FS_AIRLINE

WITH NON-UNIQUE KEY FLICONNID.

PERFORM POPULATE_TABLE.

PERFORM DISPLAY_TABLE.

&----


*& Form POPULATE_TABLE *

&----


  • This subroutine populates the internal table with flight details. *

----


  • There are no interface parameters to be passed to this subroutine. *

----


FORM POPULATE_TABLE.

CLEAR FS_AIRLINE.

FS_AIRLINE-CARRID = 'AA'.

FS_AIRLINE-FLICONNID = '0017'.

FS_AIRLINE-FLITDATE = '19991212'.

APPEND FS_AIRLINE TO T_AIRLINE.

CLEAR FS_AIRLINE.

FS_AIRLINE-CARRID = 'DL'.

FS_AIRLINE-FLICONNID = '1984'.

FS_AIRLINE-FLITDATE = '20000506'.

APPEND FS_AIRLINE TO T_AIRLINE.

CLEAR FS_AIRLINE.

FS_AIRLINE-CARRID = 'AA'.

FS_AIRLINE-FLICONNID = '0026'.

FS_AIRLINE-FLITDATE = '19990804'.

APPEND FS_AIRLINE TO T_AIRLINE.

CLEAR FS_AIRLINE.

FS_AIRLINE-CARRID = 'UA'.

FS_AIRLINE-FLICONNID = '0941'.

FS_AIRLINE-FLITDATE = '20001111'.

APPEND FS_AIRLINE TO T_AIRLINE.

CLEAR FS_AIRLINE.

FS_AIRLINE-CARRID = 'LH'.

FS_AIRLINE-FLICONNID = '1699'.

FS_AIRLINE-FLITDATE = '20000201'.

APPEND FS_AIRLINE TO T_AIRLINE.

CLEAR FS_AIRLINE.

FS_AIRLINE-CARRID = 'LH'.

FS_AIRLINE-FLICONNID = '0026'.

FS_AIRLINE-FLITDATE = '19770901'.

APPEND FS_AIRLINE TO T_AIRLINE.

ENDFORM. " POPULATE_TABLE

&----


*& Form DISPLAY_TABLE *

&----


  • This subroutine displays the contents of internal table. *

----


  • There are no interface parameters to be passed to this subroutine. *

----


FORM DISPLAY_TABLE.

LOOP AT T_AIRLINE INTO FS_AIRLINE.

WRITE:

/10 FS_AIRLINE-CARRID,

20 FS_AIRLINE-FLICONNID,

50 FS_AIRLINE-FLITDATE.

ENDLOOP. " LOOP AT T_AIRLINE...

INSERT - line_spec

Syntax

...

| {INITIAL LINE}

| {LINES OF jtab [FROM idx1] [TO idx2]} ... .

Effect

You can insert a work area wa, an initial line INITIAL LINE or multiple lines of an internal table jtab.

DATA: BEGIN OF connection,

cityfrom TYPE spfli-cityfrom,

cityto TYPE spfli-cityto,

distid TYPE spfli-distid,

distance TYPE spfli-distance,

END OF connection.

DATA connection_tab LIKE SORTED TABLE OF connection

WITH UNIQUE KEY cityfrom cityto

distid distance.

SELECT cityfrom cityto distid distance

FROM spfli

INTO connection.

INSERT connection INTO TABLE connection_tab.

ENDSELECT.

COLLECT - result

Syntax

... { ASSIGNING <fs> }

| { REFERENCE INTO dref }... .

Effect

With the ASSIGNING addition, the inserted or existing row is assigned to a field symbol <fs> and with the REFERENCE INTO addition, a reference to the inserted or existing row is set in a reference variable.

The syntax and meaning are the same as when specifying the output behavior in the READ TABLE statement and the same restrictions regarding the manipulation of key fields for sorted and hashed tables apply

regards,

kiran kumar k