34,587
社区成员
发帖
与我相关
我的任务
分享
create table tb(id int,sname varchar(10))
insert into tb
select 1,'a' union all
select 2,'b'
go
create trigger update_tb on tb
instead of insert,update
as
begin
if exists (select 1 from deleted)
begin
raiserror('update!',16,1)
update a
set a.sname = b.sname
from tb a join inserted b on a.id = b.id
end
else
begin
raiserror('insert!',16,1)
insert into tb
select id,sname from inserted
end
end
go
-- 1
insert into tb
select 3,'c'
select * from tb
-- 2
update tb
set sname = 'bb'
where id = 3
select * from tb
drop trigger update_tb
drop table tb
/***************************
消息 50000,级别 16,状态 1,过程 update_tb,第 15 行
insert!
(1 行受影响)
(1 行受影响)
id sname
----------- ----------
1 a
2 b
3 c -- insert
(3 行受影响)
消息 50000,级别 16,状态 1,过程 update_tb,第 8 行
update!
(1 行受影响)
(1 行受影响)
id sname
----------- ----------
1 a
2 b
3 bb --update
(3 行受影响)