Work area operations in SAP ABAP

Access Methods to Individual Table Entries
There are three ways to access a single table entry:
Access Using a Work Area
When you access individual table entries using a work area, you are not working directly with the data in the table. Instead, you work with another data object as a work area. The work area is an interface to the entries in the internal table, and must be convertible into the line type of the internal table. The most efficient working method is to use a work area compatible with the line type of the internal table. When you read data from a table record, the data you are reading overwrites the current contents of the work area. You can then use this data in the program. When you write data to the internal table, this must first be placed in the work area. The system then transfers it from the work area to the appropriate table entry. Data transfer follows the rules of assigning data using MOVE.
This graphic is explained in the accompanying text
If the internal table has a header line, you can use it as the work area. The ABAP statements that you use to access individual table entries can use the header line implicitly as a work area.
Access Using Field Symbols
If you access an internal table using a field symbol , you do not need to copy the data into a work area. You can assign a line of an internal table to a field symbol. Ideally, the field symbol will have the same type as the line type of the internal table. Once you have assigned the entry to the field symbol, working with the field symbol has exactly the same effect as accessing the corresponding line directly.
This graphic is explained in the accompanying text
Access via Data References
If you access an internal table using a data reference, you do not need to copy the data into a work area. You can assign a line of an internal table to a data reference. Ideally, the data reference will have the same type as the line type of the internal table. Once you have assigned the entry to the field symbol, working with the dereferenced data reference has exactly the same effect as accessing the corresponding line directly.
This graphic is explained in the accompanying text

Inserting Lines into Tables
You can insert one or several lines into internal tables using the INSERT statement.
Inserting a Single Line
To add a line to an internal table, use the statement:
INSERT 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 statement:
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.
Examples
Example
Inserting a Single Line
REPORT demo_int_tables_insert.
DATA: BEGIN OF line,
        land(3)  TYPE c,
        name(10) TYPE c,
        age      TYPE i,
        weight   TYPE p DECIMALS 2,
      END OF line.
DATA itab LIKE SORTED TABLE OF line
          WITH NON-UNIQUE KEY land name age weight.
line-land = 'G'.   line-name   = 'Hans'.
line-age  = 20.    line-weight = '80.00'.
INSERT line INTO TABLE itab.
line-land = 'USA'. line-name   = 'Nancy'.
line-age  = 35.    line-weight = '45.00'.
INSERT line INTO TABLE itab.
line-land = 'USA'. line-name   = 'Howard'.
line-age  = 40.    line-weight = '95.00'.
INSERT line INTO TABLE itab.
line-land = 'GB'.  line-name   = 'Jenny'.
line-age  = 18.    line-weight = '50.00'.
INSERT line INTO TABLE itab.
line-land = 'F'.   line-name   = 'Michele'.
line-age  = 30.    line-weight = '60.00'.
INSERT line INTO TABLE itab.
line-land = 'G'.   line-name   = 'Karl'.
line-age  = 60.    line-weight = '75.00'.
INSERT line INTO TABLE itab.
LOOP AT itab INTO line.
  WRITE: / line-land, line-name, line-age, line-weight.
ENDLOOP.
The list output is:
F   Michele            30             60,00
G   Hans               20             80,00
G   Karl               60             75,00
GB  Jenny              18             50,00
USA Howard             40             95,00
USA Nancy              35             45,00
Single lines are inserted into a sorted internal table.
Example
Inserting Several Lines
REPORT demo_int_tables_insert_lines.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA: itab LIKE STANDARD TABLE OF line,
      jtab LIKE SORTED TABLE OF line
           WITH NON-UNIQUE KEY col1 col2.
DO 3 TIMES.
  line-col1 = sy-index. line-col2 = sy-index ** 2.
  APPEND line TO itab.
  line-col1 = sy-index. line-col2 = sy-index ** 3.
  APPEND line TO jtab.
ENDDO.
INSERT LINES OF itab INTO TABLE jtab.
LOOP AT jtab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.
The list output is:
         1         1          1
         2         1          1
         3         2          4
         4         2          8
         5         3          9
         6         3         27
The example creates two internal tables with the same line type but different table types. Each is filled with three lines. Then, the entire table itab is sorted into the sorted table jtab.
Inserting Lines Using the 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 a Single Line
To insert a line into an index table, use the statement:
INSERT 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 statement:
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.
Example
REPORT demo_int_tables_insert_ind_1.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE TABLE OF line.
DO 2 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.
ENDDO.
line-col1 = 11. line-col2 = 22.
INSERT line INTO itab INDEX 2.
INSERT INITIAL LINE INTO itab INDEX 1.
LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.
The list output is:
         1          0          0
         2          1          1
         3         11         22
         4          2          4
The example creates an internal table itab and fills it with two lines. A new line containing values is inserted before the second line. Then, an initialized line is inserted before the first line.
ExampleInserting Several Lines Through Index
REPORT demo_int_tables_insert_ind_2.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE TABLE OF line.
DO 2 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.
ENDDO.
LOOP AT itab INTO line.
  line-col1 = 3 * sy-tabix. line-col2 = 5 * sy-tabix.
  INSERT line INTO itab.
ENDLOOP.
LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.
The list output is:
         1         3          5
         2         1          1
         3         9         15
         4         2          4
The example creates an internal table itab and fills it with two lines. Using a LOOP construction, the program inserts a new line before each existing line.
Example
REPORT demo_int_tables_insert_ind_3.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA: itab LIKE TABLE OF line,
      jtab LIKE itab.
DO 3 TIMES.
  line-col1 = sy-index. line-col2 = sy-index ** 2.
  APPEND line TO itab.
  line-col1 = sy-index. line-col2 = sy-index ** 3.
  APPEND line TO jtab.
ENDDO.
INSERT LINES OF itab INTO jtab INDEX 1.
LOOP AT jtab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.
The list output is:
         1         1          1
         2         2          4
         3         3          9
         4         1          1
         5         2          8
         6         3         27
