Oracle10g 单行子查询在过程中返回多行
下面的过程引发异常 单行子查询返回多行 在sql上执行query>query时执行fine并显示结果 果然Oracle10g 单行子查询在过程中返回多行,oracle10g,Oracle10g,下面的过程引发异常 单行子查询返回多行 在sql上执行query>query时执行fine并显示结果 果然 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=(
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折扣购买(“Nick Rimando”)
问题在于
customer1
表中有多个客户的cust\u name
为'Nick Rimando'
。看看
select count(1)
from customer1
where cust_name = 'Nick Rimando';
我打赌它将导致一个大于1的数字
修复程序
修复取决于您实际需要的内容。实际上你没有写你需要的东西。所以,我想到了一些案例
案例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;
/
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;
/
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;
/
案例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 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;
/
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;
/
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-修复数据模型
同样,我无法告诉您如何操作,这完全取决于您和您的设计考虑。使用别名并获取子查询的第一行sub query返回单值3002 as customer\u id尝试将和rownum=1添加到子查询条件中,如果您不关心列表中的值或确保它们相同,请参阅