简体   繁体   中英

Why subquery doesn't work in ON clause in DB2

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM