Here is a simple example for fetching
data from multiple tables for the beginners.
Note* To perform selection from multiple tables, check for the common fields preferably key fields are required to establish the relationship.
Check this sample code. Here I'm
fetching the data from two tables
REPORT ztest.
TYPES: BEGIN OF ty_mara , " STRUCTURE FOR MARA TABLE
matnr TYPE mara-matnr, " MATERIAL NO.
ersda TYPE mara-ersda, " CREATION DATE
ernam TYPE mara-ernam, " CREATED BY P
END OF ty_mara.
TYPES: BEGIN OF ty_marc, " STRUCTURE FOR MARC TABLE
matnr TYPE marc-matnr,
werks TYPE marc-werks, " PLANT
pstat TYPE marc-pstat, " Maintenance status
END OF ty_marc.
TYPES : BEGIN OF ty_makt, " STRUCTURE FOR MAKT TABLE
matnr TYPE makt-matnr,
spras TYPE makt-spras, " LANGUAGE
maktx TYPE makt-maktx, " MATERIAL DESCRIPTION
END OF ty_makt.
TYPES: BEGIN OF ty_final, " FINAL STRUCTURE
matnr TYPE mara-matnr,
spras TYPE makt-spras,
maktx TYPE makt-maktx,
werks TYPE marc-werks,
pstat TYPE marc-pstat,
ersda TYPE mara-ersda,
ernam TYPE mara-ernam,
END OF ty_final.
DATA: itab1 TYPE TABLE OF ty_mara WITH HEADER LINE, wa1 TYPE ty_mara.
DATA: itab2 TYPE TABLE OF ty_marc WITH HEADER LINE, wa2 TYPE ty_marc.
DATA: itab3 TYPE TABLE OF ty_makt WITH HEADER LINE, wa3 TYPE ty_makt.
DATA: itab4 TYPE TABLE OF ty_final WITH HEADER LINE, wa4 TYPE ty_final.
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_matnr TYPE mara-matnr OBLIGATORY.
SELECTION-SCREEN: END OF BLOCK b1.
START-OF-SELECTION.
PERFORM get_data.
PERFORM process_data.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_data.
SELECT matnr ersda ernam FROM mara INTO TABLE itab1 WHERE matnr = p_matnr.
IF sy-subrc = 0.
SELECT matnr werks pstat FROM marc INTO TABLE itab2 FOR ALL ENTRIES IN itab1 WHERE matnr = itab1-matnr.
ENDIF.
IF sy-subrc = 0.
SELECT matnr spras maktx FROM makt INTO TABLE itab3 FOR ALL ENTRIES IN itab1 WHERE matnr = itab1-matnr.
ENDIF.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form PROCESS_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM process_data .
LOOP AT itab3 INTO wa3.
wa4-matnr = wa3-matnr.
wa4-spras = wa3-spras.
wa4-maktx = wa3-maktx.
READ TABLE itab2 INTO wa2 WITH KEY matnr = wa3-matnr.
wa4-werks = wa2-werks.
wa4-pstat = wa2-pstat.
READ TABLE itab1 INTO wa1 WITH KEY matnr = wa3-matnr.
wa4-ersda = wa1-ersda.
wa4-ernam = wa1-ernam.
APPEND wa4 TO itab4.
CLEAR WA1.
CLEAR WA2.
CLEAR WA3.
CLEAR WA4.
ENDLOOP.
PERFORM DISPLAy_DATA.
ENDFORM. " PROCESS_DATA
*&---------------------------------------------------------------------*
*& Form DISPLAy_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form DISPLAy_DATA .
LOOP AT ITAB4 INTO WA4.
WRITE:/ WA4-MATNR, 20 WA4-ERSDA, 40 WA4-ERNAM, 60 WA4-SPRAS, 80 WA4-PSTAT.
ENDLOOP.
endform. " DISPLAy_DATA
No comments:
Post a Comment