【发布时间】:2016-12-26 11:25:52
【问题描述】:
我想覆盖产生错误的消息 (ORA-02292)。 就是这样的消息
ORA-02292: integrity constraint (IVANKA.FK_SUPPLIER) violated - child record found
我想要一个触发器来覆盖上面的消息到他的例子中(我的覆盖:))
我试过这样做
第一次建表
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier (supplier_id)
);
然后插入数据
INSERT INTO supplier
(supplier_id, supplier_name, contact_name)
VALUES (1000, 'Microsoft', 'Bill Gates');
INSERT INTO products
(product_id, supplier_id)
VALUES (50000, 1000);
然后触发
create or replace trigger sup_z
after delete on supplier
for each row
declare
v_error_constraint exception;
pragma exception_init(v_error_constraint, -2292);
begin
null;
exception
When v_error_constraint then
raise_application_error(-20001,
'My ovvervide:)');
End;
然后执行删除以生成消息
DELETE from supplier
WHERE supplier_id = 1000
但我在触发器中看不到我的消息我看到了
ORA-02292: integrity constraint (IVANKA.FK_SUPPLIER) violated - child record found
你能帮帮我吗?我做错了什么?
【问题讨论】:
-
在触发器触发之前抛出约束冲突。
-
您的 proc 违反了父子约束,这种违反可能会留下孤立的记录。您应该在删除父记录之前删除子记录,或者删除约束。
-
我知道如何通过约束来判断错误,但我只需要打印另一条消息
-
如果您想为最终用户显示更有意义的消息(我认为这是主要原因),您可以将 DML 语句放在 PL/SQL 块中,并在其中捕获异常部分该异常,或创建数据库事件触发器(将是一个矫枉过正,只是指出的可能性)。
-
我认为您不了解触发器的工作原理。您不能使用触发器来“拦截”异常。并且忽略异常对您没有任何好处,因为数据库仍然会拒绝提交您的更改。如果您碰巧不喜欢约束违规,则无法选择忽略它们 - 您必须编写代码,在给定数据库中存在的约束的情况下正常工作。祝你好运。
标签: sql oracle oracle11g triggers oracle10g