日期:2014-05-18 浏览次数:20418 次
举个简单的例子 declare @rowCount int select @rowCount=COUNT(*) from tb if @rowCount<3 begin insert into tb select GETDATE() end else begin delete from tb where updateDate =(select MIN(updateDate) from tb) insert into tb select GETDATE() end
------解决方案--------------------
create table tab (id int,name varchar(32)) go insert tab select 1,'sdf' union all select 2,'aerwe' union all select 3,'werwerf' go create trigger instead_tri_name on tab instead of insert as begin declare @id int,@name varchar(32) select @id=id,@name=name from inserted declare @count int,@curr_first int select @count=count(1) from tab select @curr_first=id from tab if @count=3 begin delete from tab where id= (select min(id) from tab) insert tab select @id,@name end else begin insert tab select @id,@name end end
------解决方案--------------------
那就不创建触发器,判断以后在插入数据