创建方法
create function f_getid() returns int as begin return(select isnull(max(contentID),0)+1 from contentsTest) end go
创建表
create table contentsTest(contentID int default dbo.f_getid() primary key,contentName varchar(10)) go
创建触发器
create trigger t_delete on contentsTest AFTER delete as declare @id int,@mid int select @mid=min(contentID),@id=@mid-1 from deleted update contentsTest set contentID=@id,@id=@id+1 where contentID>@mid go
搞定! contentsTest表中的contentID就会自动增长了,而且删除其中的一条后会自动重置从1开始。