This is a test message to test the length of the message box.
Login
ABAP use INSERT
Created by Software-Heroes

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.

 

APPEND

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.

 

INSERT

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.

 

Sorted table

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.

 

LINE_INDEX

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

 

Conclusion

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.

 

Source:
ABAP Documentation - LINE_INDEX


Included topics:
New ABAPINSERTAPPENDObsoleteLINE_INDEX
Comments (0)

ABAP Deep Dive - VALUE

Category - ABAP

In this article we want to look at the value statement again in all its forms and how you can use it in your daily work.

11/11/2022

ABAP - Common Table Expression (CTE)

Category - ABAP

In this article we want to take a look at the general table expression WITH and how you can use it in everyday life.

10/28/2022

ABAP Deep Dive - CORRESPONDING

Category - ABAP

In this article, a little more about the new Corresponding Statement and how to use it in detail. Let's take a look at the additional features.

09/16/2022

ABAP - Step

Category - ABAP

Today a new addition for the loops and how you can use it. With Step you have the possibility to skip steps in a loop.

09/02/2022

ABAP - Text, String, Template

Category - ABAP

How do I use character strings in modern ABAP development and how do they behave together with inline declarations. We also look at errors that can happen with it.

01/14/2022