Internal Table Operations - SAP ABAP.


Internal tables are an important feature of the ABAP language. Internal tables are preferably used to store and format the content of database tables from within a program. Furthermore, internal tables in connection with structures are an important means of defining complex data structures in an ABAP program.


The representation and processing on Internal Table plays a key role in the development Therefore, it is necessary to have well knowledge about its operation. For the developer who are new to SAP, it is somehow difficult to understand how these operation takes place and works. Here we elaborate on different operation of Internal Table with new advanced Syntax.



INTRODUCTION:

When you define internal tables, you can select one of three table types: Standard, Sorted, or Hashed. The table type determines how an application can store and access data in the table.

Storage and access are simplest for standard tables. In terms of functionality, there is little difference between the standard table type and other table types, except for the option of using unique keys. In fact, some access types that use indexes are not possible with sorted and hashed tables.

The main reason for using sorted and hashed tables is not to gain additional features but to improve efficiency when accessing large internal tables.

Internal table operations are most important for a ABAP developer, below are some of the most important internal table operations.

  • APPEND 
  • INSERT 
  • SORT 
  • DESCRIBE TABLE 
  • READ TABLE WITH KEY 
  • READ TABLE WITH INDEX
  • LOOP ……. ENDLOOP
  • MODIFY
  • DELETE
  • DELETE ADJACENT DUPLICATE
  • CLEAR, FREE
  • MOVE
  • COLLECT


APPEND:


Append statement is used to append or add a record from work area to internal table, the new record will be added at the end of the internal table.

(NOTE: Append will not work for ‘HASHED’ table, it will also lead to dump in case you try to add entries in improper sort manner in case of ‘SORTED’ table)


APPENDING SINGLE LINE

To add line to an index table, use the syntax.

APPEND wa_line TO itab.
APPEND INITIAL LINE TO itab.

Line is either work area wa that is convertible to line type, or the expression INITIAL LINE. If you use wa, the system adds a new line to the internal table itab and fills it with the contents of the work area. INITIAL LINE appends a blank line containing the correct initial value for each field of the structure. After each APPEND statement, the system field sy-tabix contains the index of the appended line.

Appending lines to standard tables and sorted tables with a non-unique key works regardless of whether lines with the same key already exist in the table. Duplicate entries may occur. A runtime error occurs if you attempt to add a duplicate entry to a sorted table with a unique key. Equally, a runtime error occurs if you violate the sort order of a sorted table by appending to it.


APPENDING SEVERAL LINE

It is possible to append internal tables to index tables using the following syntax:

APPEND LINES OF itab1 TO itab2.

This statement appends the whole of itab1 to itab2. itab1 can be any type of table. The line type of itab1 must be convertible into the line type of itab2.

When you append an index table to another index table, you can specify the lines to be appended as follows:

APPEND LINES OF itab1 FROM <n1>TO <n2> TO itab2.

n1 and n2 specify the indexes of the first and last lines of itab1 that you want to append to itab2.

This method of appending lines of one table to another is about 3 to 4 times faster than appending them line by line in a loop. After the APPEND statement, the system field sy-tabix contains the index of the last line appended. When you append several lines to a sorted table, you must respect the unique key (if defined), and not violate the sort order. Otherwise, a runtime error will occur.


RANKED LIST


You can use the APPEND statement to create ranked lists in standard tables. To do this, create an empty table, and then use the syntax:

APPEND wa TO itab SORTED BY <field>.

The new line is not added to the end of the internal table itab. Instead, the table is sorted by field in descending order. The work area wa must be compatible with the line type of the internal table. You cannot use the SORTED BY addition with sorted tables.

When you use this technique, the internal table may only contain as many entries as you specified in the INITIAL SIZE parameter of the table declaration. This is an exception to the general rule, where internal tables can be extended dynamically. If you add more lines than specified, the last line is discarded. This is useful for creating ranked lists of limited length (for example "Top Five"). You can use the APPEND statement to generate ranked lists containing up to 100 entries. When dealing with larger lists, it is advisable to sort tables normally for performance reasons.



INSERT

INSERT WITHOUT INDEX

INSERTING SINGLE LINE

To add a line to an internal table, use the syntax:

INSERT wa_line INTO TABLE itab.

Line is either a work area that is compatible with the line type, or the expression INITIAL LINE. The work area must be compatible because the fields in the table key must be filled from fields of the correct type. INITIAL LINE inserts an initialized blank line suitable for the type.

If the internal table has a unique key, lines whose key already exists in the table will not be inserted and sy-subrc is set to 4. When the system successfully adds a line to the table, sy-subrc is set to 0.

Lines are added to internal tables as follows:
  • Standard Tables: The line is appended to the end of the internal table. The statement works in the same way as the specific statement APPEND for appending lines.
  • Sorted Tables: 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.
  • Hashed Tables: The table is inserted into the internal hash administration according to the table key.

INSERTING SEVERAL LINES

To add several lines to an internal table, use the syntax:

INSERT LINES OF itab1 FROM <n1> TO <n2> INTO TABLE itab2.

itab1 and itab2 are tables with a compatible line type. The system inserts the lines of table itab1 one by one into table itab2 using the same rules as for single lines.

If itab1 is an index table, you can specify the first and last lines of itab1 that you want to append in n1 and n2.

Depending on the size of the tables and where they are inserted, this method of inserting lines of one table into another can be up to 20 times faster than inserting them line by line in a loop.



INSERT USING INDEX


The INSERT statement allows you not only to insert lines in any type of internal table, but also to change them using a line index. You can insert either a single line or a group of lines into index tables using the index.


INSERTING SINGLE LINE

To insert a line into an index table, use the syntax:

INSERT wa_line INTO itab INDEX <idx>.

line is either a work area wa that is convertible to the line type, or the expression INITIAL LINE. If you use wa, the system adds a new line to the internal table itab and fills it with the contents of the work area. INITIAL LINE inserts an initialized blank line suitable for the type.

If you use the INDEX option, the new line is inserted before the line that has the index idx. After the insertion, the new entry has the index idx and the index of the following lines is incremented by 1. If the table contains idx -1 lines, the new line is added after the last line. If the table has less than idx - 1 lines, the new line cannot be inserted, and sy-subrc is set to 4. When the system successfully adds a line to the table, sy-subrc is set to 0.

Without the INDEX addition, you can only use the above statement within a LOOP. Then the new line is inserted before the current line; idx is implicitly set to sy-tabix.

Appending lines to standard tables and sorted tables with a non-unique key works regardless of whether lines with the same key already exist in the table. Duplicate entries may occur. A runtime error occurs if you attempt to add a duplicate entry to a sorted table with a unique key. Equally, a runtime error occurs if you violate the sort order of a sorted table by appending to it.


INSERTING SEVERAL LINES

To add several lines to an internal table, use the syntax:

INSERT LINES OF itab1 INTO itab2 INDEX <idx>.

The system inserts the lines of the entire table itab1 one by one into itab2 using the same rules as for single lines. itab1 can be any type of table. The line type of itab1 must be convertible into the line type of itab2.

When you append an index table to another index table, you can specify the lines to be appended as follows:

INSERT LINES OF itab1 FROM <n1> TO <n2> INTO itab2 INDEX <idx>.

n1 and n2 specify the indexes of the first and last lines of itab1. Depending on the size of the tables and where they are inserted, this method of inserting lines of one table into another can be up to 20 times faster than inserting them line by line in a loop.


SORT

SORT WITH TABLE KEY

You can sort a standard or hashed table in a program. To sort a table by its key, use the syntax:

SORT itab (ASCENDING/DESCENDING).

The statement sorts the internal table itab in ascending/descending order by its key. The statement always applies to the table itself, not to the header line. The sort order depends on the sequence of the standard key fields in the internal table. The default key is made up of the non-numeric fields of the table line in the order in which they occur. You can specify the direction of the sort using the additions ASCENDING and DESCENDING. The default is ascending order. The larger the sort key, the more time the system needs to sort the table. If the sort key contains an internal table, the sorting process may be slowed down considerably.

(NOTE: You cannot sort a sorted table using the SORT statement. The system always maintains
these tables automatically by their sort order. If an internal table is statically recognizable as a
sorted table, the SORT statement causes a syntax error.)


SORT WITH ANOTHER SORT KEY

If you have an internal table with a structured line type that you want sort by a different key, you can specify the key in the SORT statement:

