select right(('000'+cast((min(field)+1) as varchar)),3) from #t a where not exists(select 1 from #t where cast(field as int)=cast(a.field as int)+1)
这是表的自链接,right(('000'+cast((min(field)+1) as varchar)),3)是转换数值型为字符型。
cast(field as int)=cast(a.field as int)+1)表内field是否有连续的下一个数据!
--test:
create table #t(field varchar(10),uname varchar(10))
insert #t
select '001','a'
union all select '002','b'
union all select '005','c'
union all select '006','d'
union all select '007','e'
union all select '010','f'
select * from #t
--select right(('000'+cast((min(field)+1) as varchar)),3) from #t a where not exists(select 1 from #t where cast(field as int)=cast(a.field as int)+1)
insert #t
select (select right(('000'+cast((min(field)+1) as varchar)),3) from #t a where not exists(select 1 from #t where cast(field as int)=cast(a.field as int)+1)),'gg'
drop table #t
create table #t(field varchar(10),uname varchar(10))
insert #t
select '001','a'
union all select '002','b'
union all select '005','c'
union all select '006','d'
union all select '007','e'
union all select '010','f'
select * from #t
--select right(('000'+cast((min(field)+1) as varchar)),3) from #t a where not exists(select 1 from #t where cast(field as int)=cast(a.field as int)+1)
insert #t
select (select right(('000'+cast((min(field)+1) as varchar)),3) from #t a where not exists(select 1 from #t where cast(field as int)=cast(a.field as int)+1)),'gg'
select top 1 a.number from
(select IDENTITY(INT,1,1) as Number , field from table1 order by field) a
where a.number <> cast(a.field as integer)
order by a.number