ABAP SELECT inner join statement to select from two tables at the same time

This example ABAP report demonstrates how to implement a basic SELECT INNER JOIN between two tables (i.e. EKPO and EKET). It then displays the output using a very basic objects based ALV grid.


*&-----------------------------------------------------------
*& Report  ZDEMO_JOIN
*&
*&-----------------------------------------------------------
*&
*& Example of a simple Inner join
*& ...................................
*&
*& Author: SAP Development
*&
*& Basic INNER Join example using CL_SALV_TABLE ALV method
*&-----------------------------------------------------------~
REPORT zsalv_demo.
*Data Declaration
*----------------
TYPES: BEGIN OF t_datatab,
  ebeln TYPE ekpo-ebeln,
  ebelp TYPE ekpo-ebelp,
  matnr TYPE ekpo-matnr,
  menge TYPE ekpo-menge,
  meins TYPE ekpo-meins,
  netpr TYPE ekpo-netpr,
  peinh TYPE ekpo-peinh,
  wemng type eket-wemng,
  eindt type eket-eindt,
 END OF t_datatab.
DATA: it_datatab TYPE STANDARD TABLE OF t_datatab INITIAL SIZE 0,
      wa_datatab TYPE t_datatab.
DATA alv_table    TYPE REF TO cl_salv_table.
DATA alv_columns TYPE REF TO cl_salv_columns_table.
DATA single_column  TYPE REF TO cl_salv_column.
************************************************************************
* START-OF-SELECTION
START-OF-SELECTION.
  PERFORM data_retrieval.
  PERFORM display_settings.
************************************************************************
* END-OF-SELECTION
END-OF-SELECTION.
  perform display_alv_report.
*&---------------------------------------------------------------------*
*& FORM data_retrieval.
*&---------------------------------------------------------------------*
FORM data_retrieval.
  select a~ebeln a~ebelp a~matnr a~menge a~meins
          a~netpr a~peinh b~wemng b~eindt
   up to 10 rows
    from ekpo as a inner join eket as b
     on  b~ebeln = a~ebeln
     and b~ebelp = a~ebelp
    into table it_datatab.
ENDFORM.                    " DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*& FORM display_settings.
*&---------------------------------------------------------------------*
FORM display_settings.
  DATA: err_message   TYPE REF TO cx_salv_msg.
  TRY.
      cl_salv_table=>factory(
      IMPORTING
        r_salv_table = alv_table
      CHANGING
        t_table      = it_datatab ).
      alv_columns = alv_table->get_columns( ).
      PERFORM report_settings.
    CATCH cx_salv_msg INTO err_message.
*   Add error processing
  ENDTRY.
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM display_alv_report.
*&---------------------------------------------------------------------*
FORM display_alv_report.
  alv_columns->set_optimize( ). "optimise the column width
  alv_table->display( ).
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM report_settings.
*&---------------------------------------------------------------------*
FORM report_settings.
  DATA: report_settings TYPE REF TO cl_salv_display_settings.
  report_settings = alv_table->get_display_settings( ).
  report_settings->set_striped_pattern( if_salv_c_bool_sap=>true ).
  report_settings->set_list_header( 'Display details for EKPO and EKET via INNER Join' ).
ENDFORM.


Related Articles

ABAP COLLECT statement syntax to add up all numeric internal table values within SAP
ABAP DELETE statement keyword to delete data from SAP internal and database tables
ABAP DESCRIBE statement keyword to get information about tables and fields
PERFORM TABLES command passing internal table as parameter
ABAP read command to read line of internal table in SAP
ABAP UPDATE command to modify database field values
AUTHORITY-CHECK abap Statement / command
ABAP delete command in SAP
ABAP MODIFY statement to update SAP data within database and internal tables
ABAP WRITE statement command with in SAP to display data report to users
SAP ABAP Statement syntax including basic implementation code
Concatenate ABAP statement syntax for concatenating data objects, sting values or rows of an SAP internal table
ABAP EXPORT data TO MEMORY ID and import it back again
IF, CHECK & WHEN ABAP commends example source code and information
Call Function module IN BACKGROUND TASK - Execute abap code in seperate background process
Function module IN UPDATE TASK statement - Execute abap code in seperate unit of work
ABAP STRLEN command to get the value length of a SAP field
SAP ABAP SELECT command and its different uses
SELECT..ENDSELECT command
ABAP FOR ALL ENTRIES SELECT statement addition in SAP data retrieval
SELECT directly into an internal table
SELECT directly into an internal table when field order is different
Function module STARTING NEW TASK statement - Execute abap code in seperate work process