The example creates two internal tables of the same type. Each is filled with three lines. Then, the entire table itab is inserted before the first line of jtab.

Appending Table Lines
There are several ways of adding lines to index tables. The following statements have no equivalent that applies to all internal tables.
Appending a Single Line
To add a line to an index table, use the statement:
APPEND line TO itab.
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 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 Lines
You can also append internal tables to index tables using the following statement:
APPEND LINES OF itab1 TO itab2.
This statement appends the whole of itab1 to itab2. itab1can 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 Lists
You can use the APPEND statement to create ranked lists in standard tables. To do this, create an empty table, and then use the statement:
APPEND wa TO itab SORTED BY f.
The new line is not added to the end of the internal table itab. Instead, the table is sorted by field f in descending order. The work area wamust 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 Ten"). 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.
Example
DATA: BEGIN OF wa,
        col1(1) TYPE c,
        col2 TYPE i,
      END OF wa.
DATA itab LIKE TABLE OF wa.
DO 3 TIMES.
  APPEND INITIAL LINE TO itab.
  wa-col1 = sy-index. wa-col2 = sy-index ** 2.
  APPEND wa TO itab.
ENDDO.
LOOP AT itab INTO wa.
  WRITE: / wa-col1, wa-col2.
ENDLOOP.
The list output is:
          0
1         1
          0
2         4
          0
3         9
This example creates an internal table itab with two columns that is filled in the DO loop. Each time the processing passes through the loop, an initialized line is appended and then the table work area is filled with the loop index and the square root of the loop index and appended.
Example
DATA: BEGIN OF line1,
        col1(3) TYPE c,
        col2(2) TYPE n,
        col3    TYPE i,
      END OF line1,
      tab1 LIKE TABLE OF line1.
DATA: BEGIN OF line2,
        field1(1)  TYPE c,
        field2     LIKE tab1,
      END OF line2,
      tab2 LIKE TABLE OF line2.
line1-col1 = 'abc'. line1-col2 = '12'. line1-col3 = 3.
APPEND line1 TO tab1.
line1-col1 = 'def'. line1-col2 = '34'. line1-col3 = 5.
APPEND line1 TO tab1.
line2-field1 = 'A'. line2-field2 = tab1.
APPEND line2 TO tab2.
REFRESH tab1.
line1-col1 = 'ghi'. line1-col2 = '56'. line1-col3 = 7.
APPEND line1 TO tab1.
line1-col1 = 'jkl'. line1-col2 = '78'. line1-col3 = 9.
APPEND line1 TO tab1.
line2-field1 = 'B'. line2-field2 = tab1.
APPEND line2 TO tab2.
LOOP AT tab2 INTO line2.
  WRITE: / line2-field1.
  LOOP AT line2-field2 INTO line1.
    WRITE: / line1-col1, line1-col2, line1-col3.
  ENDLOOP.
ENDLOOP.
The list output is:
A
abc 12          3
def 34          5
B
ghi 56          7
jkl 78          9
The example creates two internal tables tab1 and tab2. tab2 has a deep structure because the second component of line2 has the data type of internal table tab1. line1 is filled and appended to tab1. Then, line2 is filled and appended to tab2. After clearing tab1 with the REFRESH statement, the same procedure is repeated.
Example
DATA: BEGIN OF line,
        col1(1) TYPE c,
        col2 TYPE i,
      END OF line.
DATA: itab1 LIKE TABLE OF line,
      jtab LIKE itab.
DO 3 TIMES.
  line-col1 = sy-index. line-col2 = sy-index ** 2.
  APPEND line TO itab1.
  line-col1 = sy-index. line-col2 = sy-index ** 3.
  APPEND line TO jtab.
ENDDO.
APPEND LINES OF jtab FROM 2 TO 3 TO itab1.
LOOP AT itab1 INTO line.
  WRITE: / line-col1, line-col2.
ENDLOOP.
The list output is:
1         1
2         4
3         9
2         8
3        27
This example creates two internal tables of the same type, itab and jtab. In the DO loop, itab is filled with a list of square numbers, and jtab with a list of cube numbers. Then, the last two lines of jtab are appended to itab.
Example
DATA: BEGIN OF line3,
        col1 TYPE i,
        col2 TYPE i,
        col3 TYPE i,
       END OF line3.
DATA itab2 LIKE TABLE OF line3 INITIAL SIZE 2.
line3-col1 = 1. line3-col2 = 2. line3-col3 = 3.
APPEND line3 TO itab2 SORTED BY col2.
line3-col1 = 4. line3-col2 = 5. line3-col3 = 6.
APPEND line3 TO itab2 SORTED BY col2.
line3-col1 = 7. line3-col2 = 8. line3-col3 = 9.
APPEND line3 TO itab2 SORTED BY col2.
LOOP AT itab2 INTO line3.
  WRITE: / line3-col2.
ENDLOOP.
The list output is:
         8
         5
The program inserts three lines into the internal table itab using the APPEND statement and the SORTED BY addition. The line with the smallest value for the field COL2 is deleted from the table, since the number of lines that can be appended is fixed to 2 through the INITIAL SIZE addition.

Appending Summarized Lines
The following special statement allows you to summate entries in an internal table:
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, p). You specify the line wathat you want to add as a work area that is compatible with the line type of itab.
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.
Example
REPORT demo_int_tables_COLLECT .
DATA: BEGIN OF line,
        col1(3) TYPE c,
        col2(2) TYPE n,
        col3    TYPE i,
      END OF line.
DATA itab LIKE SORTED TABLE OF line
          WITH NON-UNIQUE KEY col1 col2.