SORT itab (ASCENDING/DESCENDING)
BY <f1> (ASCENDING/DESCENDING)
      <f2> (ASCENDING/DESCENDING)
      …
      <fn> (ASCENDING/DESCENDING).

The table is now sorted by the specified components f1... fn instead of by the table key. The number of sort fields is limited to 250. The sort order depends on the sequence of the fields f1... fn. The sort sequence specified before BY applies to all fields. The sort sequence after a field applies only to that column of the table.


DESCRIBE TABLE


Describe table is used to count the number of records in an internal table. Use below syntax:

DESCRIBE TABLE itab LINES <l_count>.

Counts the no. of record of an internal table, here <l_count> is a variable that stores the no. of records available in table itab.



READ TABLE WITH KEY


1. USING TABLE KEY

To use the table key of itab as a search key, enter key as follows:

READ TABLE itab FROM wa.

Or as follows:

READ TABLE itab WITH TABLE KEY col1 =<value>
                                                                      col2 = <value> INTO wa.

Since SAP 7.40 release this statement is reduced to as shown below:

wa = itab[ KEY col1 =…….. col2 =………..].

In the first case, wa must be a work area compatible with the line type of itab. The values of the key fields are taken from the corresponding components of the work area.

In the second case, you have to supply the values of each key field explicitly. It is not allowed to specify the same key specifications several times. If you do not know the name of one of the key fields until runtime, you can specify it dynamically as the content of a field n1... nn using (n1) = f1 (n2) = f2 If the data types of f1... fn are not compatible with the key fields, the system converts them. If the row type of the internal table is not structured, you can specify a comparison with the pseudo component table line.


2. USING DIFFERENT SEARCH KEY 

READ TABLE <itab> WITH KEY k1 = f1 ... kn = fn INTO wa.

Since SAP 7.40 release this statement is reduced to as shown below:

wa = itab[ k1=……k2=…..]

The search key consists of arbitrary table fields k1... kn. If you do not know the name of a component until runtime, you can specify it dynamically as the content of a field n1... nn using (n1) = f1 (n2) = f2 If n1... nn is empty when the statement is executed, the search field is ignored. If the data types of f1... fn are not compatible with the components, the system converts them. You can restrict the search to partial fields by specifying offset and length.

If the row type of the internal table is not structured, you can specify a comparison with the pseudo-component table_line. You can also specify the attributes of objects as keys.

The search is linear for all table types. The runtime is in linear relation to the number of table lines.


3. USING FIELD SYMBOL

You can assign the table entry read from the table to a field symbol by specifying result as follows:

FIELD-SYMBOLS: <fs> TYPE any.

READ TABLE itab ASSIGNING <fs>.

Since SAP 7.40 release this statement is reduced to as shown below:

READ TABLE itab ASSIGNING FIELD-SYMBOL<fs>.

After the READ statement, the field symbol points to the table line. If the line type is structured, you should specify the same type for the field symbol when you declare it. This allows you to address the components of the field symbol. If you cannot specify the type statically, you must use further field symbols and the technique of assigning components of structures to address the components of the structure.


READ TABLE WITH INDEX

You can use the READ statement to read lines in tables using their index. To read a single line of an index table, use the statement:

READ TABLE itab INDEX <idx> INTO wa.

Since SAP 7.40 release this statement is reduced to as shown below:

wa = itab[idx]

To access specific entry in table:

wa-<Col> = itab[idx] -<Col>

The system reads the line with the index idx from the table itab. This is quicker than searching using the key. The result part can specify a further processing option for the line that is retrieved. If an entry with the specified index was found, the system field sy-subrc is set to 0 and sy-tabix contains the index of that line. Otherwise, sy-subrc is set to a value other than 0.

If idx is less than or equal to 0, a runtime error occurs. If idx is greater than the number of lines in the table, sy-subrc is set to 4.


LOOP…. ENDLOOP

LOOP…….ENDLOOP is also used to read/insert data from a internal table into work area, this is used to read multiple records serially one after one.

Syntax 1: 

LOOP AT itab INTO wa.
…………………………………..
ENDLOOP.

wa must be a work area compatible with the line type of itab. This will read all the records from itab serially one after another.

Syntax 2: 

