--创建两张测试表
create table table1([id] int primary key, [value] varchar(100))
create table table2([id] int primary key, [value] varchar(100))
--向table1插入测试数据
declare @i int set @i = 1
while @i <= 10
begin
insert into table1 values(@i, cast(@i as varchar))
set @i = @i + 1
end
--创建trigger
create trigger triggerTable1Del on table1 for delete as
begin
declare @id int, @value varchar(100)
declare cur_del cursor local forward_only for --define cursor
select [id], [value] from deleted
open cur_del --open cursor
fetch next from cur_del into @id, @value
while @@fetch_status = 0
begin
insert into table2([id], [value]) values(@id, @value)
fetch next from cur_del into @id, @value
end
close cur_del --释放游标
deallocate cur_del
end
--测试删除数据表table1
delete from table1
--查看数据是否进入table2
select * from table2