将COLUMNS添加到由UTLEXCPT.SQL脚本创建的EXCEPTIONS表

时间:2017-08-25 18:24:40

标签: sql oracle plsql

我知道@?/ rdbms / admin / utlexcpt可以用来创建EXCEPTIONS表。该表用于声明如下:

ALTER TABLE my_tab enable constraint my_tab_fk1 exceptions into MY_EXCEPTIONS

我想通过添加新列来自定义MY_EXCEPTIONS表。但是,然后执行上面的脚本会出现以下错误:

 ORA-00947: not enough values.

这是可以理解的,因为Oracle期望MY_EXCEPTIONS表只有预定义的结构。

是否有解决方法以便将数据失败的EXCEPTIONS捕获到包含其他列的表中?

1 个答案:

答案 0 :(得分:1)

您可以通过视图实现此目的。如果您的父表和子表是这样的:

create table my_parent_tab (id number primary key);
create table my_tab (id number,
  fk_id number,
  constraint my_tab_fk1 foreign key (fk_id)
    references my_parent_tab (id) disable
);

您基于utlexcpt.sql脚本创建了例外表,并附加了一列:

create table my_exceptions(row_id rowid,
  owner varchar2(30),
  table_name varchar2(30),
  constraint varchar2(30),
  date_inserted date default sysdate -- extra column
);

然后您可以在该表上创建一个视图,仅用于异常处理程序所期望的列:

create view my_exceptions_view as
select row_id, owner, table_name, constraint
from my_exceptions;

使用替代触发器填充表,提供显式值或回复额外列的默认值:

create trigger my_exception_trigger
instead of insert on my_exceptions_view
for each row
begin
  insert into my_exceptions (row_id, owner, table_name, constraint)
  values (:new.row_id, :new.owner, :new.table_name, :new.constraint);
end;
/

然后在禁用约束时添加包含一些无效的数据:

insert into my_parent_tab (id) values (1);
insert into my_tab (id, fk_id) values (1, 1);
insert into my_tab (id, fk_id) values (2, 2);

然后您可以启用约束并指向视图而不是表:

alter table my_tab enable constraint my_tab_fk1
  exceptions into my_exceptions_view; -- use view not underlying table

ORA-02298: cannot validate (MY_SCHEMA.MY_TAB_FK1) - parent keys not found
02298. 00000 - "cannot validate (%s.%s) - parent keys not found"
*Cause:    an alter table validating constraint failed because the table has
           child records.
*Action:   Obvious

,异常表显示信息,包括额外列:

select * from my_exceptions;

ROW_ID             OWNER                          TABLE_NAME                     CONSTRAINT                     DATE_INSERTED      
------------------ ------------------------------ ------------------------------ ------------------------------ -------------------
AABiK8AAEAAAAAzAAB MY_SCHEMA                      MY_TAB                         MY_TAB_FK1                     2017-08-25 20:20:14