Classic Report of Multiple Tables
Classic Report of Multiple Tables
We have created a program which contains multiple tables like MARA, MARC and MARD. The materials
contain different Plant and Storage Location in MARC and MARD tables respectively. All those different
plant and storage location will be displayed with material number in output here.
In the program we have prepared internal table it_mara from MARA table based on the Select Option
material number range. If material number is entered then only the program will give an output.
After preparing valid information of it_mara the program selects data from MARC table into it_marc for
all entries in it_mara. Here we have to check the it_mara table if it is not initial. If we don't give this
checking then all records will be selected from MARC table and that would be wrong. Hence we can say
that this table checking is one of a prerequisites of For All Entries In statement.
Similarly after preparing the it_marc table we shall prepare the it_mard table from MARD for all entries
in it_marc. Similarly the table checking of it_marc must be there.
Now after getting all these plant and storage location information we shall loop into it_mara to make
the output table since MARA is a master table. Now one material can be stored different plants and
storage locations. So to populate output table we have to loop into it_marc and it_mard table. Here we
will loop into it_marc and it_mard with WHERE clause because we are going to fetch all records of plant
and storage location at one time. So where clause will help us to point out the particular material and
also will increase the performance.
Finally at the output population we have used control break statement like AT FIRST, AT END OF, AT
LAST. With the help of that we have synchronized the output in different lines.
ELSE.
MESSAGE 'Material doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.
ENDFORM.
" GET_MARA
*&---------------------------------------------------------------------*
*& Form GET_MARC
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_marc .
IF it_mara IS NOT INITIAL.
SELECT matnr werks xchar
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.
ELSE.
MESSAGE 'Plant doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.
ENDFORM.
" GET_MARC
*&---------------------------------------------------------------------*
*& Form GET_MARD
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_mard .
IF it_marc IS NOT INITIAL.
SELECT matnr werks lgort pstat
ENDLOOP.
ENDLOOP.
ENDIF.
ENDFORM.
" GET_OUTPUT
*&---------------------------------------------------------------------*
*& Form DISPLAY
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM display .
IF it_out IS NOT INITIAL.
LOOP AT it_out INTO wa_out.
AT FIRST.
WRITE: / c_material,
21 c_plant,
27 c_storage,
37 c_type,
45 c_batch,
54 c_maint.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_out-matnr,
21 wa_out-werks,
27 wa_out-lgort,
37 wa_out-mtart,
45 wa_out-xchar,
54 wa_out-pstat.
IF wa_out-matnr IS INITIAL.
AT END OF matnr.
SKIP.
ENDAT.
ENDIF.
AT LAST.
ULINE.
WRITE: / c_end.
ENDAT.
ENDLOOP.
ENDIF.
ENDFORM.
" DISPLAY
*&---------------------------------------------------------------------*
*& Form TOP_OF_PAGE
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM top_of_page .
WRITE: / v_prog,
/ v_date DD/MM/YYYY,
/ v_time.
ULINE.
ENDFORM.
Output:
1. Selection Screen -
2. Classical Display -
" TOP_OF_PAGE
We have a requirement where we have to prepare a primary output containing Purchase Order,
Company Code, Category, Type and Vendor with PO Info. The secondary list will be generated with PO
Item, Material, Plant, Storage Location, Material Group, Quantity and Unit of measure. The secondary
list will be generated if we double click on PO field in primary output.
We have mentioned the tables EKKO, EKPO and T161T. Now we have declared the structure types of
internal tables of it_ekko, it_text, it_out1 and it_ekpo. Here it_out1 contains the combination of two
internal tables it_ekko and it_text and it_ekpo is for the secondary output. For internal operation we
have declared work area of every internal tables.
Next we have mentioned the event INITIALIZATION. Under this we are calling program name, user name
and system date of the program. We have declared here selection screen begin with block b1 and under
this we are mentioning obligatory select option.
Now under START-OF-SELECTION we are declaring all the operations for primary listing. We have made
4 subroutines. In get_ekko we select fields from EKKO into internal table it_ekko. Then in get_t161t we
select fields from t161t into internal table it_text for all entries in it_ekko. After that we are preparing
the primary output in the basic_output subroutine. Now we have made another subroutine for
displaying the primary output and that is disp_basic.
Hence the primary list is prepared now and now we have to create the secondary list. To make the
secondary list we are calling event AT LINE-SELECTION which raises functionality when user double clicks
in any field. Now we are mentioning GET CURSOR for that field and for that value of the field. When
these two are matches with Purchase Order (EBELN) field then we are calling two subroutines for
secondary list.
Subroutine get_ekpo Selects fields from EKPO into table it_ekpo for all entries in it_ekko. Then it
prepares the secondary output display in the subroutine of ekpo_output.
We also have raised events TOP-OF-PAGE which shows the header list of primary output and TOP-OFPAGE DURING LINE-SELECTION which shows the top list of secondary output. Here TOP-OF-PAGE
DURING LINE-SELECTION is used only for interactive reports. That means when user double clicks on the
field the TOP-OF-PAGE DURING LINE-SELECTION event triggers to make the secondary output header.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
TOP-OF-PAGE.
PERFORM top_page1.
TOP-OF-PAGE DURING LINE-SELECTION.
PERFORM top_page2.
*&---------------------------------------------------------------------*
*& Form get_ekko
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_ekko .
SELECT ebeln bukrs bstyp bsart lifnr
FROM ekko INTO TABLE it_ekko
WHERE ebeln IN s_ebeln.
IF sy-subrc = 0.
SORT it_ekko BY ebeln.
ELSE.
MESSAGE 'Purchase Order doesn''t exist.' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
" get_ekko
*&---------------------------------------------------------------------*
*& Form get_t161t
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_t161t .
IF it_ekko IS NOT INITIAL.
SELECT spras bsart bstyp batxt
FROM t161t INTO TABLE it_text
FOR ALL ENTRIES IN it_ekko
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_ekpo .
DATA: lv_ebeln TYPE ekko-ebeln.
IF v_value1 IS NOT INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = v_value1
IMPORTING
output = lv_ebeln.
IF lv_ebeln IS NOT INITIAL.
SELECT ebeln ebelp matnr werks lgort
matkl menge meins
FROM ekpo INTO TABLE it_ekpo
WHERE ebeln = lv_ebeln.
IF sy-subrc <> 0.
MESSAGE 'PO Item doesn''t Exist.' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
ENDIF.
ENDFORM.
" get_ekpo
*&---------------------------------------------------------------------*
*& Form ekpo_output
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM ekpo_output .
IF it_ekpo IS NOT INITIAL.
LOOP AT it_ekpo INTO wa_ekpo.
AT FIRST.
WRITE: / 'Purchase Order',
20 'PO Item',
30 'Material',
48 'Plant',
55 'Storage',
65 'Material Group',
83 'PO Quantity',
100 'Unit'.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_ekpo-ebeln,
20 wa_ekpo-ebelp,
30 wa_ekpo-matnr,
48 wa_ekpo-werks,
55 wa_ekpo-lgort,
70 wa_ekpo-matkl,
75 wa_ekpo-menge,
100 wa_ekpo-meins.
AT LAST.
SKIP.
ULINE.
WRITE: /12 '~~End of PO Item~~'.
ENDAT.
ENDLOOP.
ENDIF.
ENDFORM.
" ekpo_output
*&---------------------------------------------------------------------*
*& Form get_mara
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_mara .
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM top_page2 .
WRITE: / 'Purchase Order Item List',
/ 'Date: ', 12 v_date DD/MM/YYYY,
/ 'User: ', 12 v_user,
/ 'Report: ', 12 v_repid.
ULINE.
SKIP.
ENDFORM.
Below is the output:
Primary Listing:
" top_page2
WHEN '&IC1'.
IF rs_selfield_ekko-fieldname = 'EBELN'.
CLEAR v_selfield.
v_selfield = rs_selfield_ekko-value.
PERFORM conversion_po.
PERFORM get_ekpo.
PERFORM fieldcat_ekpo.
PERFORM layout.
PERFORM event_ekpo.
PERFORM grid_ekpo.
PERFORM ucomm_ekpo USING r_ucomm
CHANGING rs_selfield.
ELSE.
MESSAGE 'Invalid Field' TYPE 'S'.
ENDIF.
ENDCASE.
ENDFORM.
"ucomm_ekko
*&---------------------------------------------------------------------*
*& Form GET_EKKO
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_ekko .
REFRESH it_ekko.
SELECT ebeln bukrs lifnr
FROM ekko INTO TABLE it_ekko
WHERE ebeln IN s_ebeln.
IF sy-subrc = 0.
SORT it_ekko BY ebeln.
REFRESH it_out_ekko.
LOOP AT it_ekko INTO wa_ekko.
wa_out_ekko-ebeln = wa_ekko-ebeln.
wa_out_ekko-bukrs = wa_ekko-bukrs.
wa_out_ekko-lifnr = wa_ekko-lifnr.
APPEND wa_out_ekko TO it_out_ekko.
CLEAR: wa_out_ekko, wa_ekko.
ENDLOOP.
ELSE.
MESSAGE 'Purchase Order doesn''t exist' TYPE 'I'.
ENDIF.
ENDFORM.
" GET_EKKO
*&---------------------------------------------------------------------*
*& Form FIELDCAT_EKKO
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM fieldcat_ekko .
CLEAR wa_fcat_ekko.
REFRESH it_fcat_ekko.
IF it_out_ekko IS NOT INITIAL.
DATA lv_col TYPE i VALUE 0.
lv_col
= 1 + lv_col.
wa_fcat_ekko-col_pos = lv_col.
wa_fcat_ekko-fieldname = 'EBELN'.
wa_fcat_ekko-tabname = 'IT_OUT_EKKO'.
wa_fcat_ekko-seltext_l = 'Purchase Order'.
APPEND wa_fcat_ekko TO it_fcat_ekko.
CLEAR wa_fcat_ekko.
lv_col
= 1 + lv_col.
wa_fcat_ekko-col_pos = lv_col.
wa_fcat_ekko-fieldname = 'BUKRS'.
wa_fcat_ekko-tabname = 'IT_OUT_EKKO'.
wa_fcat_ekko-seltext_l = 'Company Code'.
APPEND wa_fcat_ekko TO it_fcat_ekko.
CLEAR wa_fcat_ekko.
lv_col
= 1 + lv_col.
wa_fcat_ekko-col_pos = lv_col.
wa_fcat_ekko-fieldname = 'LIFNR'.
wa_fcat_ekko-tabname = 'IT_OUT_EKKO'.
wa_fcat_ekko-seltext_l = 'Vendor'.
APPEND wa_fcat_ekko TO it_fcat_ekko.
CLEAR wa_fcat_ekko.
ENDIF.
ENDFORM.
" FIELDCAT_EKKO
*&---------------------------------------------------------------------*
*& Form LAYOUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM layout .
wa_layout-zebra = 'X'.
wa_layout-colwidth_optimize = 'X'.
wa_layout-box_fieldname = 'SEL'.
ENDFORM.
" LAYOUT
*&---------------------------------------------------------------------*
*& Form EVENT_EKKO
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM event_ekko .
REFRESH it_event_ekko.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
* EXPORTING
* I_LIST_TYPE
=0
IMPORTING
et_events
= it_event_ekko
EXCEPTIONS
list_type_wrong
=1
OTHERS
= 2.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
i_callback_program
= v_prog
i_callback_pf_status_set
=''
i_callback_user_command
= 'UCOMM_EKKO'
I_STRUCTURE_NAME
=
is_layout
= wa_layout
it_fieldcat
= it_fcat_ekko
IT_EXCLUDING
=
IT_SPECIAL_GROUPS
=
IT_SORT
=
IT_FILTER
=
IS_SEL_HIDE
=
I_DEFAULT
= 'X'
I_SAVE
=''
IS_VARIANT
=
it_events
= it_event_ekko
IT_EVENT_EXIT
=
IS_PRINT
=
IS_REPREP_ID
=
I_SCREEN_START_COLUMN
=0
I_SCREEN_START_LINE
=0
I_SCREEN_END_COLUMN
=0
I_SCREEN_END_LINE
=0
IR_SALV_LIST_ADAPTER
=
IT_EXCEPT_QINFO
=
I_SUPPRESS_EMPTY_DATA
= ABAP_FALSE
IMPORTING
E_EXIT_CAUSED_BY_CALLER
=
ES_EXIT_CAUSED_BY_USER
=
TABLES
t_outtab
= it_out_ekko
EXCEPTIONS
program_error
=1
OTHERS
= 2.
ENDIF.
ENDFORM.
" GRID_EKKO
*&---------------------------------------------------------------------*
*& Form top_ekko
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM top_ekko.
CLEAR wa_top_ekko.
REFRESH it_top_ekko.
DATA date TYPE char12.
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal
= sy-datum
IMPORTING
date_external
= date
EXCEPTIONS
date_internal_is_invalid = 1
OTHERS
= 2.
wa_top_ekko-typ = 'H'.
wa_top_ekko-info = 'Purchase Order Header'.
APPEND wa_top_ekko TO it_top_ekko.
CLEAR wa_top_ekko.
wa_top_ekko-typ = 'S'.
wa_top_ekko-info = 'Report: '.
CONCATENATE wa_top_ekko-info v_prog
INTO wa_top_ekko-info.
APPEND wa_top_ekko TO it_top_ekko.
CLEAR wa_top_ekko.
wa_top_ekko-typ = 'S'.
wa_top_ekko-info = 'User Name: '.
CONCATENATE wa_top_ekko-info v_name
INTO wa_top_ekko-info.
APPEND wa_top_ekko TO it_top_ekko.
CLEAR wa_top_ekko.
wa_top_ekko-typ = 'S'.
wa_top_ekko-info = 'Date: '.
CONCATENATE wa_top_ekko-info date
INTO wa_top_ekko-info.
APPEND wa_top_ekko TO it_top_ekko.
CLEAR wa_top_ekko.
wa_fcat_ekpo-tabname = 'IT_OUT_EKPO'.
wa_fcat_ekpo-seltext_l = 'Purchase Order'.
APPEND wa_fcat_ekpo TO it_fcat_ekpo.
CLEAR wa_fcat_ekpo.
lv_col
= 1 + lv_col.
wa_fcat_ekpo-col_pos = lv_col.
wa_fcat_ekpo-fieldname = 'EBELP'.
wa_fcat_ekpo-tabname = 'IT_OUT_EKPO'.
wa_fcat_ekpo-seltext_l = 'PO Item'.
APPEND wa_fcat_ekpo TO it_fcat_ekpo.
CLEAR wa_fcat_ekpo.
lv_col
= 1 + lv_col.
wa_fcat_ekpo-col_pos = lv_col.
wa_fcat_ekpo-fieldname = 'MATNR'.
wa_fcat_ekpo-tabname = 'IT_OUT_EKPO'.
wa_fcat_ekpo-seltext_l = 'Material'.
APPEND wa_fcat_ekpo TO it_fcat_ekpo.
CLEAR wa_fcat_ekpo.
lv_col
= 1 + lv_col.
wa_fcat_ekpo-col_pos = lv_col.
wa_fcat_ekpo-fieldname = 'WERKS'.
wa_fcat_ekpo-tabname = 'IT_OUT_EKPO'.
wa_fcat_ekpo-seltext_l = 'Plant'.
APPEND wa_fcat_ekpo TO it_fcat_ekpo.
CLEAR wa_fcat_ekpo.
lv_col
= 1 + lv_col.
wa_fcat_ekpo-col_pos = lv_col.
wa_fcat_ekpo-fieldname = 'LGORT'.
wa_fcat_ekpo-tabname = 'IT_OUT_EKPO'.
wa_fcat_ekpo-seltext_l = 'Storage Location'.
APPEND wa_fcat_ekpo TO it_fcat_ekpo.
CLEAR wa_fcat_ekpo.
lv_col
= 1 + lv_col.
wa_fcat_ekpo-col_pos = lv_col.
wa_fcat_ekpo-fieldname = 'MENGE'.
wa_fcat_ekpo-tabname = 'IT_OUT_EKPO'.
wa_fcat_ekpo-seltext_l = 'PO Quantity'.
*
*
*
*
*
*
*
*
*
*
*
*
*
IT_FILTER
=
IS_SEL_HIDE
=
I_DEFAULT
= 'X'
I_SAVE
=''
IS_VARIANT
=
it_events
= it_event_ekpo
* IT_EVENT_EXIT
=
* IS_PRINT
=
* IS_REPREP_ID
=
* I_SCREEN_START_COLUMN
=0
* I_SCREEN_START_LINE
=0
* I_SCREEN_END_COLUMN
=0
* I_SCREEN_END_LINE
=0
* IR_SALV_LIST_ADAPTER
=
* IT_EXCEPT_QINFO
=
* I_SUPPRESS_EMPTY_DATA
= ABAP_FALSE
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER
=
* ES_EXIT_CAUSED_BY_USER
=
TABLES
t_outtab
= it_out_ekpo
EXCEPTIONS
program_error
=1
OTHERS
= 2.
ENDIF.
ENDFORM.
" GRID_EKPO
*&---------------------------------------------------------------------*
*& Form top_ekpo
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM top_ekpo.
CLEAR wa_top_ekpo.
REFRESH it_top_ekpo.
wa_top_ekpo-typ = 'H'.
wa_top_ekpo-info = 'Purchase Order Item wise Display'.
APPEND wa_top_ekpo TO it_top_ekpo.
CLEAR wa_top_ekpo.
ENDFORM.
The output is follows:
Selection Screen:
" UCOMM_EKPO
Third Output: