Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 编写异常处理程序_Sql_Exception_Null_Handler - Fatal编程技术网

Sql 编写异常处理程序

Sql 编写异常处理程序,sql,exception,null,handler,Sql,Exception,Null,Handler,我正在尝试创建一个异常处理程序,以防止插入空值。我还想将它设置为,如果插入的是空值,则原始成本将显示在消息中 我的两张桌子是 create table pitem, (item_id number, item_cost number(10,2) ); 和 这是程序 create or replace procedure update_item_cost (iItemId INTEGER, fNewcost NUMBER) AS fCurCost NUMBER(10,2); missing_

我正在尝试创建一个异常处理程序,以防止插入空值。我还想将它设置为,如果插入的是空值,则原始成本将显示在消息中

我的两张桌子是

create table pitem,
(item_id  number,
item_cost  number(10,2)
);

这是程序

create or replace procedure update_item_cost
(iItemId INTEGER,
fNewcost NUMBER) AS
fCurCost NUMBER(10,2);
missing_cost EXCEPTION;
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;
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.');/*<---heres where I want it to use the original cost of the item saying 'Null cost replaced by original cost of whatever the orignal was*\
     COMMIT;
  WHEN OTHERS THEN
     ROLLBACK;
     INSERT INTO pitem_audit VALUES (iItemId, 'Miscellaneous error.');
     COMMIT;
  END update_item_cost;
/
我的问题是如何编写异常处理程序来防止输入空值..我应该只执行ALTER TABLE语句并添加NOTNULL约束吗?另外,我如何获得pitem_审计表下的消息以显示“Null cost替换为[original item_cost]的原始值”


提前感谢:D

您的问题是?有错误吗?请用您的问题编辑您的问题标题。
create or replace procedure update_item_cost
(iItemId INTEGER,
fNewcost NUMBER) AS
fCurCost NUMBER(10,2);
missing_cost EXCEPTION;
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;
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.');/*<---heres where I want it to use the original cost of the item saying 'Null cost replaced by original cost of whatever the orignal was*\
     COMMIT;
  WHEN OTHERS THEN
     ROLLBACK;
     INSERT INTO pitem_audit VALUES (iItemId, 'Miscellaneous error.');
     COMMIT;
  END update_item_cost;
/
DECLARE
 item_ident number;
 cost number;
BEGIN
 item_ident := 10;
 cost :='';
 update_item_cost(item_ident, cost);
END;
/