SAP Database

APPEND: ABAP Keyword a day

Print This Post Email This Post Written by admin on Oct 31st, 2007 | Filed under: ABAP Keywords

APPEND

Variants:
1. APPEND [wa TO|INITIAL LINE TO] itab.
2. APPEND LINES OF itab1 [FROM idx1] [TO idx2] TO itab2.
3. APPEND [wa TO] itab SORTED BY f.

Variant 1
APPEND [wa TO|INITIAL LINE TO] itab.
Appends a new line to the end of the internal table itab. If you specify wa TO, the new line is taken from the contents of the explicitly specified work area wa.
If you use INITIAL LINE TO, a line filled with the correct value for the type is added. If the specification before itab is omitted, the new line is taken from the internal tbale itab. After the APPEND, the system field SY-TABIX contains the index
of the newly added table entry.

Examples
Generate a list with customer numbers:
TABLES SCUSTOM.
DATA: CUSTOMER LIKE SCUSTOM-ID OCCURS 0.

APPEND SCUSTOM-ID TO CUSTOMER.

Append a blank line or a line with its initial value to the above list:

APPEND INITIAL LINE TO CUSTOMER

Generate a compressed list with plane data

PARAMETERS: SEATS_LO LIKE SAPLANE-SEATSMAX DEFAULT 30,
SEATS_HI LIKE SAPLANE-SEATSMAX DEFAULT 50.

DATA: PLANE LIKE SAPLANE OCCURS 0,
PLANE_NEEDED LIKE SAPLANE WITH HEADER LINE.

LOOP AT PLANE INTO PLANE_NEEDED
WHERE SEATSMAX BETWEEN SEATS_LO AND SEATS_HI.

APPEND PLANE_NEEDED.

ENDLOOP.

Notes Performance:
1. When using internal tables with a header line, avoid unnecessary assignments to the header line. Whenever possible, use statements which have an explicit work area.

For example, “APPEND wa TO itab.” is approximately twice as fast as “itab = wa. APPEND itab.”. The same applies to COLLECT and INSERT.

2. In contrast to COLLECT, APPEND does not check whether an entry with the same default 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.

3. The runtime required for APPEND increases with the line width of the table and depends on the number of fields. Appending an entry to an internal table with a width of 111 bytes takes about 9 msn (standardized microseconds).

4. To append an internal table to another internal table, you should use the variant APPEND LINES OF … which is 3 to 4 times faster than using a LOOP to process the source table and append the entries line-by-line to the target table.

Variant 2
APPEND LINES OF itab1 [FROM idx1] [TO idx2] TO itab2.

Effect Appends the internal table itab1 or an extract from itab1 to the end of the internal table itab2. By specifying FROM idx1 or TO idx2 you can restrict the line area taken from the source table itab1. If there is no FROM specification, it begins with the first line of itab1. If there is no TO specification, it ends with the last line of itab1. This means that the complete table is appended if neither a FROM nor a TO is specified.

After the APPEND, the system field SY-TABIX contains the index of the last table entry appended, i.e. the total number of entries from both tables.

Note By comparing the values of SY-TABIX before and after the APPEND statement, you can determine how many lines were appended to the table.

Example Merge two tables with whole numbers:

DATA: ITAB1 TYPE I OCCURS 100,
ITAB2 TYPE I OCCURS 100.

APPEND 2 TO ITAB1.
APPEND 3 TO ITAB1.
APPEND 5 TO ITAB1.
APPEND 7 TO ITAB1.
APPEND 3 TO ITAB2.
APPEND INITIAL LINE TO ITAB2.
APPEND LINES OF ITAB1 FROM 2 TO 20 TO ITAB2.

The table ITAB2 now contains five lines with the values 3, 0, 3, 5 and 7.

Note Performance:
This variant is 3 to 4 times faster than using a LOOP to process the source table and append the entries line-by-line to the target table.

Variant 3
APPEND [wa TO] itab SORTED BY f.

