简体   繁体   English

为什么子查询在DB2的ON子句中不起作用

[英]Why subquery doesn't work in ON clause in DB2

Why this simple query works fine in oracle but doesn't work in DB2: 为什么这个简单的查询在oracle中可以正常工作,而在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. 将涉及子查询的条件移到where子句可能会有所帮助,但这将限制外部联接进入内部。

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: 当我尝试运行查询时,出现-338错误 ,根据信息中心(请参阅链接),对ON子句有以下限制:

An ON clause associated with a JOIN operator or in a MERGE statement is not valid for one of the following reasons. 由于以下原因之一,与JOIN运算符或MERGE语句关联的ON子句无效。

 * 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! 这在DB2 V10.1中有效! No fixpack installed. 没有安装修订包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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