Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to left outer join on three tables?

mark05
Explorer

Hey,

I'm trying to join three database tables in my ABAP report.

I have an input local table with ID's in it. So I use "FOR ALL ENTRIES IN" on this table.

The data for the result table comes from three database tables.

The first one (names) contains ID and NAME,

the second one (buildings) contains ID, BUILDING and PRIORITY

and the third one (details) contains BUILDING, DESCRIPTION, LOCATION, COUNTRY, INDEX and TYPE.

(The red ones are the fields which I want to have in the result table.)

The result table should look like:

My SQL Code is:


SELECT

     a~id

     a~name

    b~building

    c~description

    c~location

    c~country

FROM names AS a

     LEFT JOIN buildings AS b

          ON b~id = a~id

          AND b~priority = '3'

     LEFT JOIN details AS c

         ON c~building = b~building

         AND c~index = '01'

         AND c~type = 'B'

INTO CORRESPONDING FIELDS OF TABLE et_result

FOR ALL ENTRIES IN lt_input

WHERE a~id = lt_input-id.

This code looks right for me, but ABAP's syntax check says:

    Line 01: "The elements in the "SELECT LIST" list must be separated using commas.

This error only occurs when using LEFT JOIN two times. When I make first a LEFT JOIN and second a JOIN the syntax check says "okay".

But then I will get the false results, because all lines of result table with NULL values will be deleted. And that's not what I want.

I hope you can help me!

Best regards,

Mark

1 ACCEPTED SOLUTION

Former Member

It's strange the syntax check alerts here.

But corresponding to this message, I interpret, that you are on NW 7.4 >= SP08.

In general it is recommanded to use the new Open-SQL syntax. This means, you have to separate your fields with comma and use @ for variables. In your case:

  1. SELECT 
  2.      a~id,
  3.      a~name,
  4.     b~building,
  5.     c~description,
  6.     c~location,
  7.     c~country
  8. FROM names AS
  9.      LEFT JOIN buildings AS
  10.           ON b~id = a~id 
  11.           AND b~priority = '3' 
  12.      LEFT JOIN details AS
  13.          ON c~building = b~building 
  14.          AND c~index = '01' 
  15.          AND c~type = 'B' 
  16. INTO CORRESPONDING FIELDS OF TABLE @et_result 
  17. FOR ALL ENTRIES IN @lt_input 
  18. WHERE a~id = @lt_input-id. 
4 REPLIES 4

amol_samte
Contributor
0 Kudos

Might be helpful

https://scn.sap.com/thread/3700525

-Amol

0 Kudos

I have seen this post before. But in my case all conditions which are related to the join tables are already in the join conditions...

But thanks anyway

Former Member

It's strange the syntax check alerts here.

But corresponding to this message, I interpret, that you are on NW 7.4 >= SP08.

In general it is recommanded to use the new Open-SQL syntax. This means, you have to separate your fields with comma and use @ for variables. In your case:

  1. SELECT 
  2.      a~id,
  3.      a~name,
  4.     b~building,
  5.     c~description,
  6.     c~location,
  7.     c~country
  8. FROM names AS
  9.      LEFT JOIN buildings AS
  10.           ON b~id = a~id 
  11.           AND b~priority = '3' 
  12.      LEFT JOIN details AS
  13.          ON c~building = b~building 
  14.          AND c~index = '01' 
  15.          AND c~type = 'B' 
  16. INTO CORRESPONDING FIELDS OF TABLE @et_result 
  17. FOR ALL ENTRIES IN @lt_input 
  18. WHERE a~id = @lt_input-id. 

0 Kudos

That works!!

Thank you very much!