Effect Inserts the new entry into table and re-sorts the table by the sub-field f in descending order. This only makes sense if the table was sorted beforehand. When the number of table entries reaches the OCCURS parameter value, the last entry is deleted if the value f of a new entry is greater (particularly suitable for ranked lists). You can only sort by one sub-field.
If you specify wa TO, the new line is taken from the contents of the explicitly specified work area wa. Otherwise, it comes from the header line of the internal table itab.

Example
DATA: BEGIN OF COMPANIES OCCURS 3,
NAME(10), SALES TYPE I,
END OF COMPANIES.

COMPANIES-NAME = ‘big’.

COMPANIES-SALES = 90.
APPEND COMPANIES.

COMPANIES-NAME = ’small’.

COMPANIES-SALES = 10.
APPEND COMPANIES.

COMPANIES-NAME = ‘too small’.
COMPANIES-SALES = 5.

APPEND COMPANIES.

COMPANIES-NAME = ‘middle’.
COMPANIES-SALES = 50.
APPEND COMPANIES SORTED BY SALES.

The table now has three (-> OCCURS 3) entries. The line with the contents ‘too small’ in the sub-field NAME is deleted from the table because the entry for ‘middle’ has a greater value in
the sub-field SALES. This entry now appears in the second table line (after ‘big’ and before ’small’).

Notes
1. Whenever an internal table is processed with APPEND SORTED BY, it should always be filled in this way.
2. If you specify APPEND with the parameter SORTED BY, the system always searches the entire table. Therefore, it is sometimes better to create the table with a simple APPEND
and then use SORT to sort in descending ot ascending order afterwards. You can also sort in ascending order by first determining the insert position with READ TABLE itab WITH KEY f = itab-f BINARY SEARCH and then by inserting the new entry into the table (perhaps read SY-SUBRC beforehand) with INSERT itab INDEX SY-TABIX. However, you should be aware that, in such cases, the table may contain more entries than specified in the OCCURS
parameter.
3. If several lines with an identical value f are added, lines added later are treated as smaller, i.e. they are inserted after existing lines with the same value f.
4. If you use APPEND … SORTED BY f with an explicitly specified work area, this must be compatible with the line type of the internal table.
5. If the sort criterion f is not known until runtime, you can use SORTED BY (name) to specify it dynamically as the contents of the field name. If name is blank at runtime or contains an invalid component name, a runtime error occurs.
6. Regardless of whether you specify it statically or dynamically, you can restrict the sort criterion f further by defining an offset and/or length.

Related
COLLECT itab, INSERT itab, SELECT / FETCH NEXT CURSOR … INTO/APPENDING TABLE itab, MODIFY itab, WRITE f TO itab INDEX idx, SORT itab, READ TABLE itab, LOOP AT itab, DELETE itab

ASS-RPERF is not an ABAP/4 key word (in R/3).

———————
ABAPer, mail: abap.community@gmail.com http://abaplearner.blogspot.com

If you like this post, you may as well like these too:

  1. COLLECT : ABAP Keyword a day COLLECTBasic formCOLLECT [wa INTO] itab.Addition… SORTED BY fEffectCOLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab...
  2. CLEAR : ABAP Keyword a day CLEARBasic formCLEAR f.Additions 1. … WITH g2. … WITH NULL EffectResets the contents of f to its initial value. For predefined types (see DATA ), the following initial values...
  3. EDITOR-CALL : ABAP keyword a day EDITOR-CALL Call editor for internal tables - EDITOR-CALL FOR itab. Call editor for ABAP/4 programs - EDITOR-CALL FOR REPORT prog. EDITOR-CALL - call editor for internal tables Basic form...
  4. DATA : ABAP Keyword a day DATA Variants1. DATA f.2. DATA f(len).3. DATA: BEGIN OF rec,…END OF rec.4. DATA: BEGIN OF itab OCCURS n,…END OF itab.5. DATA: BEGIN OF COMMON PART c,…END OF COMMON PART.EffectDefines...
  5. DELETE : ABAP Keyword a day DELETE Delete from a database table - DELETE FROM dbtab WHERE condition. - DELETE FROM (dbtabname) WHERE condition. - DELETE dbtab. - DELETE *dbtab. - DELETE (dbtabname) … ....



Leave a Reply

  • Help Support CRY