ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Read Accesses →  SELECT →  SELECT - FROM → 

Joins

The example demonstrates inner and outer joins in the SELECT statement.

Source Code

REPORT demo_joins NO STANDARD PAGE HEADING.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: class_constructor,
                   main.
  PRIVATE SECTION.
    CLASS-DATA: wa1 TYPE demo_join1,
                wa2 TYPE demo_join2,
                out TYPE REF TO if_demo_output.
    CLASS-DATA BEGIN OF wa.
            INCLUDE STRUCTURE wa1 AS wa1 RENAMING WITH SUFFIX 1.
            INCLUDE STRUCTURE wa2 AS wa2 RENAMING WITH SUFFIX 2.
    CLASS-DATA END OF wa.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA itab LIKE TABLE OF wa.
    out->begin_section( `Inner Joins`
      )->begin_section( `demo1 INNER JOIN demo2` ).
    SELECT t1~a AS a1 t1~b AS b1 t1~c AS c1 t1~d AS d1
           t2~d AS d2 t2~e AS e2 t2~f AS f2 t2~g AS g2 t2~h AS h2
       FROM demo_join1 AS t1
         INNER JOIN demo_join2 AS t2 ON t2~d = t1~d
       INTO CORRESPONDING FIELDS OF TABLE itab
       ORDER BY t1~d.
    out->write( itab
      )->next_section( `demo2 INNER JOIN demo1` ).
    SELECT t1~a AS a1 t1~b AS b1 t1~c AS c1 t1~d AS d1
           t2~d AS d2 t2~e AS e2 t2~f AS f2 t2~g AS g2 t2~h AS h2
       FROM demo_join2 AS t2
         INNER JOIN demo_join1 AS t1 ON t1~d = t2~d
       INTO CORRESPONDING FIELDS OF TABLE itab
       ORDER BY t1~d.
    out->write( itab
      )->end_section(
      )->next_section( `Outer Joins`
      )->begin_section( `demo1 OUTER JOIN demo2` ).
    SELECT t1~a AS a1 t1~b AS b1 t1~c AS c1 t1~d AS d1
           t2~d AS d2 t2~e AS e2 t2~f AS f2 t2~g AS g2 t2~h AS h2
       FROM demo_join1 AS t1
         LEFT OUTER JOIN demo_join2 AS t2 ON t2~d = t1~d
       INTO CORRESPONDING FIELDS OF TABLE itab
       ORDER BY t1~d.
    out->write( itab
      )->next_section( `demo2 OUTER JOIN demo2` ).
    SELECT t1~a AS a1 t1~b AS b1 t1~c AS c1 t1~d AS d1
           t2~d AS d2 t2~e AS e2 t2~f AS f2 t2~g AS g2 t2~h AS h2
       FROM demo_join2 AS t2
         LEFT OUTER JOIN demo_join1 AS t1 ON t1~d = t2~d
       INTO CORRESPONDING FIELDS OF TABLE itab
       ORDER BY t1~d.
    out->display( itab ).
  ENDMETHOD.
  METHOD class_constructor.
    DATA: itab1 LIKE TABLE OF wa1,
          itab2 LIKE TABLE OF wa2.
    out = cl_demo_output=>new( )->begin_section( `Database Tables` ).
    itab1 = VALUE #( ( a = 'a1' b = 'b1' c = 'c1'  d = 'uu' )
                     ( a = 'a2' b = 'b2' c = 'c2'  d = 'uu' )
                     ( a = 'a3' b = 'b3' c = 'c3'  d = 'vv' )
                     ( a = 'a4' b = 'b4' c = 'c4'  d = 'ww' )
                    ).
    itab2 = VALUE #( ( d = 'uu' e = 'e1' f = 'f1'  g = 'g1'  h = 'h1' )
                     ( d = 'ww' e = 'e2' f = 'f2'  g = 'g2'  h = 'h2' )
                     ( d = 'xx' e = 'e3' f = 'f3'  g = 'g3'  h = 'h3' )
                    ).
    DELETE FROM demo_join1.
    INSERT demo_join1 FROM TABLE itab1.
    DELETE FROM demo_join2.
    INSERT demo_join2 FROM TABLE itab2.
    out->begin_section( `demo1`
      )->write( itab1
      )->next_section( `demo2`
      )->write( itab2
      )->end_section(
      )->end_section( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

Inner and outer joins between two tables DEMO_JOIN1 and DEMO_JOIN2, for which the last column of DEMO_JOIN1 is equated with the first column of DEMO_JOIN2 in the join conditions. The results are as follows:

demo1           demo2
-----           -----
a1 b1 c1 uu     uu e1 f1 g1 h1
a2 b2 c2 uu     ww e2 f2 g2 h2
a3 b3 c3 vv     xx e3 f3 g3 h3
a4 b4 c4 ww

demo1 INNER JOIN demo2         demo2 INNER JOIN demo1
----------------------         ----------------------
a1 b1 c1 uu uu e1 f1 g1 h1     a1 b1 c1 uu uu e1 f1 g1 h1
a2 b2 c2 uu uu e1 f1 g1 h1     a2 b2 c2 uu uu e1 f1 g1 h1
a4 b4 c4 ww ww e2 f2 g2 h2     a4 b4 c4 ww ww e2 f2 g2 h2

demo1 OUTER JOIN demo2         demo2 OUTER JOIN demo1
----------------------         ----------------------
a1 b1 c1 uu uu e1 f1 g1 h1     a1 b1 c1 uu uu e1 f1 g1 h1
a2 b2 c2 uu uu e1 f1 g1 h1     a2 b2 c2 uu uu e1 f1 g1 h1
a3 b3 c3 vv                    a4 b4 c4 ww ww e2 f2 g2 h2
a4 b4 c4 ww ww e2 f2 g2 h2                 xx e3 f3 g3 h3