line-col1 = 'abc'. line-col2 = '12'. line-col3 = 3.
COLLECT line INTO itab.
WRITE / sy-tabix.
line-col1 = 'def'. line-col2 = '34'. line-col3 = 5.
COLLECT line INTO itab.
WRITE / sy-tabix.
line-col1 = 'abc'. line-col2 = '12'. line-col3 = 7.
COLLECT line INTO itab.
WRITE / sy-tabix.
LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2, line-col3.
ENDLOOP.
The list output is:
         1
         2
         1
abc 12          10
def 34          5
The example fills a sorted table. The first two COLLECT statements work like normal insertion statements. In the third COLLECTstatement, the first line of itab is modified. The following diagram shows the three steps:
This graphic is explained in the accompanying text

Reading Lines of Tables
To read a single line of any table, use the statement:
READ TABLE itab key result.
For the statement to be valid for any kind of table, you must specify the entry using the key and not the index. You specify the key in the key part of the statement. The result part can specify a further processing option for the line that is retrieved.
If the system finds an entry, it sets sy-subrc to zero, if not, it takes the value 4, as long as it is not influenced by one of the possible additions. If the internal table is an index table, sy-tabix is set to the index of the line retrieved. If the table has a non-unique key and there are duplicate entries, the first entry is read.
Specifying the Search Key
The search key may be either the table key or another key.
If you read entries  from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search.
Using the Table Key
To use the table key of itab as a search key, enter key as follows:
READ TABLE itab FROM wa result.
or as follows
READ TABLE itab WITH TABLE KEY k1 = f1 ... kn = fn result.
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.
READ TABLE itab WITH TABLE KEY table_line = f result.
The contents of the entire table line are compared with the contents of field f. If f is not compatible with the line type of the table, the value is converted into the line type. The search key allows you to find entries in internal tables that do not have a structured line type, that is, where the line is a single field or an internal table type.
If the line type of the internal table contains object reference variables as component comp or if the entire line type is a reference variable, the attributes of the attr object to which the respective line reference points can be specified as key values using comp->attr or table_line->attr.
The system searches for the relevant table types as follows:
·        Standard tables
Linear search, where the runtime is in linear relation to the number of table entries.
·        Sorted tables
Binary search, where the runtime is in logarithmic relation to the number of table entries.
·        Hashed tables
The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.
Using a Different Search Key
READ TABLE itab WITH KEY k1 = f1 ... kn = fn result.
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.
Specifying the Extra Processing Option
You can specify an option that specifies what the system does with the table entry that it finds.
Using a Work Area
You can write the table entry read from the table into a work area by specifying result as follows:
READ TABLE itab key INTO wa [COMPARING f1 f2 ... |ALL FIELDS]
                            [TRANSPORTING f1 f2 ... |ALL FIELDS
                                                    |NO FIELDS].
If you do not use the additions COMPARING or TRANSPORTING, the contents of the table line must be convertible into the data type of the work area wa. If you specify COMPARING or TRANSPORTING, the line type and work area must be compatible. You should always use a work area that is compatible with the line type of the relevant internal table.
If you use the COMPARING addition, the specified table fields f1 ... fn of the structured line type are compared with the corresponding fields of the work area before being transported. If you use the ALL FIELDS option, the system compares all components. If the system finds an entry with the specified key key and if the contents of the compared fields are the same, sy-subrc is set to 0. If the contents of the compared fields are not the same, sy-subrc returns the value 2. If the system cannot find an entry, sy-subrc is set to 4. If the system finds an entry, it copies it into the target work area regardless of the result of the comparison.
If you use the TRANSPORTING addition, you can specify the table fields f1 ... fn  of the structured line type that you want to transport into the work area. If you specify ALL FIELDS without TRANSPORTING, the contents of all of the fields are transported. If you specify NO FIELDS, no fields are transported. In the latter case, the READ statement only fills the system fields sy-subrc and sy-tabix. Specifying the work area wa with TRANSPORTING NO FIELDS is unnecessary, and should be omitted.
In both additions, you can also specify the fields f1 ... fn … dynamically using (n1) (n2)… as the contents of a field  n1 ... nn. If n1 ... nn is empty when the statement is executed, it is ignored. You can also restrict all fields f1 ... fn to subfields by specifying offset and length.
Using a Field Symbol
You can assign the table entry read from the table to a field symbol by specifying result as follows:
READ TABLE itab key ASSIGNING <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.
For further information about assigning table lines to field symbols, refer to Access Using Field Symbols.
Example
REPORT demo_int_tables_read_comparing.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY col1.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
line-col1 = 2.
line-col2 = 3.
READ TABLE itab FROM line INTO line COMPARING col2.
WRITE: 'sy-subrc =', sy-subrc.
SKIP.
WRITE: / line-col1, line-col2.
The list output is:
sy-subrc =    2
         2        4
The program fills a hashed table with a list of square numbers. The work area LINE, which is compatible with the line type, is filled with the numbers 2 and 3. The READstatement reads the line of the table in which the key field col1has the same value as in the work area and copies it into the work area. sy-subrcequals 2, because different numbers are found when compared to the field col2.
Example
REPORT demo_int_tables_read_transport .
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
CLEAR line.
READ TABLE itab WITH TABLE KEY col1 = 3
                INTO line TRANSPORTING col2.
WRITE:   'sy-subrc =', sy-subrc,
       / 'sy-tabix =', sy-tabix.
SKIP.
WRITE: / line-col1, line-col2.
The list output is:
sy-subrc=    0
sy-tabix =       3
         0        9
The program fills a sorted table with a list of square numbers. The READ statement reads the line of the table in which the key field col1 has the value 3. Only the content of col2is copied to the work area LINE. sy-subrc has the value zero and sy-tabix has the value 3, because itabis an index table.
Example
REPORT demo_int_tables_read_transp_no .
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
READ TABLE itab WITH KEY col2 = 16  TRANSPORTING NO FIELDS.
WRITE:   'sy-subrc =', sy-subrc,
       / 'sy-tabix =', sy-tabix.
