SAP MDG · UTILITIES
How do I build an SAP ABAP report to compare the same table across two systems and show differences in ALV?
Use a generic ABAP mass-compare pattern: read the chosen table from a remote system and the local system with RFC_READ_TABLE, build a dynamic structure from DDIC metadata, compare both internal tables with CTVB_COMPARE_TABLES_2, and display totals plus missing and changed records in ALV or download them to files.
Use a generic ABAP mass-compare pattern: read the chosen table from a remote system and the local system with RFC_READ_TABLE, build a dynamic structure from DDIC metadata, compare both internal tables with CTVB_COMPARE_TABLES_2, and display totals plus missing and changed records in ALV or download them to files.
This pattern is a generic mass comparison report for one DDIC table across two systems. It lets the user choose a remote logical system and a table name, reads the table twice with RFC_READ_TABLE, rebuilds both result sets into matching dynamic internal tables, and compares them by key fields using CTVB_COMPARE_TABLES_2. The result is summarized as remote total, local total, missing rows, and changed rows, with optional file download.
Process flow
- Create a selection screen for remote logical system, table name, output flags, and file path.
- Read DDIC metadata from DD02T and DD03L for the selected table.
- Call RFC_READ_TABLE remotely with DESTINATION and locally without DESTINATION.
- Create a dynamic structure and matching internal tables from DDIC field metadata.
- Parse the RFC-delimited strings into the dynamic internal tables.
- Call CTVB_COMPARE_TABLES_2 with the remote and local tables and the key field list.
- Summarize counts for remote total, local total, missing rows, and differences.
- Display the result in REUSE_ALV_GRID_DISPLAY or download the detailed sets with GUI_DOWNLOAD.
Referenced tables
| Object | Purpose |
|---|---|
DD02T | Read table descriptions for the selected table name and language. |
DD03L | Read key field counts and key-flagged fields for the selected table. |
TBDLS | Selection-screen domain source for logical systems in so_rfc. |
SLIS_T_FIELDCAT_ALV | ALV field catalogue type used to build the output grid. |
ILLUSTRATIVE ABAP SAMPLE
Source ABAP example
Exact relevant implementation excerpt from the knowledge document.
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 compare the contents of one DDIC table between a source system and the local system in ABAP?
- What is the generic ABAP pattern for a mass table comparison report across systems?
- How do I read a remote table and a local table dynamically and compare them in SAP?
Possible questions
- Which function module reads a table remotely in this pattern?
- Which function module compares the two internal tables?
- What are the output fields for remote total, local total, missing rows, and differences?
- How do I adapt the report for file download instead of ALV?
- Why does the report build the DDIC structure dynamically?