34,575
社区成员
发帖
与我相关
我的任务
分享
create table test(id int identity, name varchar(10))
go
create table test_bak(id int, name varchar(10))
go
create trigger tri_test_ins
on test for insert
as
begin
set xact_abort off
begin try
declare @ids varchar(200) = ''
select @ids = @ids + ltrim(id) + ',' from inserted
select @ids = substring(@ids,1,LEN(@ids) -1)
exec ('insert into test_bak select * from test where id in(' + @ids + ')')
end try
begin catch
RAISERROR('ERROR : backup failure!',10,1)
end catch
end
go
insert into test values('china'),('beijing')
go
print '-- 删除掉表后,再看看效果'
drop table test_bak
insert into test values('usa'),('new york')
go
select * from test
go
drop table test
go
(2 行受影响)
(2 行受影响)
-- 删除掉表后,再看看效果
ERROR : backup failure!
(2 行受影响)
id name
----------- ----------
1 china
2 beijing
3 usa
4 new york
(4 行受影响)