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

ABAP - Use INSERT

4011

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 (3)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP - Type Casting

Category - ABAP

How do you actually get the original type of a class or instance if it is passed in a generic table? In this article we examine the possibilities.

04/16/2024

ABAP - RETURN value

Category - ABAP

After all these years, the “real” return in ABAP has finally arrived. In this article we will show you how it works and what it can do.

02/13/2024

ABAP Deep Dive - FOR (Loops)

Category - ABAP

Let's take a closer look at the FOR loop. How does it work? What do I have to consider and what can I do with it?

04/14/2023

ABAP Deep Dive - Table access (internal)

Category - ABAP

In this article, let's take a look at table access to internal tables and how they replace READ TABLE.

02/03/2023

ABAP Developer still relevant

Category - ABAP

In this article we look at whether ChatGPT can already replace an ABAP developer or whether it can be used as a help in everyday life.

01/06/2023