The list output is:
sy-subrc =    0
sy-tabix =       4
The program fills a sorted table with a list of square numbers. The READ statement reads the line of the table in which the key field col2 has the value 16. It does not use the table key. No fields are copied to a work area or assigned to a field symbol. sy-subrcis zero, since a line was found, and sy-tabix is four.
Example
REPORT demo_int_tables_read_assigning .
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY col1.
FIELD-SYMBOLS <fs> LIKE LINE OF itab.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
READ TABLE itab WITH TABLE KEY col1 = 2 ASSIGNING <fs>.
<fs>-col2 = 100.
LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2.
ENDLOOP.
The list output is:
         1        1
         2      100
         3        9
         4       16
The program fills a hashed table with a list of square numbers. The READ statement reads the line of the table in which the key field col1 has the value 2 and assigns it to the field symbol <fs> . The program then assigns the value 100 to component col2 of <fs> . This also changes the corresponding table field.

Reading Lines Using the 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 result.
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-tabixcontains 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.
Example
REPORT demo_int_tables_read_index.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.
FIELD-SYMBOLS <fs> LIKE LINE OF itab.
DO 20 TIMES.
  line-col1 = sy-index.
  line-col2 = 2 * sy-index.
  APPEND line TO itab.
ENDDO.
READ TABLE itab ASSIGNING <fs> INDEX 7.
WRITE:   sy-subrc, sy-tabix.
WRITE: / <fs>-col1, <fs>-col2.
The list output is:
    0         7
         7         14
The example creates a sorted table itaband fills it with 20 lines. The line with index 7 is read and assigned to the field symbol <fs>.

Processing Table Entries in Loops
You can use the LOOP statement to process special loops for any internal table.
LOOP AT itab result condition.
  statement block
ENDLOOP.
This reads the lines of the table one by one as specified in the result part of the LOOP statement. You can then process them in the statements within the LOOP - ENDLOOP control structure. You can either run the loop for all entries in the internal table, or restrict the number of lines read by specifying a condition. Control level processing is allowed within the loop.
The sequence in which the lines are processed depends on the table type:
·        Standard tables and sorted tables
The lines are processed according to the linear index. Within the processing block, the system field sy-tabix contains the index of the current line.
·        Hashed tables
As long as the table has not been sorted, the lines are processed in the order in which you added them to the table. Within the processing block, the system field sy-tabix is always 0.
You can nest LOOP blocks. When you leave the loop, sy-tabix has the same value as when you entered it. After the ENDLOOP statement, sy-subrc is zero if at least one table entry was processed. Otherwise, it is 4.
The loop may not contain any operations on entire internal tables  that change the table. However, you should remember that even saving a global internal table with the LOCAL statement in a procedure is a change operation on the entire table, since it exchanges the table contents. When you call procedures within loops, you should therefore check that it does not change the entire internal table. If you change the table, the loop can no longer work properly.
If you insert or delete a table entry within a loop pass, it is taken into account in subsequent loop passes as follows:
·        If you insert a line after the current line, it will be processed in a subsequent loop pass.
·        If you delete a line after the current line, it will not be processed in a subsequent loop pass.
·        If you insert a line before or at the current line, the internal loop counter will be increased accordingly.
·        If you delete a line before or at the current line, the internal loop counter will be decreased accordingly.
Specifying the Extra Processing Option
The processing option specifies how a table line is available in the statement block of the list.
Using a Work Area
To place the current loop line into a work area, specify result as follows:
LOOP AT itab INTO wa bedingung.
The contents of the table lines must be convertible into the data type of the work area wa. In each loop pass, one line of the table is copied into the work area. The end of the loop does not affect the work area, that is, the contents of wa are the same after the ENDLOOP statement as they were in the final loop pass. If no table entries are processed in the loop, because the table is entry, or no line meets the condition cond, the work area is not changed.
Using a Field Symbol
To assign the contents of the current loop line to a field symbol, specify result as follows:
LOOP AT itab ASSIGNING <fs> condition.
In each loop pass, the field symbol <fs> points to the table entry read in that pass. 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.
The end of the loop does not affect the field symbol, that is, after ENDLOOP it is still assigned to the same line as in the final loop pass. If no table entries are processed in the loop, because the table is entry, or no line meets the condition cond, the field symbol is not changed.
For further information about assigning table lines to field symbols, refer to Access Using Field Symbols.
Suppressing the Assignment of Lines
If you do not need to transfer the contents of the current table line to a work area or assign them to a field symbol, you can use the following statement:
LOOP AT itab TRANSPORTING NO FIELDS condition.
This form of the LOOP statement is useful if you want to find the index of a particular internal table in the system field sy-tabix, or the number of rows in a table that meet a particular condition.
Enter conditions
To avoid reading all of the lines in an internal table, you can specify a condition for the line selection as follows:
LOOP AT itab result WHERE cond.
This processes all of the lines that meet the logical condition  cond. The logical expression cond can consist of more than one comparison. In each comparison, the first operand must be a component of the line structure. If the table lines are not structured, the first operand can also be the expression table_line. The comparison then applies to the entire line.
If the line type of the internal table contains object reference variables as component comp or if the entire line type is a reference variable, the attributes of the attr object to which the respective line reference points, can be specified as first field of a comparison using comp->attr or table_line->attr.
Control level processing
Control level processing is allowed within a LOOP over an internal table. This means that you can divide sequences of entries into groups based on the contents of certain fields.
Internal tables are divided into groups according to the sequence of the fields in the line structure. The first column defines the highest control level and so on. The control level hierarchy must be known when you create the internal table.
The control levels are formed by sorting the internal table in the sequence of its structure, that is, by the first field first, then by the second field, and so on. Tables in which the table key occurs at the start of the table are particularly suitable for control level processing.
The following diagram illustrates control level processing in a sorted table, where different field contents in the first three fields are indicated by different colors:
This graphic is explained in the accompanying text
Each change of color in a column indicates a control level change in the corresponding hierarchy level. Within the processing block of a loop, you can use the control level statement AT to react to a control level change. This enables you to restrict statements to a certain set of lines. You can thus use the SUMstatement to calculate totals from subsets of all lines.
The AT statement introduces a statement block that you end with the ENDAT statement.
AT level.
  statement block
