错误 ORA-01427: 单行子查询返回多于一行
当我执行查询时,出现错误 [ORA-01427: 单行子查询返回多于一行]。我的查询结构如下:
SELECT LV.PRICE,
(SELECT C.MODEL_NAME FROM CARS C WHERE C.MODEL_ID = LV.MODEL_ID) as MODEL_NAME
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500
它在嵌套选择上中断。我知道视图和此查询中的逻辑都是正确的,并且嵌套选择不可能返回多于一行。 CARS 表的 MODEL_ID 是一个唯一字段。如果我在没有嵌套选择的情况下执行查询,它不会返回此错误。
LEDGER_VIEW 是一个构建在另一个视图之上的视图。这些堆叠视图在 Oracle 10g 中是否有可能存在错误?我不知道如何调试这个问题。
我知道我可以将此特定查询更改为联接而不是嵌套选择,但我想知道为什么会发生这种情况,因为我在其他不太容易修改的地方使用嵌套查询。
编辑:这是非常奇怪的事情。正如我所说,LEDGER_VIEW 是构建在另一个视图之上的。作为测试,我将嵌套视图的 SQL 直接复制到 LEDGER_VIEW 的 SQL 中,代替嵌套视图,并且它返回时没有错误(如预期)。这似乎向我证实,嵌套视图或嵌套视图+数据库链接的组合存在一些错误行为。
I'm getting the error [ORA-01427: single-row subquery returns more than one row] when I execute a query. I have a query structured like so:
SELECT LV.PRICE,
(SELECT C.MODEL_NAME FROM CARS C WHERE C.MODEL_ID = LV.MODEL_ID) as MODEL_NAME
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500
It's breaking on the nested select. I know the logic both in the view and in this query is correct, and that there's no chance of the nested select returning more than one row. The CARS table's MODEL_ID is a unique field. If I execute the query without the nested select it doesn't return this error.
The LEDGER_VIEW is a view built on top of another view. Is it possible that these stacked views are buggy in Oracle 10g? I don't know how else to debug this problem.
I am aware I could change this particular query to a join rather than a nested select, but I'd like to know why this is happening because I use nested queries in other places where it is not so easily modifiable.
EDIT: Here's the really strange thing. The LEDGER_VIEW is, as I said, built on top of another view. As a test, I copied the nested view's SQL directly into the SQL of the SQL of LEDGER_VIEW, in place of the nested view, and it returned with no errors (as expected). This seems to confirm to me that there is some buggy behavior either with nested views or with the combination of nested views + database links.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的子查询返回多行。使用以下查询找出
Car
表中的哪些MODELID
值重复:Your subquery is returning multiple rows. Use the query below to find out which
MODELID
values in theCar
table are duplicated:我无法通过创建堆叠视图来重新创建。 (尽管 RedFilters 会找到罪魁祸首)
但这引出了一个问题,为什么你要在这里进行行级查询?虽然 10g 应该对它们进行相同的优化,但我总是发现编写如下查询更容易,无论是为了可读性、可维护性,还是为了专门避免您遇到的错误(是不是总是如此,3年后,由应用程序(在数据库和调用应用程序中)是否存在会导致此错误的条件?一条 rouge 语句进入后您的整个应用程序就会崩溃?
I am unable to recreate via a creation of a stacked view. (althoug RedFilters will find the culprit)
But that begs the question, why are you doing the row level query here? While 10g ought to optimize them the same, I have always found it easier to write queries as below, both for readability, maintainability, and to specifically avoid the error you are having (is it always, 3 years down the road, guaranteed by the application (both in the db and the calling app) that you cannot have a condition that will cause this error? One rouge statement gets in and your entire app dies?
我建议使用 RedFilter 的答案来检查是否有多个具有给定 MODEL_ID 的汽车。
如果您绝对确定 CARS.MODEL_ID 是唯一的,则意味着错误消息是通过从 LEDGER_VIEW 中进行选择而生成的 - 因此请尝试在 CARS 上运行不带子查询的等效查询,如下所示:
如果您仍然看到相同的错误 (你应该,如果 CARS.MODEL_ID 是唯一的)你将需要调试 LEDGER_VIEW - 即。检查 LEDGER_VIEW 中返回多行的子查询及其所基于的基础视图。
在大多数形式的 SQL 中,基于视图创建视图是可能的,但这通常是一个坏主意 - 正是因为这个原因。
I suggest using RedFilter's answer to check whether there are multiple cars with a given MODEL_ID.
If you're absolutely certain that CARS.MODEL_ID is unique, then it implies that the error message is generated by selection from LEDGER_VIEW - so try running the equivalent query without the subquery on CARS, like so:
If you still see the same error (you should, if CARS.MODEL_ID is unique) you will need to debug LEDGER_VIEW - ie. check for sub-queries returning multiple rows in LEDGER_VIEW and the underlying views it is based on.
Creating views based on views is possible in most forms of SQL, but it is usually a bad idea - for this very reason.
尝试通过附加 rownum = 1 来强制子查询返回单个结果,如下所示:
它可能会起作用,如果起作用,您将知道您的子查询返回多行,从错误代码来看它应该是。当然,这不是一个解决方案,因此您可能必须修复 cars 表中的数据才能真正解决问题。如果 model_id 再次重复,则保留 rownum = 1 将消除错误,从而防止您注意到问题。
Try forcing your subquery to return a single result by appending rownum = 1, like this:
It will probably work and if it does, you will know that your subquery returns multiple rows, which judging by the error code it should be. Of course this is not a solution so you might have to fix your data in cars table to actually solve the problem. Leaving and rownum = 1 will eliminate the error if model_id is duplicated again, preventing you from noticing the problem.