[菜鸟问题]请教如何用触发器实现以下的功能
设有一个表格如下:
ID Name Birth
1 小张 1977-01-10
2 小李 1978-09-09
当我插入以下的数据时:
Insert into [Table] (ID, Name, Birth) values(2, '小吴 ', '1979-09-09 ')
触发器能把原数据表中所有ID为2的记录删掉,只保存新增加的记录,即运行后的结果:
ID Name Birth
1 小张 1977-01-10
2 小吴 1979-09-09
请问这个触发器应该如何写呢?请各位指教。谢谢!
------解决方案--------------------CREATE TRIGGER tri_test
ON t_table
before INSERT
AS
BEGIN
delete from t_table where id = new.id
END
------解决方案--------------------create table t(id int,name varchar(10),birth datetime)
insert into t select 1 , '小张 ' , '1977-01-10 '
insert into t select 2 , '小李 ' , '1978-09-09 '
create trigger roy11 on t
for insert
as
begin
delete t from t,inserted i
where t.id=i.id
insert into t select * from inserted
end
Insert into t (ID, Name, Birth) values(2, '小吴 ', '1979-09-09 ')
select * from t
id name birth
----------- ---------- -----------------------
1 小张 1977-01-10 00:00:00.000
2 小吴 1979-09-09 00:00:00.000
(2 行受影响)