ORA-01427:单行子查询返回多行但是我在内部查询中需要多行?

时间:2014-07-18 01:28:55

标签: sql oracle

如果它可以返回多行,那么这个SQL工作对我有用吗?如何重写sql所以它应该适用于NVL2,我的输入参数是127,147

SELECT * 
FROM org_organization_definitions ORG 
where organization_code IN      
                    NVL2(:p_org_code, (SELECT REGEXP_SUBSTR (:p_org_code, 
                                                       '[^,]+', 
                                                       1, 
                                                       LEVEL) 
                                   FROM DUAL 
                             CONNECT BY REGEXP_SUBSTR (:p_org_code, 
                                                       '[^,]+', 
                                                       1, 
                                                       LEVEL) 
                                           IS NOT NULL),organization_code)

2 个答案:

答案 0 :(得分:0)

尝试以不同的方式修复逻辑:

SELECT * 
FROM org_organization_definitions ORG 
where organization_code = :p_org_code or
      (:p_org_code is null and
       organization_code IN (SELECT REGEXP_SUBSTR(:p_org_code, '[^,]+', 1, LEVEL) 
                             FROM DUAL 
                             CONNECT BY REGEXP_SUBSTR(:p_org_code, '[^,]+', 1, LEVEL)
                            )
      ) or 
      not exists (SELECT REGEXP_SUBSTR(:p_org_code, '[^,]+', 1, LEVEL) 
                  FROM DUAL 
                  CONNECT BY REGEXP_SUBSTR(:p_org_code, '[^,]+', 1, LEVEL)
                 )

答案 1 :(得分:0)

或者,您可以使用LEFT JOIN,如下所示:

SELECT ORG.* 
FROM org_organization_definitions ORG 
LEFT JOIN (SELECT REGEXP_SUBSTR (:p_org_code, 
                                                       '[^,]+', 
                                                       1, 
                                                       LEVEL) org_code
                                   FROM DUAL 
                             CONNECT BY REGEXP_SUBSTR (:p_org_code, 
                                                       '[^,]+', 
                                                       1, 
                                                       LEVEL) 
                                           IS NOT NULL) modified_org_codes
ON ORG.organization_code = modified_org_codes.org_code;