2017-08-12 4 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を追加してください。同じ。 –

答えて

0

問題は、あなたが'Nick Rimando'cust_namecustomer1テーブルに複数の顧客を持っているということです。私はそれが修正(ES)は

修正プログラムは、あなたが実際に必要なものに依存して数を超える1

になります賭ける

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

を見てみましょう。そしてあなたは実際にあなたが必要とするものを書いていませんでした。だから、いくつかのケースでは...私の心に来て

ケース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 - アゲイン

データモデルを修正し、私はそれは単にあなたとあなたの設計上の考慮事項に任されてどのように、あなたを伝えることはできません。

関連する問題