Hello.

An upcoming databse upgrade to Sybase 16 has brought attention to the need for the lateral keyword with nested outer join statements. (I think this may have been introduced with a version before 16.) I have found very little documentation to explain this requirement. I have tested a few sqls with left outer joins and they seem to be returning the expected results even without the Lateral keyword. Is this a reliable practice to continue the use of outer joins without the lateral keyword? Can a detailed explanation be provided as to how these types of sql statements should be handled with Sybase 16?

asked 09 Jul '13, 10:22

JWinings's gravatar image

JWinings
66124
accept rate: 0%

Why would you think that nested outer joins need lateral?

You surely can "stack" outer joins like this (untested)

FROM ((t1 left outer join t2 on t1.pk = t2.pk)
   left outer join t3 on t1.pk = t3.pk)
       left outer join t4 on t1.pk = t4.pk...

IMHO, LATERAL is only needed for particular cases like joining with a stored procedure when the parameter are parts of the join, too.

(09 Jul '13, 11:58) Volker Barth

I had read this in some of the documentation and it seemed to indicate it was no longer supported. I am looking for either clarification and/or explanation on the issue.

"References to table expressions preceding in the FROM clause may now be used in ON clauses of nested outer joins. In previous releases, outer references in the ON phrase were permitted. Such outer references must now be indicated by use of the LATERAL keyword. The restriction enforces clarity and conforms to the SQL/99 standard. The following query is an example of one that is no longer valid, as it contains an outer reference (highlighted) without use of the LATERAL keyword: SELECT * FROM T1, T2 LEFT OUTER JOIN ( T3 LEFT OUTER JOIN T4 ON T1.col1 = T2.col2 ) ON T1.col2 = T2.col2)"

(09 Jul '13, 12:20) JWinings
Replies hidden

I have fond (not!) memories of being shocked and confused when reading that for the first time. Sadly, I don't remember much from 2003, but that sticks... see my answer below.

(09 Jul '13, 13:00) Breck Carter

I believe you may have misread a behavior change made in Version 9: "... The following query is an example of one that is no longer valid, as it contains an outer reference (highlighted) without use of the LATERAL keyword: ..."

Don't confuse "outer reference" with "outer join".

An outer join is a SQL join clause like LEFT OUTER JOIN.

An outer reference is an English documentation term for a reference inside a ( nested table expression ) to an object in the outer statement. That's what the Help is talking about when it says "You must use a lateral derived table to use an outer reference in the FROM clause."

So, no, there is no requirement to use the LATERAL keyword with outer joins.


FROM clause

lateral-derived-table - A derived table, stored procedure, or joined table that may include references to objects in the parent statement (outer references). You must use a lateral derived table to use an outer reference in the FROM clause.

You can use outer references only to tables that precede the lateral derived table in the FROM clause. For example, you cannot use an outer reference to an item in the SELECT list.

The table and the outer reference must be separated by a comma. For example, the following queries are valid:

 SELECT *
 FROM A, LATERAL( B LEFT OUTER JOIN C ON ( A.x = B.x ) ) myLateralDT;

SELECT *
 FROM A, LATERAL( SELECT * FROM B WHERE A.x = B.x ) myLateralDT;

SELECT *
 FROM A, LATERAL( procedure-name( A.x ) ) myLateralDT;

Specifying LATERAL (table-expression) is equivalent to specifying LATERAL (SELECT * FROM table-expression).

permanent link

answered 09 Jul '13, 12:35

Breck%20Carter's gravatar image

Breck Carter
32.5k5417261050
accept rate: 20%

1

This is the clarification that I was looking for. I appreciate the very understandable explanation. Thank you.

(09 Jul '13, 16:53) JWinings
Replies hidden

@JWinings: Well, feel free then to accept Breck's good answer, as described here:

How do I accept an answer to my question?

(09 Jul '13, 17:30) Volker Barth
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×7
×5

question asked: 09 Jul '13, 10:22

question was seen: 7,181 times

last updated: 09 Jul '13, 17:31