This is a test message to test the length of the message box.

ABAP Quick - Compare tables


Imagine you are currently implementing an ALV with editing functions and would like to compare what has changed after editing by the user? No problem, we'll show you a quick and easy solution.

The scenario is relatively simple: You offer the user an ALV to edit his data. Here he can change data, record new records or maybe even delete them. If the user then click on the save button, the data must now be stored on the database and adapted.

Some rely on the method of logging each action of the user into separate tables and then simply processing them. But again there is an easier way to realize this.



For this we want to build together a small test scenario, which you can also use with your machine. To do this, you only need to adjust the listed company codes to match which ones in your system.

" Data selection
 WHERE bukrs IN ('A001','A002','A003','A004','A005')
 INTO TABLE @DATA(lt_old).

" Create the new table
DATA(lt_new) = lt_old.
DELETE lt_new INDEX 1.
DATA(lr_t001) = REF #( lt_new[ 1 ] ).
lr_t001->butxt = 'Some changes'.
lr_t001 = REF #( lt_new[ bukrs = 'A004' ] ).
lr_t001->ktopl = 'ABC'.


We read five entries from table T001 (company codes) that are hard coded in the select statement. Then we take the data in a new table, the old serves us later as a comparison of what has changed. Then we adapt the new table accordingly:

  • Delete the first entry in the table
  • Assign the new first entry and change the company code text
  • Read the entry A004 and change the chart of accounts



This allows the comparison to be carried out. In the internal table lt_old are the originally selected data, in the table lt_new are the changed data that the user has adjusted. For the comparison, we simply call the following function module:

  lt_del  TYPE STANDARD TABLE OF t001,
  lt_add  TYPE STANDARD TABLE OF t001,
  lt_mod  TYPE STANDARD TABLE OF t001,
  ld_flag TYPE abap_bool.

" Compare the two tables
    table_old  = lt_old
    table_new  = lt_new
    key_length = 14 
    table_del  = lt_del
    table_add  = lt_add
    table_mod  = lt_mod
    no_changes = ld_flag.

The comparison was made by the function module and we get back 3 internal tables, which we can now easily process. Thus, we have a clear representation of what has been adjusted, what data is new and what data has been deleted. The internal tables all have the same data type, so they can be used directly for customization.

Hint: The key_length field must be calculated beforehand and represents the key of the table. In this case, the length is 14 and is calculated from the fields MANDT and BUKRS (3 4), which are the key. Since we are in a Unicode system, the key must be taken times 2, since each character needs 2 digits space.



The comparison of two tables is easy with the right tool for you and should be no problem for you with our little tip.

Included topics:
QuickCompare tables
Comments (2)

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 Tools - Quick Actions und Highlighting (ABAP Unit)

Category - ABAP

With the last release of the ABAP Development Tools, Quick Actions were added for ABAP development in the ABAP Unit area. Here we take a closer look at them.


ABAP Quick - RFC Error handling

Category - ABAP

How do you actually handle errors when communicating via a destination in the function module? More about this in our tip.


ABAP Quick - CLEAR right

Category - ABAP

Delete correctly? In this article we want to take a look at when it makes sense to delete and how you can do it effectively.


ABAP Quick - Performance chained statements

Category - ABAP

Let's take a look at the performance when creating chained statements with DATA and FIELD-SYMBOL. Which variant will be ahead in terms of performance?


ABAP - ALV still relevant in 2022?

Category - ABAP

Today the joking question, do we still need reports that generate ALV outputs in 2022? In this article, we want to look into this question.