ENDAT.
You can react to the following control level changes:
level
Meaning
FIRST
First line of the internal table
LAST
Last line of the internal table
NEW f
Beginning of a group of lines with the same contents in the field f and in the fields left of f
END Of f
End of a group of lines with the same contents in the field f and in the fields left of f
You can use control level statements to react to control breaks in internal tables instead of programming them yourself with logical expressions. Within the loop, you must order the AT-ENDATstatement blocks according to the hierarchy of the control levels. If the internal table itab has the columns f1, f2,.... and if it is sorted by these columns, you must program the loop as follows:
LOOP AT itab.
  AT FIRST.... ENDAT.
    AT NEW f1....... ENDAT.
      AT NEW f2....... ENDAT.
     .......
          single record processing
     .......
      AT END OF f2.... ENDAT.
    AT END OF f1.... ENDAT.
  AT LAST..... ENDAT.
ENDLOOP.
The innermost hierarchy level single line processing processes the table lines that do not correspond to a control level change. You do not have to use all control level statements. But you must place the ones that you use in the above sequence. You should not use control level statements in loops where the line selection is restricted by WHERE or FROM and TO. Neither should the table be modified during the loop.
If a control level field f1, f2,.... is not known until runtime, you can specify it dynamically as (n1), (n2), … where n1, n2,.... contains the field of f1, f2,.... . If n1, n2,....  is empty at runtime, the criterion for changing the control level is ignored. You can further restrict the group level fields  f1, f2,.... to partial fields by specifying offset and length.
If you are working with a work area wa, it does not contain the current line in the AT-ENDATstatement block. All character fields to the right of the current group key are filled with asterisks (*). All other fields to the right of the current group key contain their initial value.
Within an AT-ENDAT block, you can calculate the contents of the numeric fields of the corresponding control level using the SUMstatement.
.
You can only use this statement within a LOOP. If you use SUM in an AT-ENDAT block, the system calculates totals for the numeric fields of all lines in the current line group and writes them to the corresponding fields in the work area (see example in  ). If you use the SUM statement outside an AT-ENDAT block (single entry processing), the system calculates totals for the numeric fields of all lines of the internal table in each loop pass and writes them to the corresponding fields of the work area. It therefore only makes sense to use the SUM statement in AT-ENDAT blocks.
If the table contains a nested table, you cannot use the SUMstatement. Neither can you use it if you are using a field symbol instead of a work area in the LOOP statement.
Example
REPORT demo_int_tables_at_1.
DATA: BEGIN OF line,
         col1(1) TYPE c,
         col2 TYPE i,
         col3 TYPE i,
      END OF line.
DATA itab LIKE HASHED TABLE OF line
          WITH UNIQUE KEY col1 col2.
line-col1 = 'A'.
DO 3 TIMES.
  line-col2 = sy-index.
  line-col3 = sy-index ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
line-col1 = 'B'.
DO 3 TIMES.
  line-col2 = 2 * sy-index.
  line-col3 = ( 2 * sy-index ) ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
SORT itab.
LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2, line-col3.
  AT END OF col1.
    SUM.
    ULINE.
    WRITE: / line-col1, line-col2, line-col3.
    SKIP.
  ENDAT.
  AT LAST.
    SUM.
    ULINE.
    WRITE: / line-col1, line-col2, line-col3.
  ENDAT.
ENDLOOP.
The list output is:
A          1          1
A          2          4
A          3          9
________________________________
A          6         14

B          2          4
B          4         16
B          6         36
________________________________
B         12         56

________________________________
*         18         70
The program creates a hashed table itab, fills it with six lines, and sorts it. In the LOOP - ENDLOOP block, the work area LINE is output for each loop pass. The first field of the table key, col1, is used for control level processing. The total for all numeric fields is always calculated when the contents of col1 change and when the system is in the last loop pass.
Example REPORT demo_int_tables_at_2.
DATA: BEGIN OF line,
        carrid   TYPE sbook-carrid,
        connid   TYPE sbook-connid,
        fldate   TYPE sbook-fldate,
        custtype TYPE sbook-custtype,
        class    TYPE sbook-class,
        bookid   TYPE sbook-bookid,
      END OF line.
DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY table_line.
SELECT carrid connid fldate custtype class bookid
       FROM sbook INTO CORRESPONDING FIELDS OF TABLE itab.
LOOP AT itab INTO line.
  AT FIRST.
    WRITE / 'List of Bookings'.
    ULINE.
  ENDAT.
    AT NEW carrid.
      WRITE: / 'Carrid:', line-carrid.
    ENDAT.
      AT NEW connid.
        WRITE: / 'Connid:', line-connid.
      ENDAT.
        AT NEW fldate.
          WRITE: / 'Fldate:', line-fldate.
        ENDAT.
          AT NEW custtype.
            WRITE: / 'Custtype:', line-custtype.
          ENDAT.
               WRITE: / line-bookid, line-class.
            AT END OF class.
              ULINE.
            ENDAT.
ENDLOOP.
In this example, the sorted internal table itab is filled with data from the database table SBOOK using the Open SQL statement SELECT. The sequence of the columns in the internal table defines the control level hierarchy. Since the table key is the entire line, the sort sequence and the control level hierarchy are the same. The sequence of the AT-ENDAT blocks within the LOOP - ENDLOOP statements is important.
The output looks something like this:
List of Bookings
Carrid: AA
Connid: 0017
Fldate: 1998/11/22
Custtype: B
00063509 C
00063517 C
...
______________________________________________
00063532 F
00063535 F
...
______________________________________________
Custtype: P
00063653 C
00063654 C
...
______________________________________________
00063668 F
00063670 F
...
______________________________________________
Fldate: 1998/29/11
Custtype: B
00064120 C
00064121 C
...
and so on.
Specifying the Index in Loops
When you process an internal table in a loop, you can specify the index of an index table to restrict the number of entries that are read:
LOOP AT itab result [FROM n1] [TO n2] condition.
  statement block
