You are currently viewing How to show data of detail database table in SAP

How to show data of detail database table in SAP

In my previous articles we have seen some basic reports in SAP and some basic operations that we can perform on the SAP tables and reports. In this article we will see how can we show data of detail database table in SAP. Below is the Program that can be used to show data of detail database table in SAP.

REPORT  ZBILL_ALV_REPORT_1.

TABLES: ZBILL_DESC.

*TABLES ZBILL.
*---Calling the type pool SLIS to inherit all of its fields-----
TYPE-POOLS SLIS.

" ---- WORK-AREA AND INTERNAL TABLE DECLARATION ----
DATA: WA_BILLDESC TYPE ZBILL_DESC,
      IT_BILLDESC TYPE ZBILL_DESC OCCURS 0.
*      IT_BILLDESC TYPE ZBILL_DESC OCCURS 0 WITH HEADER LINE.

DATA: REP_ID TYPE SY-REPID VALUE SY-REPID.

DATA:
*-----DECLARING THE FIELD CATALOG WORK AREA & INTERNAL TABLE-----------*
      WA_FCAT   TYPE SLIS_FIELDCAT_ALV,
      IT_FCAT   TYPE SLIS_T_FIELDCAT_ALV,

*-----DECLARING THE WORK AREA OF ALV LAYOUT----------------------------*
      WA_LAYOUT TYPE SLIS_LAYOUT_ALV,

*-----DECLARING THE WORK AREA & INTERNAL TABLE FOR TOP OF PAGE---------*
      WA_TOP    TYPE SLIS_LISTHEADER,
      IT_TOP    TYPE SLIS_T_LISTHEADER.
 
SELECTION-SCREEN BEGIN OF BLOCK A WITH FRAME TITLE TEXT-001.
 PARAMETERS: P_DOC_ID TYPE ZBILL_DESC-DOC_ID OBLIGATORY.
 PARAMETERS: P_BILL_N TYPE ZBILL-BILNO OBLIGATORY.
SELECTION-SCREEN END OF BLOCK A.

START-OF-SELECTION. " for selection of data from database.
       PERFORM GET_BILLS.

*END-OF-SELECTION.
       PERFORM ALV_LAYOUT.
       PERFORM ALV_FIELD_CATALOG.
       PERFORM ALV_GRID_DISPLAY.

TOP-OF-PAGE.
       PERFORM TOP_OF_BILL.

*&---------------------------------------------------------------------*
*&      Form  GET_BILLS
*&---------------------------------------------------------------------*
*       Selection of BILL table data
*----------------------------------------------------------------------*
FORM GET_BILLS.
*  SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_BILL FROM ZBILL.
  SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_BILLDESC FROM ZBILL_DESC WHERE DOC_ID = P_DOC_ID.
    IF SY-SUBRC = 0.
      SORT IT_BILLDESC.
    ELSE.
      MESSAGE 'Bill Detail''s doesn''t exist' TYPE 'I'.
    ENDIF.
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  alv_field_catalog
*&---------------------------------------------------------------------*
*       Preparing ALV field catalog
*----------------------------------------------------------------------*
FORM ALV_FIELD_CATALOG.
  DATA LV_COL TYPE I VALUE 0.
  IF NOT IT_BILLDESC IS INITIAL.

    LV_COL            = 1 + LV_COL.
    WA_FCAT-COL_POS   = LV_COL.         "COLUMN POSITION
    WA_FCAT-FIELDNAME = 'PRO_ID'.       "TECHNICAL FIELD NAME
    WA_FCAT-TABNAME   = 'IT_BILLDESC'.     "OUTPUT TABLE NAME
    WA_FCAT-SELTEXT_L = 'Product Id'. "FIELD TEXT
    APPEND WA_FCAT TO IT_FCAT.          "PREPARING THE FIELDCAT TABLE
    CLEAR WA_FCAT.

    LV_COL            = 1 + LV_COL.
    WA_FCAT-COL_POS   = LV_COL.         "COLUMN POSITION
    WA_FCAT-FIELDNAME = 'PRO_NAM'.       "TECHNICAL FIELD NAME
    WA_FCAT-TABNAME   = 'IT_BILLDESC'.     "OUTPUT TABLE NAME
    WA_FCAT-SELTEXT_L = 'Name'. "FIELD TEXT
    APPEND WA_FCAT TO IT_FCAT.          "PREPARING THE FIELDCAT TABLE
    CLEAR WA_FCAT.

    LV_COL            = 1 + LV_COL.
    WA_FCAT-COL_POS   = LV_COL.         "COLUMN POSITION
    WA_FCAT-FIELDNAME = 'PRO_QTY'.       "TECHNICAL FIELD NAME
    WA_FCAT-TABNAME   = 'IT_BILLDESC'.     "OUTPUT TABLE NAME
    WA_FCAT-SELTEXT_L = 'Quantity'. "FIELD TEXT
    APPEND WA_FCAT TO IT_FCAT.          "PREPARING THE FIELDCAT TABLE
    CLEAR WA_FCAT.

        LV_COL            = 1 + LV_COL.
    WA_FCAT-COL_POS   = LV_COL.         "COLUMN POSITION
    WA_FCAT-FIELDNAME = 'PRO_AMT'.       "TECHNICAL FIELD NAME
    WA_FCAT-TABNAME   = 'IT_BILLDESC'.     "OUTPUT TABLE NAME
    WA_FCAT-SELTEXT_L = 'AMOUNT'. "FIELD TEXT
    WA_FCAT-DO_SUM    = 'X'.
    APPEND WA_FCAT TO IT_FCAT.          "PREPARING THE FIELDCAT TABLE
    CLEAR WA_FCAT.
  ENDIF.
ENDFORM.  " end alv_field_catalog

*&---------------------------------------------------------------------*
*&      Form  alv_layout
*&---------------------------------------------------------------------*
*       Preparing the ALV Layout
*----------------------------------------------------------------------*

FORM ALV_LAYOUT.
  WA_LAYOUT-ZEBRA = 'X'.             "CALLING THE ZEBRA LAYOUT
  WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
ENDFORM.

FORM TOP_OF_BILL.
    REFRESH IT_TOP.
    WA_TOP-TYP  = 'H'.            "HEADER TYPE
    WA_TOP-INFO = 'Technocrats Club'. "HEADER TEXT
    APPEND WA_TOP TO IT_TOP.
    CLEAR WA_TOP.

    WA_TOP-TYP  = 'S'.            "NORMAL LINE TYPE
    WA_TOP-KEY = 'Document ID:'.     "NORMAL LINE TEXT
    WA_TOP-INFO = P_DOC_ID.
    APPEND WA_TOP TO IT_TOP.
    CLEAR WA_TOP.

    WA_TOP-TYP  = 'S'.            "NORMAL LINE TYPE
    WA_TOP-KEY = 'Bill No.:'.     "NORMAL LINE TEXT
    WA_TOP-INFO = P_BILL_N.
    APPEND WA_TOP TO IT_TOP.
    CLEAR WA_TOP.

    WA_TOP-TYP  = 'S'.            "NORMAL LINE TYPE
    WA_TOP-KEY = 'Report:'.     "NORMAL LINE TEXT
    WA_TOP-INFO = SY-REPID. 
    APPEND WA_TOP TO IT_TOP.
    CLEAR WA_TOP.

    WA_TOP-TYP  = 'S'.
    WA_TOP-KEY = 'User:'.
    WA_TOP-INFO = SY-UNAME. 
    APPEND WA_TOP TO IT_TOP.
    CLEAR WA_TOP.

    DATA: DATUM TYPE C LENGTH 10.   " CONVERT DATE TO EXTERNAL.
    WRITE SY-DATUM TO DATUM.

    WA_TOP-TYP  = 'S'.
    WA_TOP-KEY = 'Date:'.
    WA_TOP-INFO = DATUM. 
    APPEND WA_TOP TO IT_TOP.
    CLEAR WA_TOP.


   DATA : S_TIME TYPE C LENGTH 20.   "TIME CONVERSION TO EXTERNAL
   WRITE SY-UZEIT TO S_TIME.
    WA_TOP-TYP  = 'S'.
    WA_TOP-KEY = 'Time:'.
    WA_TOP-INFO = S_TIME. 
    APPEND WA_TOP TO IT_TOP.
    CLEAR WA_TOP.

    "-CALLING FUNCTION MODULE FOR DISPLAYING TOP OF PAGE-------------------*
    CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      IT_LIST_COMMENTARY       = IT_TOP "PASSING THE INTERNAL TABLE
      I_LOGO                   =  'ENJOYSAP_LOGO'
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =
            .
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  alv_grid_display
*&---------------------------------------------------------------------*
*       Preparing the final output by using Grid Display
*----------------------------------------------------------------------*
FORM ALV_GRID_DISPLAY.
    IF NOT IT_BILLDESC IS INITIAL AND NOT IT_FCAT IS INITIAL.
     CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
       I_CALLBACK_PROGRAM        = SY-REPID
*      I_CALLBACK_USER_COMMAND   = 'USER_COMMAND'
       I_CALLBACK_TOP_OF_PAGE    = 'TOP_OF_BILL'
       IS_LAYOUT                 = WA_LAYOUT
       IT_FIELDCAT               = IT_FCAT
       I_GRID_TITLE              = 'Bill Details'
     TABLES
       T_OUTTAB               = IT_BILLDESC.
       IF SY-SUBRC <> 0.
* Implement suitable error handling here
       ENDIF.
    ENDIF.
ENDFORM.

 

Output:

 

Dinesh Kumar Bansal

Dinesh Kumar Bansal is an Indian Software Developer, who is working on ASP.Net, MS-SQL, SAP-ABAP technologies from last one year. His Basic Principle of developing software is, “You have to clear about, what do you want to do, how can it be done”. He always says that development can be done with a cool & fresh mind.

Leave a Reply