
ABAP Tools - Working with Eclipse (Table Comparison)
Comparing internal tables with the debugger in the ABAP Development Tools? With the latest feature, it's no longer a problem and can be used in many situations.
Table of contents
In this article, we'll look at the latest feature for the debugger in the ABAP Development Tools and how you can use it in your daily work.
Introduction
The ABAP Development Tools, or ADT for short, are considered the current standard in ABAP development. Therefore, in recent years, many functions have been transferred to the Eclipse plugin to make life easier for us as ABAP developers. The current release 2508 for the ABAP Environment and S/4HANA Public Cloud now includes a new view for comparing tables during debugging. In the next chapters, we'll test the new feature in various scenarios.
Getting Started
To call the comparison, we first need to enter the debugger. To do this, we need to set a breakpoint in the source code to start debugging. You can find out the easiest way to do this and how to use the various features in the debugger in these tutorials, which you can read or watch:
Start
First of all, we need to start the comparison in the debugger. There are currently several options available for this. You can find the different variants below in the link to the official documentation. Here they are summarized again with images.
Context menu
You can execute the first two options via the context menu in the variable view. To do this, bring the table or both tables directly into the view. Either by double-clicking while debugging or by writing the variable names into the view. If you then select both tables and call up the context menu, you can compare them directly.
The second process is two-step. To do this, select your first table and select the entry "Prepare Comparison / Set as First Table". The contents or current state of the table is then saved. If you want to compare it with the second table, select the second table, open the context menu, and choose "Compare with First Table" to start the comparison.
Internal Table
The third option available is the "ABAP Internal Table (Debugger)" view. Here, you'll find two buttons at the top that follow the principle of the two-step process. Note the first table and start the comparison with the second button.
Hint: If you change the sorting, fields, or order for the table in the view, these settings will also be included in the comparison.
View
The view is loaded as soon as the table comparison starts. A new instance of the view is created for each comparison, just like the different tabs for the source code editor. On the left side, you will find the first table and on the right side, the second table. You can find out how the individual colors work during the comparison in the documentation linked below. In this view, identical rows are hidden and only the differences are displayed.
In the top right corner, you'll find the menu with additional settings and actions. First, you'll find information about the differences found in the table. If you hover your mouse over the differences, you'll see the numbers again in detail. You can then use the navigation to jump through the various differences. In addition, various views and filters are available to you.
Test
In this chapter, we'll test the various functions and behavior, thus leaving the theoretical part.
Simple Comparison
In the first step, we'll perform a simple comparison. To do this, we'll create two tables of type String. In the second table, we'll omit the "two" and add the "four".
DATA(table_one) = VALUE string_table( ( `One` ) ( `Two` ) ( `Three` ) ).
DATA(table_two) = VALUE string_table( ( `One` ) ( `Three` ) ( `Four` ) ).
The comparison clearly shows which row in the table has been removed and which is new. This makes a simple comparison very easy.
Standard Table
Let's take a look at a standard table without a defined key. We change a relatively large amount of information between the two tables. Basically, some of the information in the cells is still correct.
DATA(table_one) = VALUE table_no_key( ( rkey = 'K1'
text = `Key One`
number = 10
amount = '12.65' )
( rkey = 'K2'
text = `Key Two`
number = 112
amount = '99.00' )
( rkey = 'K1'
text = `Key Three`
number = 19
amount = '87.06' ) ).
DATA(table_two) = VALUE table_no_key( ( rkey = 'K3'
text = `Key One`
number = 10
amount = '12.65' )
( rkey = 'K2'
text = `Key Two`
number = 112
amount = '99.00' )
( rkey = 'K4'
text = `Key Three`
number = 15
amount = '87.06' ) ).
The entire table is now compared, and the different rows are highlighted in gray. The changed cells in the table are displayed in dark gray.
In the second scenario, we sort the table in the "ABAP Internal Table (Debugger)" view in ascending order by the RKEY field and perform the same comparison again. The view changes, and the comparison is based on the sorted key. Changes at the cell level are no longer displayed, but the rows are interpreted as a whole.
Sorted Table
In the last scenario, we use a SORTED TABLE via the RKEY field and look at the behavior in detail again. Here, we have removed the key K3, reinserted K4, but with similar values to K3, and adjusted the AMOUNT for K1.
DATA(table_one) = VALUE table_sorted( ( rkey = 'K1'
text = `Key One`
number = 10
amount = '12.65' )
( rkey = 'K2'
text = `Key Two`
number = 112
amount = '99.00' )
( rkey = 'K3'
text = `Key Three`
number = 19
amount = '87.06' ) ).
DATA(table_two) = VALUE table_sorted( ( rkey = 'K1'
text = `Key One`
number = 10
amount = '12.66' )
( rkey = 'K2'
text = `Key Two`
number = 112
amount = '99.00' )
( rkey = 'K4'
text = `Key Four`
number = 19
amount = '87.06' ) ).
So what does the comparison of the two tables look like? The table is displayed as the sorted standard table. The deleted row and the new row are displayed, as well as the change in the first record. The key field appears to have a specific color again.
Finally, let's compare the two tables again and hide the AMOUNT field in the second table. The comparison is still performed, but we also receive a corresponding error message, and the column is displayed as a difference in the first table.
Session
Because the comparison is also available outside of the debugging session, you can also make comparisons across different sessions. To do this, you need the two-step process to remember the table in the first session. Then you start a new session and call the comparison for the second table. In principle, you can also work on two different systems and thus, for example, validate different settings. If you check the view above, you'll find the system and client, as well as the time the table snapshot was taken, in the header, along with the number of rows.
Complete example
Here you'll find the class we used above. You can use it to replicate our example on your system.
CLASS zcl_bs_demo_table_compare DEFINITION
PUBLIC FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PRIVATE SECTION.
TYPES: BEGIN OF structure,
rkey TYPE c LENGTH 15,
text TYPE string,
number TYPE i,
amount TYPE p LENGTH 10 DECIMALS 2,
END OF structure.
TYPES table_no_key TYPE STANDARD TABLE OF structure WITH EMPTY KEY.
TYPES table_sorted TYPE SORTED TABLE OF structure WITH UNIQUE KEY rkey.
METHODS compare_string_table
IMPORTING !out TYPE REF TO if_oo_adt_classrun_out.
METHODS compare_standard_table
IMPORTING !out TYPE REF TO if_oo_adt_classrun_out.
METHODS compare_sorted_table
IMPORTING !out TYPE REF TO if_oo_adt_classrun_out.
METHODS compare_different_session
IMPORTING !out TYPE REF TO if_oo_adt_classrun_out.
METHODS compare_method_changes
IMPORTING !out TYPE REF TO if_oo_adt_classrun_out.
METHODS change_table
IMPORTING !source TYPE string_table
RETURNING VALUE(result) TYPE string_table.
METHODS get_content_table
RETURNING VALUE(result) TYPE string_table.
ENDCLASS.
CLASS zcl_bs_demo_table_compare IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
compare_string_table( out ).
compare_standard_table( out ).
compare_sorted_table( out ).
compare_different_session( out ).
compare_method_changes( out ).
ENDMETHOD.
METHOD compare_string_table.
DATA(table_one) = VALUE string_table( ( `One` ) ( `Two` ) ( `Three` ) ).
DATA(table_two) = VALUE string_table( ( `One` ) ( `Three` ) ( `Four` ) ).
IF table_one = table_two.
out->write( table_one ).
out->write( table_two ).
ENDIF.
ENDMETHOD.
METHOD compare_standard_table.
DATA(table_one) = VALUE table_no_key( ( rkey = 'K1'
text = `Key One`
number = 10
amount = '12.65' )
( rkey = 'K2'
text = `Key Two`
number = 112
amount = '99.00' )
( rkey = 'K1'
text = `Key Three`
number = 19
amount = '87.06' ) ).
DATA(table_two) = VALUE table_no_key( ( rkey = 'K3'
text = `Key One`
number = 10
amount = '12.65' )
( rkey = 'K2'
text = `Key Two`
number = 112
amount = '99.00' )
( rkey = 'K4'
text = `Key Three`
number = 15
amount = '87.06' ) ).
IF table_one = table_two.
out->write( table_one ).
out->write( table_two ).
ENDIF.
ENDMETHOD.
METHOD compare_sorted_table.
DATA(table_one) = VALUE table_sorted( ( rkey = 'K1'
text = `Key One`
number = 10
amount = '12.65' )
( rkey = 'K2'
text = `Key Two`
number = 112
amount = '99.00' )
( rkey = 'K3'
text = `Key Three`
number = 19
amount = '87.06' ) ).
DATA(table_two) = VALUE table_sorted( ( rkey = 'K1'
text = `Key One`
number = 10
amount = '12.66' )
( rkey = 'K2'
text = `Key Two`
number = 112
amount = '99.00' )
( rkey = 'K4'
text = `Key Four`
number = 19
amount = '87.06' ) ).
IF table_one = table_two.
out->write( table_one ).
out->write( table_two ).
ENDIF.
ENDMETHOD.
METHOD compare_different_session.
DATA(changed) = change_table( get_content_table( ) ).
out->write( changed ).
ENDMETHOD.
METHOD compare_method_changes.
DATA(table) = get_content_table( ).
DATA(changed) = change_table( table ).
IF table = changed.
out->write( table ).
out->write( changed ).
ENDIF.
ENDMETHOD.
METHOD change_table.
result = source.
DATA(random) = cl_abap_random=>create( CONV i( cl_abap_context_info=>get_system_time( ) ) ).
DO random->intinrange( low = 0
high = 3 ) TIMES.
DATA(delete_index) = random->intinrange( low = 1
high = lines( result ) ).
DELETE result INDEX delete_index.
ENDDO.
DO random->intinrange( low = 0
high = 2 ) TIMES.
DATA(run_id) = sy-index.
DATA(change_index) = random->intinrange( low = 1
high = lines( result ) ).
result[ change_index ] = |Changed { run_id }|.
ENDDO.
ENDMETHOD.
METHOD get_content_table.
DO 26 TIMES.
INSERT substring( val = sy-abcde
off = sy-index - 1
len = 1 ) INTO TABLE result.
ENDDO.
ENDMETHOD.
ENDCLASS.
Conclusion
This new feature can support you in development, especially if you want to compare results across different sessions or simply see what has changed in an internal table. This eliminates the need to rely on external tools. As a bonus, comparing content across different systems is a nice bonus.
Source:
SAP Help - Getting Started with the Table Comparison Tool
SAP Help - Comparing Internal Tables