LOOP AT itab INTO wa WHERE <f1> = value.
…………………..
ENDLOOP.

This syntax will read the records from the specified field into a work area.

Syntax 3: 

LOOP AT itab INTO wa FROM <idx1> TO <idxn>.
………………..
ENDLOOP.

The records of an internal table from specified index values (idx) will be read in wa serially. This will help to reduce the unnecessary transfer of data into wa.

Syntax 4: 

LOOP AT itab ASSIGNING <fs>.
……………………..
ENDLOOP.

Since 7.40 release this can be defined as:

LOOP AT itab ASSIGNING FIELD-SYMBOL <fs>.
…………………………………
ENDLOOP.

The values of the internal table pass to the field symbol serially one after another to perform further operations.


MODIFY

MODIFY is used to modify single or multiple internal table records based on condition. TRANSPORTING is a keyword which is used to specify list of fields to be modified instead of all fields.


MODIFY ITAB FROM WA INDEX <INDEX NO> TRANSPORTING <FIELD1> <FIELD2>.

OR 

MODIFY <ITAB> FROM <WA> TRANSPORTING <FIELD1> <FIELD2> WHERE <fieldn=………>.

Above both syntax will change/modify the value of the fields specified after TRANSPORTING keyword.

Since 7.40 release the MODIFICATION can be done as illustrated by the below given example.

* Old Way 

FIELD-SYMBOLS: <lfs_data> LIKE LINE OF t_data.

READ TABLE itab ASSIGNING <lfs_data> WITH KEY <field1> = 10.
IF sy-subrc EQ 0.
  <lfs_data> = 20.
ENDIF.

* New Way

itab[ field1 = 10 ] = 20.


DELETE

DELETE is used to delete single or multiple records from an internal table from work area based on some condition.

Syntax 1: 

DELETE ITAB INDEX <INDEX NO>.

This statement will delete the entry from the table specified by the index number.

Syntax 2: 

DELETE ITAB WHERE <FIELD1> = <FIELD1 VALUE> (OR/AND)
                                           <FIELD2> = <FIELD2 VALUE>.

This will delete the row depending upon the values specified by the field values


DELETE ADJACENT DUPLICATE

DELETE ADJACENT DUPLICATES is used to delete duplicate records which are adjacent to each-other. Pre-requisite for this is the internal table must be sorted in ascending order.

Syntax 1: 

Using any key.

DELETE ADJACENT DUPLICATE FROM ITAB.

adjacent duplicated will be deleted in internal table comparing all fields.

Syntax 2: 

Comparing specified fields.

DELETE ADJACENT DUPLICATE FROM ITAB COMPARING <field1><field2>.

adjacent duplicates will be deleted comparing specified fields.


CLEAR & FREE


1. CLEAR: It is used to clear values in work area, internal table and variable.

CLEAR wa.
OR
CLEAR itab. (clear work area or variable).


2. FREE: Is used to clear (FREE) memory of a internal table or work area. We all know that whenever we declare a internal table or work area, 8kb initial memory will be allocated.

FREE wa.
OR
FREE itab.


MOVE CORRESPONDING 

MOVE-CORRESPONDING itab1 TO itab2.

Or

MOVE-CORRESPONDING struct1 TO struct2.

The statement MOVE-CORRESPONDING is used to assign components with the same name in structured data objects to each other. There are two variants of the statements, which can call each other internally:

Both operands are structures.

Both operands are internal tables.

No other combinations of operand types are possible. Field symbols typed with the generic type ANY or formal parameters can also be used as operands. An operand of this type must be either a structure or an internal table when the statement is executed and match the other operands; if not, an unhandleable exception is raised. struct1 and itab1 are functional operand positions.


COLLECT

The COLLECT statement is a special technique for filling an internal table. The system aggregates the table during the insert operation. If the table does not have a line with the same key yet, the COLLECT statement has the same effect as inserting by key access. If the table already contains one or more lines with this key, the system sums the values of non-key fields in the new line to the corresponding values in the top line.

(HINT: The COLLECT statement is only allowed when all non-key fields are numeric (TYPE I). The syntax check reports an error when this prerequisite is not met.) The COLLECT statement is key access, which means you can use it for all three table types.

COLLECT wa INTO itab.

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

Comments

Also Read...