banner ads

Object Oriented Classic for Multiple Tables

Object Oriented Classic for Multiple Tables


We can have data from multiple tables. In object oriented approach those multiple internal tables have to be structured in the class definition part. In the implementation we can use multiple methods to select each database table. Here we can use "for all entries in" concept also. We are preparing the output table and output format in different methods. After that in start of selection event we can call them as well.

Here is a program by which we can fetch the data from MARA table based on the Material Number selected in the Selection Screen. Then we can find out the Plant and Storage Location details from MARC and MARD table based on the material in the selection screen. These MARC and MARD table data will be selected FOR ALL ENTRIES IN selected MARA table.

REPORT  zsr_test.

*-------Declaration of Table for Select Option-------------------------*
TABLES: mara.

*----Event Initialization----------------------------------------------*
INITIALIZATION.

*----Selection screen with B1 block------------------------------------*
  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

  "Mandatory selection range
  SELECT-OPTIONS  s_matnr FOR mara-matnr OBLIGATORY.
  SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*       Class Definition
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.

  PUBLIC SECTION"Public section property

    "Internal table structure
    TYPESBEGIN OF ty_mara,
            matnr TYPE mara-matnr,
            ernam TYPE mara-ernam,
            mtart TYPE mara-mtart,
           END OF ty_mara,

           BEGIN OF ty_marc,
             matnr TYPE marc-matnr,
             werks TYPE marc-werks,
           END OF ty_marc,

           BEGIN OF ty_mard,
             matnr TYPE mard-matnr,
             werks TYPE mard-werks,
             lgort TYPE mard-lgort,
           END OF ty_mard,

           BEGIN OF ty_out1,
             matnr TYPE mara-matnr,
             ernam TYPE mara-ernam,
             mtart TYPE mara-mtart,
             werks TYPE mard-werks,
             lgort TYPE mard-lgort,
           END OF ty_out1.

    "Declaration of work area & internal table
    DATA: wa_mara TYPE ty_mara,
          it_mara TYPE STANDARD TABLE OF ty_mara,
          wa_marc TYPE ty_marc,
          it_marc TYPE STANDARD TABLE OF ty_marc,
          wa_mard TYPE ty_mard,
          it_mard TYPE STANDARD TABLE OF ty_mard,
          wa_out1 TYPE ty_out1,
          it_out1 TYPE STANDARD TABLE OF ty_out1.

    "Declaration of methods
    METHODS: m_mara, m_marc, m_mard,
             m_out1, m_display1.

  PROTECTED SECTION"Protected section property
  PRIVATE SECTION.   "Private section property

ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*       Class Implementation
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.

  "Implementing m_mara
  METHOD m_mara.

    SELECT matnr ernam mtart
      FROM mara INTO TABLE it_mara
      WHERE matnr IN s_matnr.

    IF sy-subrc = 0.
      SORT it_mara BY matnr.
    ELSE.
      MESSAGE 'No data found' TYPE 'I'.
    ENDIF.

  ENDMETHOD.                    "m_mara

  "Implementing m_marc
  METHOD m_marc.

    "Prerequisite of For all entries
    IF it_mara IS NOT INITIAL.
      SELECT matnr werks FROM marc
        INTO TABLE it_marc
        FOR ALL ENTRIES IN it_mara
        WHERE matnr = it_mara-matnr.

      IF sy-subrc = 0.
        SORT it_marc BY matnr.
      ENDIF.
    ENDIF.

  ENDMETHOD.                    "m_marc

  "Implementing m_mard
  METHOD m_mard.

    "Prerequisite of For all entries
    IF it_mara IS NOT INITIAL.
      SELECT matnr werks lgort
        FROM mard INTO TABLE it_mard
        FOR ALL ENTRIES IN it_mara
        WHERE matnr = it_mara-matnr.

      IF sy-subrc = 0.
        SORT it_mard BY matnr.
      ENDIF.
    ENDIF.

  ENDMETHOD.                    "m_mard

  "Implementing m_out1 - Output table
  METHOD m_out1.

    IF it_marc IS NOT INITIAL.

      "Looping at plant table
      LOOP AT it_marc INTO wa_marc.
        wa_out1-werks = wa_marc-werks.

        "Looping at storage table - loop inside loop with where clause
        LOOP AT it_mard INTO wa_mard
          WHERE matnr = wa_marc-matnr.
          wa_out1-lgort = wa_mard-lgort.

          "Reading the master table since it has unique material
          READ TABLE it_mara INTO wa_mara
          WITH KEY matnr = wa_mard-matnr
          BINARY SEARCH.

          IF sy-subrc = 0.
            wa_out1-matnr = wa_mara-matnr.
            wa_out1-ernam = wa_mara-ernam.
            wa_out1-mtart = wa_mara-mtart.

            "Appending the output table
            APPEND wa_out1 TO it_out1.
            CLEAR: wa_out1, wa_mara, wa_marc, wa_mard.
          ENDIF.
        ENDLOOP.
      ENDLOOP.
    ENDIF.

  ENDMETHOD.                                                "m_out1

  "Implementing m_display1
  METHOD m_display1.

    IF it_out1 IS NOT INITIAL.
      LOOP AT it_out1 INTO wa_out1.
        AT FIRST.  "Control break statement
          WRITE: /3 'MATERIAL',
                 21 'NAME',
                 36 'TYPE',
                 48 'PLANT',
                 56 'STORAGE LOCATION'.
          ULINE.
          SKIP.
        ENDAT.
        WRITE: /3 wa_out1-matnr,
               21 wa_out1-ernam,
               36 wa_out1-mtart,
               48 wa_out1-werks,
               56 wa_out1-lgort.
      ENDLOOP.
      CLEAR wa_out1.
    ENDIF.

  ENDMETHOD.                                                "m_display1

ENDCLASS.                    "cls1 IMPLEMENTATION

*-----Event Start of Selection-----------------------------------------*
START-OF-SELECTION.
  "Declaring the object / instance referenced to class
  DATA: obj1 TYPE REF TO cls1.

  "Creating the object
  CREATE OBJECT: obj1.

  "Calling the methods by object
  CALL METHOD: obj1->m_mara,
               obj1->m_marc,
               obj1->m_mard,
               obj1->m_out1,
               obj1->m_display1.

The Selection Screen:



The Output is:


No comments