SAP MDG · UTILITIES
How do I compare the same table across two SAP systems in bulk with ABAP?
Use RFC_READ_TABLE to read the chosen table from the remote and local systems, rebuild both result sets into matching dynamic internal tables, and compare them with CTVB_COMPARE_TABLES_2. The report then summarizes totals, missing records, and changed records in ALV or optional file downloads.
Use RFC_READ_TABLE to read the chosen table from the remote and local systems, rebuild both result sets into matching dynamic internal tables, and compare them with CTVB_COMPARE_TABLES_2. The report then summarizes totals, missing records, and changed records in ALV or optional file downloads.
This ABAP pattern compares the same DDIC table across two systems in bulk. It reads the table from a remote system with RFC_READ_TABLE, reads the same table locally, converts both datasets into matching dynamic internal tables, and then compares them with CTVB_COMPARE_TABLES_2 before displaying the totals and differences in ALV or downloading them to files.
Process flow
- Select the remote SAP system.
- Select one or more table names.
- Read `DD02T` for table texts and `DD03L` for key-field counts.
- Call `RFC_READ_TABLE` with `DESTINATION` for the remote system.
- Call `RFC_READ_TABLE` again locally for the same table.
- Split the delimited rows and map them into a dynamic internal table.
- Build the key field list for comparison.
- Call `CTVB_COMPARE_TABLES_2` to get deleted, added, and modified rows.
Referenced tables
| Object | Purpose |
|---|---|
DD02T | Used to read the table description text (`DDTEXT`) for the selected table names. |
DD03L | Used to determine key-field counts for the selected table names via `KEYFLAG = 'X'`. |
ILLUSTRATIVE ABAP SAMPLE
Source ABAP example: Mass Table Comparison Between Two Systems.abap
Exact relevant implementation excerpt from the knowledge document set.
1*&---------------------------------------------------------------------*
2*& Report MASS_TABLE_COMPARISON_BETWEEN_SYSTEMS
3*&---------------------------------------------------------------------*
4*& Purpose:
5*& Generic, flattened example of a mass data comparison report.
6*& It compares the contents of the same table in two systems and
7*& summarizes the differences in an ALV grid.
8*&
9*& Main idea:
10*& - select a remote system and a table name
11*& - read the table from the remote system
12*& - read the same table from the local system
13*& - compare both datasets by key fields
14*& - display totals and differences
15*&
16*& This version removes project-specific naming and keeps the logic
17*& generic so it can be adapted to other landscapes.
18*&---------------------------------------------------------------------*
19
20REPORT zmdg_compare_data_mass_generic.
21
22TABLES: tbdls, dd02l.
23
24"---------------------------------------------------------------------
25" Selection screen: source system and table scope
26"---------------------------------------------------------------------
27SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
28 SELECT-OPTIONS: so_rfc FOR tbdls-logsys NO INTERVALS NO-EXTENSION OBLIGATORY,
29 so_tabs FOR dd02l-tabname NO INTERVALS OBLIGATORY.
30SELECTION-SCREEN END OF BLOCK b1.
31
32"---------------------------------------------------------------------
33" Selection screen: control flags for missing / diff output
34"---------------------------------------------------------------------
35SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME.
36 PARAMETERS: c_misrfc AS CHECKBOX DEFAULT 'X',
37 c_mislcl AS CHECKBOX DEFAULT 'X',
38 c_diff AS CHECKBOX DEFAULT 'X'.
39SELECTION-SCREEN END OF BLOCK b2.
40
41"---------------------------------------------------------------------
42" Selection screen: output mode and optional download folder
43"---------------------------------------------------------------------
44SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME.
45 PARAMETERS: r_file RADIOBUTTON GROUP rb1 USER-COMMAND cmd,
46 r_alv RADIOBUTTON GROUP rb1 DEFAULT 'X'.
47SELECTION-SCREEN END OF BLOCK b3.
48
49SELECTION-SCREEN BEGIN OF BLOCK b4 WITH FRAME.
50 PARAMETERS: p_file TYPE localfile MODIF ID gp1.
51SELECTION-SCREEN END OF BLOCK b4.
52
53"---------------------------------------------------------------------
54" Generic data containers used by the comparison process
55"---------------------------------------------------------------------
56DATA: lt_data_rfc TYPE TABLE OF /bods/tab2048,
57 lt_data_lcl TYPE TABLE OF /bods/tab2048,
58 lv_rowcount TYPE soid-accnt,
59 lv_rowskips TYPE soid-accnt,
60 lt_fields TYPE esh_t_co_rfcrt_fields,
61 lv_no_changes TYPE flag,
62 lt_options TYPE TABLE OF /bods/rfc_db_opt.
63
64DATA: lo_structdescr TYPE REF TO cl_abap_structdescr,
65 it_components TYPE abap_component_tab,
66 wa_component TYPE LINE OF abap_component_tab,
67 lo_datadescr TYPE REF TO cl_abap_datadescr,
68 lo_tabledescr TYPE REF TO cl_abap_tabledescr,
69 lr_workarea TYPE REF TO data,
70 lr_table_rfc TYPE REF TO data,
71 lr_table_lcl TYPE REF TO data,
72 lr_table_del TYPE REF TO data,
73 lr_table_ins TYPE REF TO data,
74 lr_table_mod TYPE REF TO data,
75 lv_string TYPE string,
76 lt_fieldnames TYPE TABLE OF string.
77
78CONSTANTS: c_separator TYPE so_text001 VALUE '|'.
79
80FIELD-SYMBOLS:
81 <it_mdg_rfc> TYPE STANDARD TABLE,
82 <it_mdg_lcl> TYPE STANDARD TABLE,
83 <it_mdg_add> TYPE STANDARD TABLE,
84 <it_mdg_mod> TYPE STANDARD TABLE,
85 <it_mdg_del> TYPE STANDARD TABLE.
86
87TYPES: BEGIN OF ty_output,
88 tabname TYPE dd03l-tabname,
89 descr TYPE char40,
90 totrfc TYPE char10,
91 totlcl TYPE char10,
92 extrarfc TYPE char10,
93 extralcl TYPE char10,
94 diff TYPE char10,
95 END OF ty_output.
96
97DATA: lt_output TYPE TABLE OF ty_output,
98 ls_output TYPE ty_output.
99
100"---------------------------------------------------------------------
101" Generic helper class for ALV display
102" This keeps the example self-contained while still showing the output.
103"---------------------------------------------------------------------
104CLASS lcl_alv_display DEFINITION.
105 PUBLIC SECTION.
106 METHODS: build_fieldcatalogue
107 RETURNING VALUE(rt_fieldcatalog) TYPE slis_t_fieldcat_alv,
108 display_alv
109 IMPORTING
110 it_fieldcatalog TYPE slis_t_fieldcat_alv
111 is_layout TYPE slis_layout_alv,
112 build_layout
113 RETURNING VALUE(rs_layout) TYPE slis_layout_alv.
114ENDCLASS.
115
116CLASS lcl_alv_display IMPLEMENTATION.
117 METHOD build_fieldcatalogue.
118 DATA: ls_fieldcat TYPE LINE OF slis_t_fieldcat_alv.
119
120 " Table name column.The remaining configuration, implementation details, and testing guidance continue from this answer more…
Related questions and keywords
Alternative questions
- How can I build a mass table comparison report for remote and local SAP systems?
- Which ABAP function modules are used to compare table contents between two systems?
- How do I read a table remotely and locally and show differences in ALV?
Possible questions
- What is the ABAP pattern for mass comparing tables across systems?
- How do I use RFC_READ_TABLE for a bulk table comparison report?
- How do I summarize added, deleted, and changed rows after comparing two tables?
- How can I download comparison results to files from an ABAP report?
- Which function module compares two internal tables by key fields?