设有一个表格如下:
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请问这个触发器应该如何写呢?请各位指教。谢谢!
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请问这个触发器应该如何写呢?请各位指教。谢谢!
ON t_table
before INSERT
AS
BEGIN
delete from t_table where id = new.id
END
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
endInsert into t (ID, Name, Birth) values(2, '小吴', '1979-09-09')select * from tid name birth
----------- ---------- -----------------------
1 小张 1977-01-10 00:00:00.000
2 小吴 1979-09-09 00:00:00.000(2 行受影响)
我用的是SQL Server2000