【问题标题】:Exception handler not raising异常处理程序未引发
【发布时间】:2014-06-06 01:57:12
【问题描述】:

**UPDATE 修复了显示的错误,但它仍然无法正常运行。我需要创建一个用户定义的异常处理程序,它不允许将空值作为项目成本插入,但由于某种原因 raise missing_cost 没有运行。我将 item_id 和 item_cost 设置为用户定义,但我认为它没有正确传递?

表格是:

pitem
{item_id number;
 item_name char(20);
 item_desc char(20);
 retail_price number(10,2);
 item_cost number(10,2);
 category char(10);
}

还有……

pitem_audit
{item_id number;
message char(80);
}

提前致谢!

create or replace procedure update_item_cost (iItemId INTEGER, 
fNewcost NUMBER) as fCurCost NUMBER(10,2);
missing_cost EXCEPTION;

item_id pitem.item_id%type :=&item_id;
item_cost pitem.item_cost%type := &item_cost;


begin
    select item_cost into fCurCost from pitem
    where item_id=iItemid;

    if fCurCost IS null then
        raise missing_cost;
    else
        update pitem set item_cost=fNewCost
        where item_id=iItemid;
    end if;

commit;
end;

exception
    when no_data_found then
    insert into pitem_audit
        values(iItemid, 'Invalid Item identifier.');
    commit;
    when missing_cost then
    insert into pitem_audit
        values(iItemid, 'Item Cost is null.');
    commit;
    when others then
    rollback;
    insert into pitem_audit
        values(iItemid, 'Miscellaneous error.');
    commit;
end update_item_cost;
/

【问题讨论】:

  • 在 Oracle SqlPlus 或工作室中,您可以在 create 语句后添加 show errors。它会告诉你错误。不确定您使用的是什么数据库,但它可能有类似的东西。

标签: exception plsql handler


【解决方案1】:

这是一个过程的结构:

procedure <procname>
   (<parameters>)
as
   <declarations>
begin
   <body to execute>
exception
   <exception handlers>
end <procname>;

在你的情况下,你有一个额外的end;

procedure <procname>
   (<parameters>)
as
   <declarations>
begin
   <body to execute>
end; <-- this is incorrect
exception
   <exception handlers>
end <procname>;

【讨论】:

  • 我很愚蠢,我的代码中遗漏了一些东西......想通了谢谢
猜你喜欢
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 2016-03-25
  • 2021-06-29
  • 1970-01-01
相关资源
最近更新 更多