Skip to Content
0
Oct 27, 2017 at 09:56 PM

Internal tables - UNION in ABAP

1040 Views

Hi,

We are on 740 SP12. The requirement is to generate a report to compare between SAP data and Legacy system data.

Searching did not yield any results. Answers kept pointing to SQL SELECT statements.

There could be a mismatch in number of records between the two systems, for a given order.

I have a solution using intermediate tables, but am wondering if there is a better way to do it without intermediate tables using the new ABAP syntax. For example, using FOR, REDUCE etc.

This is what I have that is giving me the correct results. Looking for a cleaner approach.

Any help is much appreciated.

REPORT ztest_itab_union.
TYPES:
  BEGIN OF lty_leg_bol,
    order TYPE vbeln,
    bolnr TYPE vbeln,
  END OF lty_leg_bol,
  lty_t_leg_bol TYPE STANDARD TABLE OF lty_leg_bol WITH EMPTY KEY,
  BEGIN OF lty_sap_bol,
    order TYPE vbeln,
    vbeln TYPE vbeln,
  END OF lty_sap_bol,
  lty_t_sap_bol TYPE STANDARD TABLE OF lty_sap_bol WITH EMPTY KEY,
  BEGIN OF lty_report,
    order TYPE vbeln,
    bolnr TYPE vbeln,
    vbeln TYPE vbeln,
  END OF lty_report,
  lty_t_report TYPE STANDARD TABLE OF lty_report WITH EMPTY KEY.

DATA:
  lt_report  TYPE lty_t_report.

START-OF-SELECTION.
  DATA(lt_leg_bol) = VALUE lty_t_leg_bol(
                       ( order = 'ORD001' bolnr = 'LEG101' )
                       ( order = 'ORD001' bolnr = 'LEG102' )
                       ( order = 'ORD002' bolnr = 'LEG201' )
                       ( order = 'ORD002' bolnr = 'LEG202' )
                       ( order = 'ORD002' bolnr = 'LEG203' ) ).

  DATA(lt_sap_bol) = VALUE lty_t_sap_bol(
                       ( order = 'ORD001' vbeln = 'SAP101' )
                       ( order = 'ORD001' vbeln = 'SAP102' )
                       ( order = 'ORD001' vbeln = 'SAP103' )
                       ( order = 'ORD002' vbeln = 'SAP201' )
                       ( order = 'ORD002' vbeln = 'SAP202' )
                     ).

  LOOP AT lt_leg_bol ASSIGNING FIELD-SYMBOL(<lfs_leg_bol>).

    AT END OF order.
      DATA(lt_leg_temp) = VALUE lty_t_leg_bol(
        FOR wa_leg IN lt_leg_bol WHERE ( order = <lfs_leg_bol>-order ) ( wa_leg ) ).

      DATA(lt_sap_temp) = VALUE lty_t_sap_bol(
        FOR wa_sap IN lt_sap_bol WHERE ( order = <lfs_leg_bol>-order ) ( wa_sap ) ).

      IF lines( lt_leg_temp ) GT lines( lt_sap_temp ).

        DO lines( lt_leg_temp ) - lines( lt_sap_temp ) TIMES.
          APPEND INITIAL LINE TO lt_sap_temp ASSIGNING FIELD-SYMBOL(<lfs_sap_temp>).
          <lfs_sap_temp>-order = <lfs_leg_bol>-order.
        ENDDO.

      ELSE.

        DO lines( lt_sap_temp ) - lines( lt_leg_temp ) TIMES.
          APPEND INITIAL LINE TO lt_leg_temp ASSIGNING FIELD-SYMBOL(<lfs_leg_temp>).
          <lfs_leg_temp>-order = <lfs_leg_bol>-order.
        ENDDO.

      ENDIF.

      DATA(lt_report_temp) = VALUE lty_t_report(
          FOR wa1 IN lt_leg_temp INDEX INTO idx
          ( order = wa1-order
            bolnr = wa1-bolnr
            vbeln = lt_sap_temp[ idx ]-vbeln )
        ).

      APPEND LINES OF lt_report_temp TO lt_report.
      REFRESH lt_report_temp.

    ENDAT.

  ENDLOOP.

  DATA(out) = cl_demo_output=>new( ).

  out->write( lt_report ).

  out->display( ).

and the correct result:

Thank you.

With best regards.

Attachments