oracle 错误的 ORA-01427: 单行子查询返回多于一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3804850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 21:35:13  来源:igfitidea点击:

Erroneous ORA-01427: single-row subquery returns more than one row

sqloracleora-01427

提问by aw crud

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:

我在执行查询时收到错误 [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

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.

它打破了嵌套选择。我知道视图和此查询中的逻辑都是正确的,并且嵌套选择不可能返回多于一行。CARS 表的 MODEL_ID 是一个唯一字段。如果我在没有嵌套选择的情况下执行查询,它不会返回此错误。

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.

LEDGER_VIEW 是建立在另一个视图之上的视图。这些堆叠视图在 Oracle 10g 中是否有可能出错?我不知道如何调试这个问题。

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.

编辑:这是真正奇怪的事情。正如我所说,LEDGER_VIEW 建立在另一个视图之上。作为测试,我将嵌套视图的 SQL 直接复制到 LEDGER_VIEW 的 SQL 的 SQL 中,代替嵌套视图,并且它没有错误地返回(正如预期的那样)。这似乎向我证实了嵌套视图或嵌套视图 + 数据库链接的组合存在一些错误行为。

回答by RedFilter

Your subquery is returning multiple rows. Use the query below to find out which MODELIDvalues in the Cartable are duplicated:

您的子查询返回多行。使用下面的查询找出表中的哪些MODELIDCar是重复的:

select MODELID as CarsModelID, count(*) as Count
from cars 
where MODELID in (
    select MODEL_ID
    from LEDGER_VIEW  
    WHERE LV.PRICE < 500 
)
group by MODELID
having count(*) > 1

回答by Harrison

I am unable to recreate via a creation of a stacked view. (althoug RedFilters will find the culprit)

我无法通过创建堆叠视图来重新创建。(尽管 RedFilters 会找到罪魁祸首)

    CREATE TABLE t1
    (
        t1_id NUMBER        ,
        txt   VARCHAR2( 50 ),
        CONSTRAINT t1_pk PRIMARY KEY( t1_id )
    ) ;


    CREATE TABLE t2
    (
        t2_id NUMBER                      ,
        t1_id NUMBER                      ,
        price NUMBER( 10, 4 )             ,
        CONSTRAINT t2_pk PRIMARY KEY( t2_id ),
        CONSTRAINT t2_fk FOREIGN KEY( t1_id ) REFERENCES t1( t1_id )
    );

    insert into t1(t1_id, txt) values(1,'fit');
    insert into t1(t1_id, txt) values(2,'focus');
    insert into t1(t1_id, txt) values(3,'golf');
    insert into t1(t1_id, txt) values(4,'explorer');
    insert into t1(t1_id, txt) values(5,'corolla');

insert into t2(t2_id, t1_id, price) values(1,1,17000);
insert into t2(t2_id, t1_id, price) values(2,2,16000);
insert into t2(t2_id, t1_id, price) values(3,3,22000);
insert into t2(t2_id, t1_id, price) values(4,4,31000);
insert into t2(t2_id, t1_id, price) values(5,5,17000);


create view t1_view as select * from t1;
create view t2_view as select * from t2;
create view t_stacked_view as 
  select t1_view.txt ,
       t2_view.price ,
         t1_view.t1_id
    from t1_view 
          left join
          t2_view 
            on t1_view.t1_id = t2_view .t1_id
    ;   


--stacked view test
select t1_view.txt ,
       (select t_stacked_view.price 
            from t_stacked_view 
             where t1_view.t1_id = t_stacked_view .t1_id) price
    from t1_view ;

--or better yet, just drop the row level query
select t1_view.txt ,
       t2_view.price
    from t1_view 
          left join
          t2_view 
            on t1_view.t1_id = t2_view .t1_id
    ; 

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?

但这引出了一个问题,为什么要在这里进行行级查询?虽然 10g 应该对它们进行相同的优化,但我一直发现编写如下查询更容易,无论是为了可读性、可维护性,还是为了避免您遇到的错误(是否总是如此,3 年后,由应用程序(在数据库和调用应用程序中)你不能有一个会导致这个错误的条件?一个胭脂语句进入并且你的整个应用程序死亡?

    SELECT LV.PRICE,
            c.model_name
FROM LEDGER_VIEW LV
      LEFT /* OR INNER */ JOIN CARS C 
       ON C.MODEL_ID = LV.MODEL_ID
WHERE LV.PRICE < 500

回答by Harrison

I suggest using RedFilter's answer to check whether there are multiple cars with a given MODEL_ID.

我建议使用 RedFilter 的答案来检查是否有多个具有给定 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:

如果您绝对确定 CARS.MODEL_ID 是唯一的,那么这意味着错误消息是通过从 LEDGER_VIEW 中选择生成的 - 所以尝试在没有 CARS 子查询的情况下运行等效查询,如下所示:

SELECT LV.PRICE
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500

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.

如果您仍然看到相同的错误(您应该看到,如果 CARS.MODEL_ID 是唯一的)您将需要调试 LEDGER_VIEW - 即。检查在 LEDGER_VIEW 及其所基于的基础视图中返回多行的子查询。

Creating views based on views is possible in most forms of SQL, but it is usually a bad idea - for this very reason.

在大多数形式的 SQL 中都可以基于视图创建视图,但这通常是一个坏主意 - 正是出于这个原因。

回答by vls

Try forcing your subquery to return a single result by appending rownum = 1, like this:

尝试通过附加 rownum = 1 来强制您的子查询返回单个结果,如下所示:

SELECT LV.PRICE,
(SELECT C.MODEL_NAME FROM CARS C WHERE C.MODEL_ID = LV.MODEL_ID AND ROWNUM = 1) as MODEL_NAME
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500

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.

它可能会起作用,如果它起作用,您将知道您的子查询返回多行,根据错误代码判断它应该是。当然,这不是解决方案,因此您可能必须修复汽车表中的数据才能真正解决问题。如果 model_id 再次重复,Leaving and rownum = 1 将消除错误,防止您注意到问题。

回答by user3774053

select
                                    a.account_number,
                                    a.party_id,
                                    a.TRX_NUMBER,
                                    a.trx_date,
                                    a.order_number,
                                    adv.unapplied_amt,
                                    a.Finance,
                                    a.customer_name,a.PARTY_NAME,
                                    a.customer_number,a.contact_number,
                                    a.name,
                                    a.Aging,
                                    a.transaction_type,
                                    a.exec_name,
                                    a.team_leader,
                                    sum(a.O_SAmount),
                                    (case when (trunc(sysdate) - trunc(a.trx_date)) <=:ag1 then sum(a.O_SAmount) else 0 end ) bucket1,--"<" || :ag1,
                                    (case when (trunc(sysdate) - trunc(a.trx_date)) between :ag1+1 and :ag2 then sum(a.O_SAmount) else 0 end ) bucket2,--:ag1+1 || "to" || :ag2,
                                    (case when (trunc(sysdate) - trunc(a.trx_date)) between :ag2+1 and :ag3 then sum(a.O_SAmount) else 0 end ) bucket3,--:ag2+1 || "to" || :ag3,
                                    (case when (trunc(sysdate) - trunc(a.trx_date)) >:ag3 then sum(a.O_SAmount) else 0 end ) bucket4,
                                    :AS_ON_date
from
(select distinct hca.account_number,hp.party_id,--rcta.CUSTOMER_TRX_ID,
--rcta.trx_number,rcta.trx_date,apsa.due_date,
(
select distinct
                            --ooha.order_number,
                            rcta.trx_number
                            --to_char(rcta.trx_date,'DD-MON-YYYY') trx_date
from                            
                            ra_customer_trx_all rcta,
                            oe_order_headers_all ooh,
                            oe_order_lines_all oola,
                            --ra_customer_trx_all rcta,
                            ra_customer_trx_lines_all rctla,
                            ra_cust_trx_types_all rctta
                            --ra_customer_trx_lines_all rctl
where 1=1
                AND ooh.header_id = oola.header_id
                   --AND ooh.order_number = '111111010101698'
                   AND ooh.order_number=oohA.order_number
                   AND TO_CHAR (ooh.order_number) = rcta.ct_reference
                   AND rcta.customer_trx_id = rctla.customer_trx_id
                   AND rctla.inventory_item_id = oola.inventory_item_id
                   and rcta.CUST_TRX_TYPE_ID = rctta.cust_trx_type_id
                   and rcta.org_id = rctta.org_id
                   and rctta.type like 'INV'
                   and oola.ordered_item LIKE 'MV%' 
                   AND oola.attribute3 = 'Y'
                   AND ooh.flow_status_code <> 'ENTERED'
                   AND oola.flow_status_code <> 'CANCELLED'
)TRX_NUMBER,
(select distinct
--ooha.order_number,
--rcta.trx_number
rcta.trx_date
from
            ra_customer_trx_all rcta,
            oe_order_headers_all ooh,
            oe_order_lines_all oola,
            --ra_customer_trx_all rcta,
            ra_customer_trx_lines_all rctla,
            ra_cust_trx_types_all rctta
            --ra_customer_trx_lines_all rctl
where 1=1
               AND ooh.header_id = oola.header_id
               --AND ooh.order_number = '111111010101698'
               AND ooh.order_number=oohA.order_number
               AND TO_CHAR (ooh.order_number) = rcta.ct_reference
               AND rcta.customer_trx_id = rctla.customer_trx_id
               AND rctla.inventory_item_id = oola.inventory_item_id
               and rcta.CUST_TRX_TYPE_ID = rctta.cust_trx_type_id
               and rcta.org_id = rctta.org_id
               and rctta.type like 'INV'
               and oola.ordered_item LIKE 'MV%' 
               AND oola.attribute3 = 'Y'
               AND ooh.flow_status_code <> 'ENTERED'
               AND oola.flow_status_code <> 'CANCELLED'
)TRX_Date,
rcta.INTERFACE_HEADER_ATTRIBUTE1 order_number,
ooha.attribute10 Finance,
f.customer_name,HP.PARTY_NAME,
TO_NUMBER(f.customer_number)customer_number,hp.primary_phone_number contact_number,--csi.incident_number,
--cii.instance_number,
haou.name,
--sum(acr.amount) Advance,--rcta.CUST_TRX_TYPE_ID,--acr.cash_receipt_id,
--sum(abs((apsa.AMOUNT_DUE_REMAINING-nvl(acr.amount,0)))) "O_SAmount",
apsa.AMOUNT_DUE_REMAINING O_SAmount,
--sum(abs((apsa.AMOUNT_DUE_REMAINING))) "O_SAmount",
round(months_between(sysdate,rcta.trx_date)*30) Aging,
--(case when ((round(months_between(sysdate,rcta.trx_date)*30)>=0) or (round(months_between(sysdate,rcta.trx_date)*30)<:aging1)) then apsa.AMOUNT_DUE_REMAINING end) "0 TO 30"
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) <=:ag1 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket1,--"<" || :ag1,
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) between :ag1+1 and :ag2 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket2,--:ag1+1 || "to" || :ag2,
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) between :ag2+1 and :ag3 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket3,--:ag2+1 || "to" || :ag3,
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) >:ag3 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket4,
--apsa.amount_due_original,
--TO_NUMBER(apsa.AMOUNT_DUE_REMAINING)AMOUNT_DUE_REMAINING,
rctta.name transaction_type,
PAPF.full_name||'-'||PAPF.EMPLOYEE_NUMBER exec_name,
ooha.attribute9 team_leader,
:AS_ON_date
from                    ra_customer_trx_all rcta,
                        oe_order_headers_all ooha,
                        hz_cust_accounts hca,
                        hz_parties hp,
                        --cs_incidents_all_b csi,
                        --csi_item_instances cii,
                        hr_all_organization_units haou,
                        ar_cash_receipts_all acr,
                        ar_receivable_applications_all aaa,
                        ra_cust_trx_types_all RCTTA,
                        hr.per_all_people_f papf,
                        ar_customers f,
                        ar_payment_schedules_all apsa,
                        jtf.JTF_RS_SALESREPS jrs
