如何在触发器内使用delete

时间:2014-10-16 18:45:05

标签: sql-server triggers

我必须手动使用删除操作 delete from t where=col=1,因为如果我将其插入触发器内部则不起作用。即删除操作不会保存到deleted表中。

alter trigger trig1 on t
after insert,delete as
set col = case when col1=1.8 then 2

delete from t where col=1;

insert into history(col,col1)
select *
from deleted 

表格历史记录无法插入。

1 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是将您要执行的三项操作分为三个部分,两个触发器和一个存储过程。

create trigger trig1 on t
after insert as
set col = case when col1=1.8 then 2

create procedure p(@t sysname) as
begin
if exists (select * from t where col=1)
delete from t where col=1;
end

create trigger trig2 on t
after delete as
insert into history(col,col1)
select *
from deleted 

插入值时,您必须执行p