ORA-01427: 单行子查询返回多于一行 ,,WHEN USING SELECT COUNT
Posted
技术标签:
【中文标题】ORA-01427: 单行子查询返回多于一行 ,,WHEN USING SELECT COUNT【英文标题】:ORA-01427: single-row subquery returns more than one row ,,WHEN USING SELECT COUNT 【发布时间】:2014-07-01 09:44:03 【问题描述】:我有这两个查询,两者都是相同的,但第一行的区别,第一个语句计算结果,另一个得到行, 问题是它得到了行并且无法计算它们
第一个查询出错(ORA-01427:单行子查询返回多行)
第一个(计数行)
SELECT COUNT(*) FROM (
SELECT A.* ,
SUBSTR(A.RECORD_DATE,9,4) RECORD_NOTICE_TIME ,
(SELECT B.CLOSE_FLAG FROM HARAM.NOTICES_DEPT B WHERE B.NOTICE_SN = A.NOTICE_SN AND B.DEPT_ID = 116 AND B.RECORD_STATUS <> 3) CLOSE_FLAG ,
(SELECT DEPARTMENT_ID FROM HARAM.EMPLOYES WHERE PERSONAL_CODE =A.NOTICE_USER) NOTICE_USER_DEPT ,
(SELECT SUBSTR(ND.CREATION_TIMESTAMP,9,4) TRANS_NOTICE_TIME FROM HARAM.NOTICES_DEPT ND WHERE ND.NOTICE_SN = A.NOTICE_SN AND ND.RECORD_STATUS != 3 AND ND.DEPT_ID = 116 ) TRANS_NOTICE_TIME,
(SELECT (SELECT DEPARTMENT_NAME FROM HARAM.DEPARTMENT WHERE DS.SECTION_ID=DEPARTMENT_ID) FROM HARAM.NOTICES_DEPT_SECTIONS DS WHERE DS.NOTICE_SN=A.NOTICE_SN) SECTION_NAME
FROM HARAM.NOTICES A
WHERE EXISTS (SELECT NULL FROM HARAM.NOTICES_DEPT C WHERE C.NOTICE_SN = A.NOTICE_SN AND C.RECORD_STATUS <> 3 AND C.DEPT_ID = 116 ) AND A.RECORD_STATUS <> 3
)
WHERE NOTICE_STATUS != 5 AND RECORD_STATUS <> 3 AND CLOSE_FLAG=3 AND SECTION_NAME IS NOT NULL
ORDER BY CLOSE_FLAG DESC , CREATION_TIMESTAMP DESC;
第二个(获取行)
SELECT * FROM (
SELECT A.* ,
SUBSTR(A.RECORD_DATE,9,4) RECORD_NOTICE_TIME ,
(SELECT B.CLOSE_FLAG FROM HARAM.NOTICES_DEPT B WHERE B.NOTICE_SN = A.NOTICE_SN AND B.DEPT_ID = 116 AND B.RECORD_STATUS <> 3) CLOSE_FLAG ,
(SELECT DEPARTMENT_ID FROM HARAM.EMPLOYES WHERE PERSONAL_CODE =A.NOTICE_USER) NOTICE_USER_DEPT ,
(SELECT SUBSTR(ND.CREATION_TIMESTAMP,9,4) TRANS_NOTICE_TIME FROM HARAM.NOTICES_DEPT ND WHERE ND.NOTICE_SN = A.NOTICE_SN AND ND.RECORD_STATUS != 3 AND ND.DEPT_ID = 116 ) TRANS_NOTICE_TIME,
(SELECT (SELECT DEPARTMENT_NAME FROM HARAM.DEPARTMENT WHERE DS.SECTION_ID=DEPARTMENT_ID) FROM HARAM.NOTICES_DEPT_SECTIONS DS WHERE DS.NOTICE_SN=A.NOTICE_SN) SECTION_NAME
FROM HARAM.NOTICES A
WHERE EXISTS (SELECT NULL FROM HARAM.NOTICES_DEPT C WHERE C.NOTICE_SN = A.NOTICE_SN AND C.RECORD_STATUS <> 3 AND C.DEPT_ID = 116 ) AND A.RECORD_STATUS <> 3
)
WHERE NOTICE_STATUS != 5 AND RECORD_STATUS <> 3 AND CLOSE_FLAG=3 AND SECTION_NAME IS NOT NULL
ORDER BY CLOSE_FLAG DESC,CREATION_TIMESTAMP DESC;
【问题讨论】:
有意思,你用的是什么版本的Oracle?此错误可能来自内部SELECT
子句中的 sub-select,与外部 SELECT
无关
你是对的,错误出现在(SELECT (SELECT DEPARTMENT_NAME FROM HARAM.DEPARTMENT WHERE DS.SECTION_ID=DEPARTMENT_ID) FROM HARAM.NOTICES_DEPT_SECTIONS DS WHERE DS.NOTICE_SN=A.NOTICE_SN)SECTION_NAME 但如何我解决了吗?
【参考方案1】:
您可能需要将您的INNER
Select 替换为以下之一。我将其转换为Join
并使用ROWNUM < =1
将行数限制为最多1。
(SELECT
D.DEPARTMENT_NAME
FROM HARAM.NOTICES_DEPT_SECTIONS DS, HARAM.DEPARTMENT D
WHERE DS.NOTICE_SN=A.NOTICE_SN
AND DS.SECTION_ID=D.DEPARTMENT_ID
AND ROWNUM <= 1
) SECTION_NAME
【讨论】:
以上是关于ORA-01427: 单行子查询返回多于一行 ,,WHEN USING SELECT COUNT的主要内容,如果未能解决你的问题,请参考以下文章
Oracle SQL:ORA-01427:单行子查询返回多于一行
ORA-01427: 单行子查询返回多于一行 ,,WHEN USING SELECT COUNT
我得到那个错误 ORA-01427: 单行子查询返回多于一行