How to left outer join on three tables?
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:
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!
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:
- 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.