declare @i as int select @i = isnull(max(id),0) + 1 from tb
SQL code: --自已做标识列的例子,不自动重排编号,而是自动补号:
--创建得到最大id的函数 IF NOT OBJECT_ID('[f_getid]') IS NULL DROP function f_getid GO create function f_getid() returns int as begin declare @id int if not exists(select 1 from tb where id='001') set @id=1 else begin select @id=max(id) from tb if @id is null set @id=1 else begin declare @id1 int select @id1=min(id) from tb a where id<>@id and not exists(select 1 from tb where id=a.id+1)