Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

compare 2 internal tables

Former Member
0 Likes
357

Hi All,

I have yesterdays data frm BSEG table in itab1, & todays data frm BSEG table in itab2. i want to compare itab1 & itab2 & find which field of a record, changes r there. i want to compare all 300+ fields & find the fields that r changed & also the related record.

For ex. fields in itab1 & itab2 is say A B C D E F...

day1 - ITAB1

A B C D E F

1 2 3 4 5 6

2 3 5 9 4 1

day2 - ITAB2

A B C D E F

1 2 3 8 5 7

2 8 5 9 3 1

so in comparing tables itab1 & itab2

in row1 fields D & F is changed &

in row2 fields B & E is changed.

how do we find this if BSEG has thousands of records & 300+ fields.

Thanks All.

Hi All,

I have yesterdays data frm BSEG table in itab1, & todays data frm BSEG table in itab2. i want to compare itab1 & itab2 & find which field of a record, changes r there. i want to compare all 300+ fields & find the fields that r changed & also the related record.

For ex. fields in itab1 & itab2 is say A B C D E F...

day1 - ITAB1

A B C D E F

1 2 3 4 5 6

2 3 5 9 4 1

day2 - ITAB2

A B C D E F

1 2 3 8 5 7

2 8 5 9 3 1

so in comparing tables itab1 & itab2

in row1 fields D & F is changed &

in row2 fields B & E is changed.

how do we find this if BSEG has thousands of records & 300+ fields.

Thanks All.

1 REPLY 1
Read only

rainer_hbenthal
Active Contributor
0 Likes
293

Hi,

this will work indepent on how many fields a structure may have and how they are named.


REPORT  z_compare_two_tables.

DEFINE fill_tab_1.
  clear wa_tab.
  wa_tab-keyfield = &1.
  wa_tab-amount   = &2.
  wa_tab-value    = &3.

  wa_tab-flag     = 0.

  append wa_tab to itab1.
END-OF-DEFINITION.

DEFINE fill_tab_2.
  clear wa_tab.
  wa_tab-keyfield = &1.
  wa_tab-amount   = &2.
  wa_tab-value    = &3.

  wa_tab-flag     = 0.

  append wa_tab to itab2.
END-OF-DEFINITION.

TYPES:
  BEGIN OF t_inttab,
    keyfield(1)            TYPE c,
    amount                 TYPE i,
    value                  TYPE i,

    flag                   TYPE i,
  END OF t_inttab.

DATA:
  wa_tab                   TYPE t_inttab,
  itab1                    TYPE STANDARD TABLE OF t_inttab,
  itab2                    TYPE STANDARD TABLE OF t_inttab,

  rtty                     TYPE c LENGTH 1,
  ncom                     TYPE i,

  desc_table               TYPE REF TO cl_abap_tabledescr,
  desc_struc               TYPE REF TO cl_abap_structdescr,

  outstr(100)              TYPE c.


FIELD-SYMBOLS:
  <p>                      TYPE t_inttab,
  <pi>                     TYPE t_inttab,
  <po>                     TYPE t_inttab,

  <p_component>            TYPE abap_compdescr,

  <p_field_1>              TYPE ANY,
  <p_field_2>              TYPE ANY.

START-OF-SELECTION.

  fill_tab_1 'A' 1 2.
  fill_tab_1 'B' 2 3.
  fill_tab_1 'C' 3 5.
  fill_tab_1 'D' 4 9.
  fill_tab_1 'E' 5 4.
  fill_tab_1 'F' 6 1.
  fill_tab_1 'Z' 9 9.

  fill_tab_2 'A' 1 2.
  fill_tab_2 'B' 2 8.
  fill_tab_2 'C' 3 5.
  fill_tab_2 'D' 8 9.
  fill_tab_2 'E' 0 3.
  fill_tab_2 'F' 7 1.
  fill_tab_2 'X' 9 9.

  WRITE:/ 'Rows in table 1 not present in table 2'.

  LOOP AT itab1 ASSIGNING <po>.
    READ TABLE itab2 WITH KEY keyfield = <po>-keyfield ASSIGNING <pi>.
    IF sy-subrc = 0.
      <pi>-flag = 1.
      <po>-flag = 1.
    ELSE.
      WRITE:/ 'Key ', <po>-keyfield, 'is not present in table 2.'.
    ENDIF.
  ENDLOOP.

  WRITE:/ 'Rows in table 2 not present in table 1'.
  LOOP AT itab2 ASSIGNING <p> WHERE flag = 0.
    WRITE:/ 'Key ', <p>-keyfield, 'is not present in table 1.'.
  ENDLOOP.

  DESCRIBE FIELD wa_tab TYPE rtty COMPONENTS ncom.

  desc_table ?= cl_abap_tabledescr=>describe_by_data( itab1 ).
  desc_struc ?= desc_table->get_table_line_type( ).

  WRITE:/ 'Comparison on field level'.

  LOOP AT itab1 ASSIGNING <po> WHERE flag = 1.

    CLEAR outstr.

    READ TABLE itab2 WITH KEY keyfield = <po>-keyfield ASSIGNING <pi>.
    IF sy-subrc = 0.
      LOOP AT desc_struc->components ASSIGNING <p_component>.
        ASSIGN COMPONENT <p_component>-name OF STRUCTURE <po> TO <p_field_1>.
        ASSIGN COMPONENT <p_component>-name OF STRUCTURE <pi> TO <p_field_2>.

        IF <p_field_1> ne <p_field_2>.
          IF outstr IS INITIAL.
            outstr = <p_component>-name.
          ELSE.
            CONCATENATE outstr ',' <p_component>-name INTO outstr.
          ENDIF.
        ENDIF.
      ENDLOOP.
    ENDIF.

    IF outstr IS NOT INITIAL.
      WRITE:/ 'Row ', <po>-keyfield, 'differs in fields ', outstr, '.'.
    ENDIF.
  ENDLOOP.

and the output is


Rows in table 1 not present in table 2
Key  Z is not present in table 2.
Rows in table 2 not present in table 1
Key  X is not present in table 1.
Comparison on field level
Row  B differs in fields  VALUE
Row  D differs in fields  AMOUNT
Row  E differs in fields  AMOUNT,VALUE
Row  F differs in fields  AMOUNT

Note the filling of the table does not correspond to yout example, it is slightly different.