【发布时间】:2015-11-27 07:52:31
【问题描述】:
我创建了一个以 pid 作为参数并返回该 pid 的每月销售统计信息的过程,它使用两个表 products 和 purchase ,其中购买中的 pid 是引用产品中的 pid 的外键。 该过程构建没有错误,并且仅对单行返回执行良好,否则会给出 too_many_rows 异常 我的程序如下:
set serveroutput on
create or replace procedure try(
p_pid in purchases.pid%type) is
p_pname products.pname%type;
p_date varchar2(10);
p_qty_monthly number(5);
p_amnt_monthly number(7,2);
p_avg_price number(7,2);
begin
select pname, p_time, qty_month, amount_month,(amount_month/qty_month) as avg_sale into p_pname,p_date,p_qty_monthly, p_amnt_monthly, p_avg_price
FROM
(select p.pname, to_char(q.ptime,'MON yyyy') p_time,
sum(qty) as qty_month, sum(total_price) as amount_month
from products p, purchases q
where p.pid=q.pid
and q.pid= p_pid
group by pname, to_char(q.ptime,'MON yyyy'));
dbms_output.put_line ('Product name is:'|| p_pname || 'Purchase date is:' || p_date || 'Units sold per month are/is: ' || p_qty_monthly || 'Monthly sale amount: ' || p_amnt_monthly || 'Average sale price is: ' || p_avg_price);
end;
/
show errors
它为某些输入返回不止一行,我怎样才能让它打印所有行而不是给出 too_many_rows 异常?
【问题讨论】:
-
找不到数据意味着您的选择查询没有返回任何用于处理使用异常块的值。
-
该错误表明您的结果中没有
pname为“p001”的行。您的select语句返回 0 行。如果这是预期的,您可以添加一个异常处理程序来捕获no_data_found异常。 -
@JustinCave 查询返回 11 行,其中 1 行与我给出的输入有关,即 p001
-
您传入的是
p_pid。您似乎从未使用该参数是您的代码。您的代码引用了一个从未初始化的局部变量p_pname,因此它为空。没有任何东西等于NULL所以,根据定义,查询不能返回任何东西。 -
您已更改程序,但未更改错误。你还没有得到我假设的
no_data_found异常。我猜想当您尝试将别名为p_time的字符串存储到名为p_date的变量中时会发生数据类型转换,该变量似乎被定义为日期而不是varchar2。如果你解决了这个问题,我希望你会得到一个too_many_rows异常而不是no_data_found异常。select into必须准确返回 1 行。
标签: oracle plsql exception-handling