ABAP Keyword Documentation → ABAP - Reference → Processing External Data → ABAP Database Accesses → Open SQL → Open SQL - Read Accesses → SELECT → SELECT - FROM → SELECT - JOIN
Multiple Joins
This example demonstrates chaining and compounding of multiple joins in the SELECT
statement.
Other versions:
7.31 | 7.40 | 7.54
Source Code
DATA itab TYPE TABLE OF wa WITH EMPTY KEY.
out->begin_section( `Joins` ).
out->begin_section( `1. 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.
out->write( itab
)->next_section( `2. demo3 INNER JOIN demo4 ` ).
SELECT t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3,
t4~l AS l4, t4~m AS m4, t4~n AS n4
FROM
demo_join3 AS t3
INNER JOIN
demo_join4 AS t4 ON t4~l = t3~l
INTO CORRESPONDING FIELDS OF TABLE @itab.
out->write( itab
)->next_section( `3. (demo1 INNER JOIN demo2) ` &&
`LEFT OUTER JOIN demo3` ).
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,
t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3
FROM
( demo_join1 AS t1
INNER JOIN
demo_join2 AS t2 ON t2~d = t1~d )
LEFT OUTER JOIN
demo_join3 AS t3 ON t3~l = t2~d
INTO CORRESPONDING FIELDS OF TABLE @itab.
out->write( itab
)->next_section( `4. ((demo1 INNER JOIN demo2) ` &&
`LEFT OUTER JOIN demo3) INNER JOIN demo4` ).
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,
t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3,
t4~l AS l4, t4~m AS m4, t4~n AS n4
FROM
( ( demo_join1 AS t1
INNER JOIN
demo_join2 AS t2 ON t2~d = t1~d )
LEFT OUTER JOIN
demo_join3 AS t3 ON t3~l = t2~d )
INNER JOIN
demo_join4 AS t4 ON t4~l = t3~l
INTO CORRESPONDING FIELDS OF TABLE @itab.
out->write( itab
)->next_section( `5. (demo1 INNER JOIN demo2) ` &&
`LEFT OUTER JOIN (demo3 INNER JOIN demo4)` ).
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,
t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3,
t4~l AS l4, t4~m AS m4, t4~n AS n4
FROM
( demo_join1 AS t1
INNER JOIN
demo_join2 AS t2 ON t2~d = t1~d )
LEFT OUTER JOIN
( demo_join3 AS t3
INNER JOIN
demo_join4 AS t4 ON t4~l = t3~l ) ON t3~l = t2~d
INTO CORRESPONDING FIELDS OF TABLE @itab.
out->write( itab
)->next_section( `6. demo1 INNER JOIN demo2 ` &&
`LEFT OUTER JOIN demo3 INNER JOIN demo4` ).
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,
t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3,
t4~l AS l4, t4~m AS m4, t4~n AS n4
FROM
demo_join1 AS t1
INNER JOIN
demo_join2 AS t2 ON t2~d = t1~d
LEFT OUTER JOIN
demo_join3 AS t3 ON t3~l = t2~d
INNER JOIN
demo_join4 AS t4 ON t4~l = t3~l
INTO CORRESPONDING FIELDS OF TABLE @itab.
out->display( itab ).
Description
Chaining of up to four database tables using inner and outer joins.