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;
/