ENDLOOP.
The loop is processed like any internal table. Within the processing block, the system field sy-tabix contains the index of the current line.
You can use the additions FROM and TO to specify the indexes n1 and n2 of the first and last entries that you want to read. The FROM and TO options restrict the number of lines which the system has to read. The WHERE addition in the condition only prevents the result from being processed. However, all of the table lines are read. To improve performance, you should use the FROM and TO options as much as possible. It can be also beneficial under certain conditions to leave the loop with the CONTINUE or EXIT statement.
Example
REPORT demo_int_tables_loop_ind.
DATA: BEGIN OF line,
         col1 TYPE i,
         col2 TYPE i,
      END OF line.
DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY table_line.
DO 30 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.
ENDDO.
LOOP AT itab INTO line FROM 10 TO 25 WHERE col2 > 400.
  WRITE: / sy-tabix, line-col2.
ENDLOOP.
The list output is:
        21       441
        22       484
        23       529
        24       576
        25       625
The example fills a sorted table itab with four lines. In the loop, only the lines 10 to 25 are read. There is also a condition that the contents of  col2 must be more than 400.

Changing Lines
To change a single line of any internal table, use the MODIFYstatement. You can either use the table key to find and change a single line using its key, or find and change a set of lines that meet a certain condition. If the table has a non-unique key and there are duplicate entries, the first entry is changed.
Changing a Line Using the Table Key
To change a single line, use the following statement:
MODIFY TABLE itab FROM wa [TRANSPORTING f1 f2 ...].
The work area wa, which must be compatible with the line type of the internal table, plays a double role in this statement. Not only it is used to find the line that you want to change, but it also contains the new contents. The system searches the internal table for the line whose table key corresponds to the key fields in wa.
The system searches for the relevant table types as follows:
·        Standard tables
Linear search, where the runtime is in linear relation to the number of table entries. The first entry found is changed.
·        Sorted tables
Binary search, where the runtime is in logarithmic relation to the number of table entries. The first entry found is changed.
·        Hashed tables
The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.
If a line is found, the contents of the non-key fields of the work area are copied into the corresponding fields of the line, and sy-subrc is set to 0. Otherwise, sy-subrc is set to 4. If the table has a non-unique key and the system finds duplicate entries, it changes the first entry.
You can specify the non-key fields that you want to assign to the table line in the TRANSPORTING addition. You can also specify the fields f1 f2 … dynamically using (n1) (n2) … as the contents of a field n1 n2 …. If <ni> is empty when the statement is executed, it is ignored. You can also restrict all fields f1 f2 … to subfields by specifying offset and length.
For tables with a complex line structure, the use of the TRANSPORTINGaddition results in better performance, provided the system does not have to transport unnecessary table-like components.
Changing Several Lines Using a Condition
To change one or more lines using a condition, use the following statement:
MODIFY itab FROM wa TRANSPORTING f1 f2 ... WHERE cond.
This processes all of the lines that meet the logical condition cond. The logical expression cond can consist of more than one comparison. In each comparison, the first operand must be a component of the line structure. If the table lines are not structured, the first operand can also be the expression TABLE_LINE. The comparison then applies to the entire line. If the line type of the internal table contains object reference variables as component comp or if the entire line type is a reference variable, the attributes of the attr object to which the respective line reference points can be specified as comparison values using comp->attr or table_line->attr.
The work area wa, which must be compatible with the line type of the internal table, contains the new contents, which in turn will be assigned to the relevant table line using the TRANSPORTING addition. Unlike the above MODIFYstatement, the TRANSPORTING addition is not optional here. Furthermore, you can only modify the key fields of the internal table if it is a standard table. If at least one line is changed, the system sets sy-subrc to 0, otherwise to 4.
Example
REPORT demo_int_tables_modify .
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY col1.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
line-col1 = 2.
line-col2 = 100.
MODIFY TABLE itab FROM line.
LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2.
ENDLOOP.
The list output is:
         1        1
         2      100
         3        9
         4       16
The program fills a hashed table with a list of square numbers. The MODIFY statement changes the line of the table in which the key field col1 has the value 2.
Example
REPORT demo_int_tables_modify_transp.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY col1.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
line-col2 = 100.
MODIFY itab FROM line TRANSPORTING col2
            WHERE ( col2 > 1 ) AND ( col1 < 4 ).
LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2.
ENDLOOP.
The list output is:
         1        1
         2      100
         3      100
         4       16
The program fills a hashed table with a list of square numbers. The MODIFY statement changes the lines of the table where the content of field col2 is greater than 1 and the content of field col1 is less than 4.
Changing Table Lines Using the Index
You can use the MODIFY statement to change lines in tables using their index. There is also an obsolete variant of the WRITE TOstatement that you can use to modify lines in standard tables.
Changing Single Lines using MODIFY
To change a line using its index, use the following statement:
MODIFY itab FROM wa [INDEX idx] [TRANSPORTING f1 f2... ].
The work area wa specified in the FROM addition replaces the addressed line in the itab table. The work area must be convertible into the line type of the internal table.
If you use the INDEX option, the contents of the work area overwrite the contents of the line with the index idx. If the operation is successful, sy-subrc is set to 0. If the internal table contains fewer lines than idx, no line is changed and sy-subrc is set to 4.
Without the INDEX addition, you can only use the above statement within a LOOP. In this case, you delete the current line. idx is implicitly set to sy-tabix .
When you change lines in sorted tables, remember that you must not change the contents of key fields, and that a runtime error will occur if you try to replace the contents of a key field with another value. However, you can assign the same value.
The TRANSPORTING addition allows you to specify the fields that you want to change explicitly in a list.  If you change a sorted table, you may only specify non-key fields.
Example
REPORT demo_int_tables_modify_ind.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE TABLE OF line.
DO 3 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.
ENDDO.
LOOP AT itab INTO line.
  IF sy-tabix = 2.
    line-col1 = sy-tabix * 10.
    line-col2 = ( sy-tabix * 10 ) ** 2.
    MODIFY itab FROM line.
  ENDIF.
