REPORT zpet_join_report.
TYPES: BEGIN OF ty_full,
pet_id TYPE zpet_id_de,
pet_name TYPE zpet_name_de,
pet_type TYPE zpet_type_de,
owner_id TYPE char10,
owner_name TYPE char30,
END OF ty_full.
DATA it_full TYPE TABLE OF ty_full.
SELECT
a~pet_id,
a~pet_name,
a~pet_type,
b~owner_id,
b~owner_name
FROM zpet_adoption AS a
INNER JOIN zpet_owner AS b
ON a~pet_id = b~pet_id
INTO TABLE @it_full.
IF sy-subrc <> 0.
WRITE: / 'No data found.'.
EXIT.
ENDIF.
LOOP AT it_full INTO DATA(ls_full).
WRITE: / 'Pet ID :', ls_full-pet_id,
/ 'Pet Name :', ls_full-pet_name,
/ 'Pet Type :', ls_full-pet_type,
/ 'Owner ID :', ls_full-owner_id,
/ 'Owner Name:', ls_full-owner_name,
/ '-----------------------------'.
ENDLOOP.
Alv report:--------
REPORT zpet_join_alv.
TYPE-POOLS: slis.
*-----------------------------
* Structure for JOIN Result
*-----------------------------
TYPES: BEGIN OF ty_full,
pet_id TYPE zpet_id_de,
pet_name TYPE zpet_name_de,
pet_type TYPE zpet_type_de,
owner_id TYPE char10,
owner_name TYPE char30,
END OF ty_full.
DATA: it_full TYPE TABLE OF ty_full,
wa_full TYPE ty_full.
*-----------------------------
* ALV Components
*-----------------------------
DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv.
*-----------------------------
* Fetch Data Using JOIN
*-----------------------------
SELECT
a~pet_id,
a~pet_name,
a~pet_type,
b~owner_id,
b~owner_name
FROM zpet_adoption AS a
INNER JOIN zpet_owner AS b
ON a~pet_id = b~pet_id
INTO TABLE @it_full.
IF sy-subrc <> 0.
MESSAGE 'No records found in join' TYPE 'I'.
LEAVE PROGRAM.
ENDIF.
*-----------------------------
* Field Catalog Build
*-----------------------------
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'PET_ID'.
wa_fieldcat-seltext_m = 'Pet ID'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'PET_NAME'.
wa_fieldcat-seltext_m = 'Pet Name'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'PET_TYPE'.
wa_fieldcat-seltext_m = 'Pet Type'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'OWNER_ID'.
wa_fieldcat-seltext_m = 'Owner ID'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'OWNER_NAME'.
wa_fieldcat-seltext_m = 'Owner Name'.
APPEND wa_fieldcat TO it_fieldcat.
*-----------------------------
* Call ALV
*-----------------------------
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = ''
it_fieldcat = it_fieldcat
TABLES
t_outtab = it_full
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'Error in ALV Display' TYPE 'E'.
ENDIF.
Comments