日期:2014-05-17 浏览次数:20508 次
create table info
(no int,grade int)
insert into info
select 12,88
-- 建触发器
create trigger tr_info
on info instead of insert
as
begin
delete a
from info a
inner join inserted b on a.no=b.no
insert into info(no,grade)
select no,grade from inserted
end
-- 插入
insert into info values(12,99)
-- 结果
select * from info
/*
no grade
----------- -----------
12 99
(1 row(s) affected)
*/
create TRIGGER t ON info
INSTEAD OF INSERT
as
IF EXISTS (SELECT 1 FROM INSERTED a INNER JOIN info b ON a.[NO]=b.[no] AND a.grade<>b.grade)
UPDATE info SET info.grade=b.grade FROM info INNER JOIN INSERTED b ON info.[NO]=b.[no] AND info.grade<>b.grade
ELSE
INSERT INTO info SELECT * FROM INSERTED