ABAP - Use INSERT
APPEND already has a long history in ABAP, but this will play less and less of a role in the future. We'll show you why.
With Clean ABAP and the new statements, the ABAP language has developed significantly in recent years, so that many statements have lost their meaning or have become obsolete. In today's article we take a closer look at INSERT and why we actually no longer need the APPEND and can slowly forget it.
The APPEND appends a row to a table, as the name suggests. This only works for the standard table type and not for sorted tables (sorted and hash). This severely limits the functionality of the statement and, in the worst case, leads to the termination of your application if the type of the table should change.
As a result, this ABAP statement has a very limited scope of use and can lead to errors when called. Since tables with sorting and keys are more often used nowadays due to performance, this command takes a back seat.
The Insert statement can correctly insert a line in all types of tables and behaves according to the type of table. In sorted tables (Sorted, Hash) the data record is inserted in the right place according to the key. In standard tables, the data record is appended at the end. This avoids problems if the type of the table should change. Such passages are not always noticeable when refactoring the source code and can quickly lead to crashes that you could have avoided from the start.
The following is an example of the comparison of the two commands, one using a structure and the second using the new VALUE command. Both commands behave the same, but are written out differently.
DATA: ls_tool TYPE zbs_dy_tools, lt_tool TYPE STANDARD TABLE OF zbs_dy_tools WITH EMPTY KEY. ls_tool-short_name = 'WRENCH'. ls_tool-stock_quantity = 10. APPEND ls_tool TO lt_tool. INSERT ls_tool INTO TABLE lt_tool. APPEND VALUE #( short_name = 'WRENCH' stock_quantity = 10 ) TO lt_tool. INSERT VALUE #( short_name = 'WRENCH' stock_quantity = 10 ) INTO TABLE lt_tool.
How does it all work with sorted tables? With Append you can no longer work here, at least until you attach a data record that violates the sorting order. This property poses a high risk as this error may not be noticed during the test.
Here is an example of handling sorted tables:
DATA: ls_tool TYPE zbs_dy_tools, lt_tool TYPE SORTED TABLE OF zbs_dy_tools WITH UNIQUE KEY short_name. ls_tool-short_name = 'WRENCH'. ls_tool-stock_quantity = 10. INSERT ls_tool INTO TABLE lt_tool. INSERT VALUE #( short_name = 'WRENCH' stock_quantity = 10 ) INTO TABLE lt_tool.
Some developers like to insert an empty record into a table and then continue working with the field symbol or the reference. In the case of sorted tables, however, at least the key must be filled so that the behavior works. You can find more about this in the article "Work with references". A quick example of this behavior:
DATA: lt_tool TYPE SORTED TABLE OF zbs_dy_tools WITH UNIQUE KEY short_name. INSERT VALUE #( short_name = 'WRENCH' ) INTO TABLE lt_tool ASSIGNING FIELD-SYMBOL(<ls_tool>). <ls_tool>-stock_quantity = 12. INSERT VALUE #( short_name = 'HAMMER' ) INTO TABLE lt_tool REFERENCE INTO DATA(lr_tool). lr_tool->stock_quantity = 15.
Have you inserted a series of data records into a table and would like to know the TABIX for a certain key? For this purpose there is the LINE_INDEX command in ABAP, which searches for the key and returns the corresponding line number. If the key is not found, the value 0 (zero) is returned. Here is a small example:
DATA: lt_tool TYPE SORTED TABLE OF zbs_dy_tools WITH UNIQUE KEY short_name. INSERT VALUE #( short_name = 'WRENCH' stock_quantity = 10 ) INTO TABLE lt_tool. INSERT VALUE #( short_name = 'SCREW' stock_quantity = 10 ) INTO TABLE lt_tool. INSERT VALUE #( short_name = 'HAMMER' stock_quantity = 10 ) INTO TABLE lt_tool. DATA(ld_tabix) = line_index( lt_tool[ short_name = 'HAMMER' ] ).
The value of the variable ld_tabix will accordingly be 1, since the data record was inserted at the first position in the table (following the sorting).
You should slowly delete the ABAP command Append from your memory and fully set it to Insert, in order to have fewer problems in the future. It is just as easy to use as Append and saves you a little trouble in your next program or class.
ABAP Documentation - LINE_INDEX