3
A
回答
1
假设你在表中的序列YourNumbersTable 试试这个(SQL Server 2005+):
declare @min int, @max int
select @min = MIN(Id), @max = MAX(Id) from YourNumbersTable
;WITH numbers(id) as
(
SELECT @min id
UNION ALL
SELECT id+1
FROM numbers
WHERE id <= @max
)
SELECT MIN(Numbers.id)
FROM Numbers
LEFT JOIN YourNumbersTable ON Numbers.Id = YourNumbersTable.Id
WHERE YourNumbersTable.Id IS NULL
OPTION(MAXRECURSION 0)
3
declare @T table(Number int)
insert into @T values (1),(2),(3),(13),(15)
select top 1 Number + 1
from @T
where Number + 1 not in (select Number from @T)
order by Number
更新:
使用炭(3)零填充一个版本。
declare @T table(ID char(3))
insert into @T values ('001'),('002'),('003'),('013'),('015')
select top 1 right(1001 + Id, 3)
from @T
where Id + 1 not in (select Id from @T)
order by Id
0
也许我有一个替代解决方案。由于其中的循环,它可能在较宽的数字范围内变慢。
-- prepare a table to have your example
declare @ref table (id int)
insert into @ref (id) values (1), (2), (3), (13), (15)
-- this will return 1
select min(id) from @ref
-- magic happens here
declare @i int, @max int, @returnValue int
select @i = min(id), @max = max(id) from @ref
declare @tmp table (id int)
while @i <= @max
begin
insert into @tmp (id) values (@i)
set @i = @i + 1
end
select @returnValue = min(t.id)
from @tmp t left outer join @ref r on t.id = r.id
where r.id is null
-- this will return 4
select @returnValue
1
试试这个(无连接,无REC-CTE)
declare @T table(n int)
insert into @T values (1),(2),(3),(13),(15)
select max(n)+1 from (
select *,l=n-row_number() over(order by n)
from (
select n from @T
union
select 0 -- what about 0 ??
) as s
) as a
where l=-1
1
这类似于what @szauri has suggested,但没有聚集:
;
WITH ranked AS (
SELECT n, r = ROW_NUMBER() OVER (ORDER BY n)
FROM @T
)
SELECT TOP 1 r
FROM ranked
WHERE n <> r
ORDER BY n
提示:这两种@ szauri和我的解决方案需要SQL Server 2005或更高版本。
相关问题
- 1. 找到最小正值
- 2. MATLAB:fmincon找不到最小值
- 3. Python,lambda,找到最小值
- 4. 试图找到最小值
- 5. TSQL程序找到的最大素数
- 6. 找到[最大值,最小值在列表蟒列表值
- 7. 下划线找到对象值的最小值和最大值
- 8. 找到两个最小的值输入
- 9. Python scipy优化未找到最小值
- 10. 找到坐标点的最小值
- 11. Dijkstra算法多边找到最小值
- 12. 找到最小值但为零
- 13. 找到变量中的最小值?
- 14. 找到活动图表的最小值
- 15. Excel VBA - 找到最小的值列表?
- 16. 查找最大值/最小值
- 17. 查找局部最小值/最大值
- 18. 查找最小值和最大值JAVA
- 19. 如何找到标量的最大值和最小值?
- 20. 用数组找到和,最小值,最大值
- 21. 找到最小值和最大值以及R中
- 22. 如何找到一个数组的最大值和最小值
- 23. 我想在java中找到最大值和最小值
- 24. 需要从用户输入找到最大值和最小值
- 25. 找到最大值和最小值与AWK在特定范围
- 26. 如何在Python中找到最小值和最大值?
- 27. 找到表中数据的最小最大值和平均值
- 28. TSQL:从最低正值
- 29. Prolog程序找到列表中的最小值和下一个最小值
- 30. 查找最大的5值小于1,最小的5个值
这通常工作得很好,但如果'009'被存储为'CHAR(3)',这将如何工作?问题中的前导零使我认为它是一个“CHAR”而不是“INT”字段。只是想知道:) – Seph 2011-12-29 08:15:02
@Seph - 添加了一个char(3)作为数据类型的版本。除了结果值以及存在大量隐式类型转换并且不会在'Number'上使用任何索引的事实之外,没有什么区别。 – 2011-12-29 08:19:59