Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

How to left outer join on three tables?

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

Tags:
Former Member
Former Member replied

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. 
1 View this answer in context
Not what you were looking for? View more on this topic or Ask a question