ENDLOOP.
LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.
This produces the following output:
         1         1          1
         2        20        400
         3         3          9
Here, a sorted table itab is created and filled with three lines. The second line is replaced by the contents of the work area line.
Example
REPORT demo_int_tables_modify_ind_ind.
DATA name(4) TYPE c VALUE 'col2'.
DATA: BEGIN OF line,
         col1 TYPE i,
         col2 TYPE i,
      END OF line.
DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.
ENDDO.
line-col2 = 222.
MODIFY itab FROM line INDEX 2 TRANSPORTING (name).
line-col1 = 3.
line-col2 = 333.
MODIFY itab FROM line INDEX 3.
LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.
The list output is:
         1         1          1
         2         2        222
         3         3        333
         4         4         16
The example fills a sorted table with four lines. In the second and third lines, the component col2 is modified. If the third line were to be changed so that the value of line-col1 was no longer 3, a runtime error would occur, since the key fields of sorted tables may not be changed.

Deleting Lines
To delete a single line of any internal table, use the DELETEstatement. You can either use the table key to find and delete a single line using its key, delete a set of lines that meet a condition, or find and delete neighboring duplicate entries. If the table has a non-unique key and there are duplicate entries, the first entry is deleted.
Deleting a Line Using the Table Key
To use the table key of table itab as a search key, use one of the following statements:
DELETE TABLE itab FROM wa.
or
DELETE TABLE itab WITH TABLE KEY k1 = f1 ... kn = fn.
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. To delete tables with the elementary line type and the table_line key using the table keys, you can use either the first variant or the table_line pseudo component  as an operand in the second variant.
In the second case, you have to supply the values of each key field explicitly. 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 k1 k2 using (k1) = f1 (k2) = f2  If the data types of f1 … fn are not compatible with the key fields, the system converts them.
The system searches for the relevant table types as follows:
·        Standard tables
Linear search, where the runtime is in linear relation to the number of table entries.
·        Sorted tables
Binary search, where the runtime is in logarithmic relation to the number of table entries.
·        Hashed tables
The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.
If the system finds a line, it deletes it from the table and sets sy-subrc to zero. Otherwise, sy-subrc is set to 4. If the table has a non-unique key and the system finds duplicate entries, it deletes the first entry.
Deleting Several Lines Using a Condition
To delete more than one line using a condition, use the following statement:
DELETE itab WHERE cond.
This processes all of the lines that meet the logical expression cond. The logical expression cond can consist of more than one comparison. In each comparison, the first operand must be a component of the line structure. If the table lines are not structured, the first operand can also be the expression table_line. The comparison then applies to the entire line. If at least one line is deleted, the system sets sy-subrc to 0, otherwise to 4.
If the line type of the internal table contains object reference variables as component comp or if the entire line type is a reference variable, the attributes of the attr object to which the respective line reference points can be specified as comparison values using comp->attr or table_line->attr.
Deleting Adjacent Duplicate Entries
To delete adjacent duplicate entries use the following statement:
DELETE ADJACENT DUPLICATE ENTRIES FROM itab
                                 [COMPARING f1 f2 ... |ALL FIELDS].
The system deletes all adjacent duplicate entries from the internal table itab. Entries are duplicate if they fulfill one of the following compare criteria:
·        Without the COMPARINGaddition, the contents of the key fields of the table must be identical in both lines.
·        If you use the addition COMPARING f1 f2 ... the contents of the specified fields f1 f2 ... must be identical in both lines. You can also specify the fields f1 f2… dynamically using (n1) (n2) … as the contents of a field n1 n2. If n1 n2 is empty when the statement is executed, it is ignored. You can also restrict all fields f1 f2 … to subfields by specifying offset and length.
·        If you use the addition COMPARING ALL FIELDS the contents of all fields of both lines must be identical.
You can use this statement to delete all duplicate entries from an internal table if the table is sorted by the specified compare criterion.
If at least one line is deleted, the system sets sy-subrc to 0, otherwise to 4.
Example
REPORT demo_int_tables_DELETE_from .
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY col1.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
line-col1 = 1.
DELETE TABLE itab: FROM line,
                   WITH TABLE KEY col1 = 3.
LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2.
ENDLOOP.
The list output is:
         2        4
         4       16
The program fills a hashed table with a list of square numbers. The DELETE statements delete lines from the table where the key field col1 has the contents 1 or 3.
Example
REPORT demo_int_tables_DELETE_where.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY col1.
DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.
ENDDO.
DELETE itab WHERE ( col2 > 1 ) AND ( col1 < 4 ).
LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2.
ENDLOOP.
The list output is:
         1        1
         4       16
The program fills a hashed table with a list of square numbers. The DELETE statement deletes the lines of the table where the content of field col2 is greater than 1 and the content of field col1 is less than 4.
Example
REPORT demo_int_tables_DELETE_adjacen.
DATA off TYPE i.
DATA: BEGIN OF line,
        col1    TYPE i,
        col2(1) TYPE c,
      END OF line.
DATA itab LIKE STANDARD TABLE OF line
          WITH NON-UNIQUE KEY col2.
