Table1格式如下:
Col1
1
2
3
4
6
7
8
9
10
13
14
如上所示,col1具有值序列,但是由于某种原因,用户没有插入5、11等等。如何找出序列中的缺失值。这里的序列是1到14,缺失值是5,11。请帮助我。
发布于 2017-08-18 16:51:59
试试这个:
declare @min int
declare @max int
select @min = min(field_ID), @max = max(field_ID) from [Table]
create table #tmp (Field_No int)
while @min <= @max
begin
if not exists (select * from [Table] where field_ID = @min)
insert into #tmp (seq_field) values (@min)
set @min = @min + 1
end
select * from #tmp
drop table #tmp
使用上面的脚本,您将在#tmp表的"ID“列中获得缺少的值。
希望这能对你有所帮助!
https://stackoverflow.com/questions/9328145
复制相似问题