但为什么我不能在这之后再放下一张桌子呢?
insert into a_backup select * from a;
在触发器中,您显式地引用表A,但此时它不存在。
您可以使用动态SQL:
create or replace trigger a_backup_tr
before drop
on database
begin
IF ora_dict_obj_name <> 'A' then
null;
ELSIF ora_dict_obj_name = 'A' and ora_dict_obj_owner = 'TRANEE' then
EXECUTE IMMEDIATE 'insert into tranee.a_backup select * from tranee.a';
ELSE null;
end if;
end;
/
就我个人而言,我不喜欢使用触发器来实现这种机制。还有盲嵌件和
SELECT *
如果模式在将来漂移,可能会失败。也许更好的方法是
Flashback Drop (Recycle Bin)
编辑:
正如@wl_ i所提到的,为了减轻盲插入,您可以在触发器内创建表:
create or replace trigger a_backup_tr
before drop
on database
begin
IF ora_dict_obj_name <> 'A' then
null;
ELSIF ora_dict_obj_name = 'A' and ora_dict_obj_owner = 'TRANEE' then
--TODO: additional check if table already exists
EXECUTE IMMEDIATE 'CREATE TABLE tranee.a_backup AS SELECT * FROM tranee.a';
ELSE null;
end if;
end;
/