line-col1 = 1. line-col2 = 'A'. APPEND line TO itab.
line-col1 = 1. line-col2 = 'A'. APPEND line TO itab.
line-col1 = 1. line-col2 = 'B'. APPEND line TO itab.
line-col1 = 2. line-col2 = 'B'. APPEND line TO itab.
line-col1 = 3. line-col2 = 'B'. APPEND line TO itab.
line-col1 = 4. line-col2 = 'B'. APPEND line TO itab.
line-col1 = 5. line-col2 = 'A'. APPEND line TO itab.
off = 0. PERFORM list.
DELETE ADJACENT DUPLICATES FROM itab COMPARING ALL FIELDS.
off = 14. PERFORM list.
DELETE ADJACENT DUPLICATES FROM itab COMPARING col1.
off = 28. PERFORM list.
DELETE ADJACENT DUPLICATES FROM itab.
off = 42. PERFORM list.
FORM list.
  SKIP TO LINE 3.
  LOOP AT itab INTO line.
    WRITE: AT /off  line-col1, line-col2.
  ENDLOOP.
ENDFORM.
The list output is:
         1 A          1 A          1 A          1 A
         1 A          1 B          2 B          2 B
         1 B          2 B          3 B          5 A
         2 B          3 B          4 B
         3 B          4 B          5 A
         4 B          5 A
         5 A
The example creates and fills a standard table. Here, the first DELETE statement deletes the second line, as the second line has the same contents as the first line. The second DELETE statement deletes the second line from the remaining table because the contents of the field col1 is the same as in the first line. The third DELETE statement deletes the third and fourth line from the remaining table because the contents of the default key field col2 are the same as on the second line. Although the contents of the default key are the same for the first and the fifth line, the fifth line is not deleted because it is not adjacent to the first line.
Deleting Lines Using the Index
You can use the DELETE statement to delete lines in tables using their index. You can insert one or several lines into internal tables via the index:
Deleting a Single Line
To delete a line using its index, use the following statement:
DELETE itab [INDEX idx].
If you use the INDEX addition, the system deletes the line with the index idx from table itab, reduces the index of the subsequent lines by 1, and sets sy-subrc to zero. Otherwise, if no line with index idx exists, sy-bubrc is set to 4.
Without the INDEX addition, you can only use the above statement within a LOOP. In this case, you delete the current line. idx is implicitly set to sy-tabix .
Deleting Several Lines
To delete more than one line using the index, use the following statement:
DELETE [FROM n1] [TO n2] [WHERE condition].
Here, you must specify at least one of the additions. The WHERE addition work in the same way as any other tables. As well as the WHEREaddition, you can specify the lines that you want to delete by their index using FROM and TO. The system deletes all of the lines of itab whose index lies between n1 und n2. If you do not specify a FROM addition, the system deletes lines from the first line onwards. If you do not specify a TO addition, the system deletes lines up to the last line.
If at least one line is deleted, the system sets sy-subrc to 0, otherwise to 4.
Example
REPORT demo_int_tables_delete_ind_1.
DATA: BEGIN OF line,
         col1 TYPE i,
         col2 TYPE i,
      END OF line.
DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.
DO 5 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.
ENDDO.
DELETE itab INDEX: 2, 3, 4.
WRITE: 'sy-subrc =',sy-subrc.
SKIP.
LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.
The list output is:
sy-subrc     4
         1          1          1
         2          3          9
         3          5         25
The example fills a sorted table itab with five lines. Then it deletes the three lines with the indexes 2, 3, and 4. After deleting the line with index 2, the index of the following lines is decremented by one. Therefore, the next deletion removes the line with an index which was initially 4. The third delete operation fails, since the table now only has three lines.
Example
REPORT demo_int_tables_delete_ind_2.
DATA: BEGIN OF line,
         col1 TYPE i,
         col2 TYPE i,
      END OF line.
DATA itab LIKE TABLE OF line.
DO 30 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.
ENDDO.
LOOP AT itab INTO line.
  IF line-col1 < 28.
    DELETE itab.
  ENDIF.
ENDLOOP.
LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.
The list output is:
         1        28        784
         2        29        841
         3        30        900
A standard table ITAB is filled with 30 lines. Using a LOOP construction, the program deletes all of the lines in the table with a value of less than 28 in field col1.
Example
REPORT demo_int_tables_delete_ind_3.
DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.
DATA itab LIKE TABLE OF line.
DO 40 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.
ENDDO.
DELETE itab FROM 3 TO 38 WHERE col2 > 20.
LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2.
ENDLOOP.
The list output is:
         1          1
         2          4
         3          9
         4         16
        39      1.521
        40      1.600
The program deletes all entries from the standard table itab with an index between 3 and 39 where the value in col2 is greater than 20.
Searching Through and Replacing Table Rows
Using the statement:
REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern
  IN TABLE itab ... WITH new [IN {BYTE|CHARACTER} MODE] ...
the internal table itab is searched through, row by row, for the byte or character strings determined by pattern. The found location(s) is (are) replaced by the content of the new data object – depending on whether the first or all occurrences of the search pattern are searched for (optional addition {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF). For itab, a standard table has to be specified. The row type in the table must be either byte or character, depending on which of the additions BYTE and CHARACTER MODE has been selected. Byte or character strings that cover several table rows are not found.
For further details and additions to the statement, refer to the keyword documentation.
 Searching through Table Rows
Using the statement:
FIND [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern
  IN TABLE itab ... [IN {BYTE|CHARACTER} MODE] ...
the internal table itab is searched through row-by-row according to the byte or character string defined by pattern. For itab, a standard table has to be specified. The row type in the table must be either byte or character, depending on which of the additions BYTE and CHARACTER MODE has been selected. Byte or character strings that cover several table rows are not found. The optional addition {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF defines whether all or only the first occurrence of the search pattern is searched.
For further details and additions to the statement, see the keyword documentation.

Popular posts from this blog

How to create Interactive Report in SAP ABAP

How to create ALV Interactive Report in SAP ABAP

BDC Call Transaction Method Program