Why subquery doesn't work in ON clause in DB2

5.1k Views Asked by At

Why this simple query works fine in oracle but doesn't work in DB2:

select * 
from 
sysibm.dual d1 
left join sysibm.dual d2 on 1=1 and exists (select 1 from sysibm.dual)

Moving subquery-involving condition to where clause may help, but that will restrain outer join into inner.

2

There are 2 best solutions below

2
bhamby On

When I try to run the query you have, I get a -338 error, which according to Information Center (see link), there are the following restrictions on the ON clause:

An ON clause associated with a JOIN operator or in a MERGE statement is not valid for one of the following reasons.

* The ON clause cannot include any subqueries.
* Column references in an ON clause must only reference columns
  of tables that are in the scope of the ON clause.
* Scalar fullselects are not allowed in the expressions of an ON clause.
* A function referenced in an ON clause of a full outer join 
  must be deterministic and have no external action.
* A dereference operation (->) cannot be used.
* A SQL function or SQL method cannot be used.
* The ON clause cannot include an XMLQUERY or XMLEXISTS expression.

I'm not sure if it's possible with your query, but do you think perhaps you could re-write something like this:

select * 
from 
sysibm.dual d1 
left join (
    SELECT dl.*,
    CASE WHEN EXISTS (SELECT 1 FROM sysibm.dual)
         THEN 1
         ELSE 0
    END AS jn
    FROM sysibm.dual dl
) D2
  on 1=1 and 1=d2.jn
0
Bjallemad On

This works in DB2 V10.1! No fixpack installed.