2017-08-12 79 views
-1

過程下面拋出異常單排子查詢在過程返回多於一行

單行子查詢返回多於一個行

而在SQL執行查詢>查詢執行精細和節目導致 如預期

create or replace procedure discount_purchase(cust_name customer1.cust_name%type) 
as 
    amt int; 
    discount int; 
begin 
    select sum(purch_amt) into amt 
    from orders 
    where customer_id=(select customer_id from customer1 where cust_name=cust_name); 
    dbms_output.put_line('Total amount is='||amt); 

    if(amt>=500) then 
     discount:=amt-(0.25*amt); 
     dbms_output.put_line('Discount amount is='||discount); 
    else 
     dbms_output.put_line('NO Discount on='||amt||'Discount only above 500'); 
    end if; 
end; 
/

SQL> EXEC discount_purchase( '尼克·裏曼多');

BEGIN discount_purchase('Nick Rimando'); END; 
* 
ERROR at line 1: 
ORA-01427: single-row subquery returns more than one row 
ORA-06512: at "ARPAN.DISCOUNT_PURCHASE", line 6 
ORA-06512: at line 1 
+0

使用別名並獲得第一行子查詢 –

+0

子查詢返回單個值3002作爲customer_id –

+0

如果您不關心列表中的值或者確定它們是否爲您的子查詢條件,那麼嘗試添加rownum = 1和rownum = 1一樣。 –

回答

0

的問題是,你有多個客戶在customer1表的'Nick Rimando'cust_name。看看

select count(1) 
from customer1 
where cust_name = 'Nick Rimando'; 

我敢打賭,這將導致數大於1

的修復(ES)

的修復取決於你的實際需要。而你其實並沒有寫出你需要的東西。因此,少數病例來我的心......

案例1 - 任意單個客戶的購買與總和 - 所有客戶的購買與該名稱

create or replace procedure discount_purchase(cust_name customer1.cust_name%type) 
as 
    amt int; 
    discount int; 
begin 
    select sum(purch_amt) into amt 
    from orders 
    where customer_id in (select customer_id from customer1 where cust_name=cust_name); 
    dbms_output.put_line('Total amount is='||amt); 

    if(amt>=500) then 
     discount:=amt-(0.25*amt); 
     dbms_output.put_line('Discount amount is='||discount); 
    else 
     dbms_output.put_line('NO Discount on='||amt||'Discount only above 500'); 
    end if; 
end; 
/

案例2的總和命名

create or replace procedure discount_purchase(cust_name customer1.cust_name%type) 
as 
    amt int; 
    discount int; 
begin 
    select sum(purch_amt) into amt 
    from orders 
    where customer_id = (select customer_id from customer1 where cust_name=cust_name and rownum <= 1); 
    dbms_output.put_line('Total amount is='||amt); 

    if(amt>=500) then 
     discount:=amt-(0.25*amt); 
     dbms_output.put_line('Discount amount is='||discount); 
    else 
     dbms_output.put_line('NO Discount on='||amt||'Discount only above 500'); 
    end if; 
end; 
/

案例3 - 「第一個」 客戶購買的總和與該名稱

create or replace procedure discount_purchase(cust_name customer1.cust_name%type) 
as 
    amt int; 
    discount int; 
begin 
    select sum(purch_amt) into amt 
    from orders 
    where customer_id = (select min(customer_id) from customer1 where cust_name=cust_name); 
    dbms_output.put_line('Total amount is='||amt); 

    if(amt>=500) then 
     discount:=amt-(0.25*amt); 
     dbms_output.put_line('Discount amount is='||discount); 
    else 
     dbms_output.put_line('NO Discount on='||amt||'Discount only above 500'); 
    end if; 
end; 
/

案例4 - 你選擇,你實現

我不能告訴你怎麼樣,那是完全取決於你和你的設計考慮。

案例5 - 修復你的數據模型

同樣,我不能告訴你怎麼樣,那是完全取決於你和你的設計考慮。