where 1=1
                        --and INTERFACE_HEADER_ATTRIBUTE1 like '111111060100538'
                        --and INTERFACE_HEADER_ATTRIBUTE1 like '111111010105402'
                        --and INTERFACE_HEADER_ATTRIBUTE1 like '111111010102791'
                        and rcta.ct_reference(+)=TO_CHAR(ooha.order_number)
                        AND f.customer_id = (rcta.bill_to_customer_id) 
                        and f.customer_id=hca.cust_account_id
                        and hca.party_id=hp.party_id
                        and haou.organization_id=rcta.INTERFACE_HEADER_ATTRIBUTE10
                        --and hp.party_id=cii.owner_party_id
                        --and csi.inventory_item_id=cii.inventory_item_id
                        --and csi.inv_organization_id=haou.organization_id
                        --and haou.organization_id=nvl(:location,haou.organization_id)
                        and ooha.SHIP_FROM_ORG_ID=nvl(:location,haou.organization_id)
                        AND RCTTA.NAME like :transaction_type||'%'
                        --decode(:org_id,null,null,(select name from ar_cash_receipts_all where organization_id = :org_id)) ||'%')
                        and rcta.trx_date<=to_date(:AS_ON_date)
                        --AND RCTTA.NAME=NVL(:TRANS_TYPE,RCTTA.NAME)
                        and rcta.org_id=nvl(:org_id,rcta.org_id)
                        --and f.customer_name like 'VIKAS SATAV'
                        and aaa.applied_customer_trx_id(+)=rcta.customer_trx_id
                        and aaa.cash_receipt_id=acr.cash_receipt_id(+)
                        and rcta.status_trx like 'OP'
                        and rcta.CUST_TRX_TYPE_ID=rctta.CUST_TRX_TYPE_ID
                        and apsa.CUSTOMER_TRX_ID=rcta.CUSTOMER_TRX_ID
                        and TO_NUMBER(apsa.AMOUNT_DUE_REMAINING) >0
                        --and hp.party_id=papf.party_id(+)
                        and jrs.salesrep_id = ooha.SALESREP_ID
                        and jrs.ORG_ID = ooha.ORG_ID
                        and jrs.PERSON_ID = papf.PERSON_ID(+)
) a,
(
select
b.order_number,
sum(b.AMOUNT_APPLIED) unapplied_amt
from
                        (select distinct to_char(ooha.order_number) order_number,ara.* from 
                        oe_order_headers_all ooha,
                        oe_payments oe,
                        ar_receivable_applications_all ara
where                    1=1--ooha.order_number = :p_order_num
                        and oe.header_id=ooha.header_id
                        and ara.PAYMENT_SET_ID=oe.PAYMENT_SET_ID
                        and ara.DISPLAY='Y'
                        and (ara.STATUS like 'OTHER ACC' or ara.STATUS like 'UNAPP') --or ara.STATUS like 'ACC')
                        ) b
group by b.order_number
) adv
where                       adv.order_number(+)=a.order_number
group by                    a.account_number,
                            a.party_id,
                            a.TRX_NUMBER,
                            a.trx_date,
                            a.order_number,
                            adv.unapplied_amt,
                            a.Finance,
                            a.customer_name,a.PARTY_NAME,
                            a.customer_number,a.contact_number,
                            a.name,
                            a.Aging,
                            a.transaction_type,
                            a.exec_name,
                            a.team_leader
order by a.Aging desc