我需要一种有效的方法来传递参数[StartingNumber]并从[StartingNumber]开始按顺序计数,直到找到丢失的数字。
我使用以下sql获取下一个数字:
DECLARE @StartOffset int
SET @StartOffset = 23
; With Missing as (
select @StartOffset as N where not exists(
select * from [QUEUE] where QueueNum = @StartOffset AND ismelutash = 1)
), Sequence as
( select @StartOffset as N from [QUEUE] where QueueNum = @StartOffset
union all
select b.QueueNum from [QUEUE] b inner join Sequence s
on b.QueueNum = s.N + 1 and b.ismelutash = 1
)
select COALESCE((select N from Missing),(select MAX(N)+1 from Sequence))
它已经工作了一段时间,但现在当我运行它时,我得到'语句终止。在语句完成之前,最大递归100已经耗尽。
有人有什么想法吗?感谢
编辑:
我添加了maxrecursion但它只是加载并且不返回数据:
DECLARE @StartOffset int
SET @StartOffset = 50
DECLARE @isMelutash int
SET @isMelutash = 0
; With QueueFilters as (
select queuenum from queue where ismelutash = 1
), Missing as (
select @StartOffset as N where not exists(select * from QueueFilters where queuenum = @StartOffset)
), Sequence as (
select @StartOffset as N from QueueFilters where queuenum = @StartOffset
union all
select b.queuenum from QueueFilters b inner join Sequence s on b.queuenum = s.N + 1
)
select COALESCE((select N from Missing ),(select MAX(N)+1 from Sequence ) )
**OPTION(MAXRECURSION 150)**
答案 0 :(得分:3)
您可以使用当前代码中的MAXRECURSION选项
然而,没有必要逐行递归(使用Itzik Ben-Gan的方法)。这将检测队列中没有行的情况,ismelutash = 1,因为它使用Tally表作为参考序列
;WITH
Pass0 as (select 1 as C union all select 1), --2 rows
Pass1 as (select 1 as C from Pass0 as A, Pass0 as B),--4 rows
Pass2 as (select 1 as C from Pass1 as A, Pass1 as B),--16 rows
Pass3 as (select 1 as C from Pass2 as A, Pass2 as B),--256 rows
Pass4 as (select 1 as C from Pass3 as A, Pass3 as B),--65536 rows
Pass5 as (select 1 as C from Pass4 as A, Pass4 as B),--4,294,967,296 rows
Tally as (select row_number() over(order by C) as Number from Pass5)
select TOP 1
Number
from
Tally T
LEFT JOIN --corrected, oops.
[QUEUE] Q ON T.Number = Q.QueueNum AND Q.ismelutash = 1
where
T.Number >= @StartOffset AND T.Number <= 1000000
AND
Q.QueueNum IS NULL
ORDER BY
T.Number
编辑:
MAXRECURSION的原始代码提示在最后。 This link也更好
DECLARE @StartOffset int
SET @StartOffset = 23
; With Missing as (
select @StartOffset as N where not exists(
select * from [QUEUE] where QueueNum = @StartOffset AND ismelutash = 1)
), Sequence as
( select @StartOffset as N from [QUEUE] where QueueNum = @StartOffset
union all
select b.QueueNum from [QUEUE] b inner join Sequence s
on b.QueueNum = s.N + 1 and b.ismelutash = 1
)
select COALESCE((select N from Missing),(select MAX(N)+1 from Sequence))
OPTION (MAXRECURSION 0)
答案 1 :(得分:2)
我相信这是等价的,它不使用递归:
SELECT COALESCE(MIN(QueueNum)
, (SELECT Max(QueueNum) + 1
FROM [Queue]
WHERE QueueNum > @StartOffset)
, @StartOffset)
FROM [QUEUE]
WHERE QueueNum >= @StartOffset
AND ismelutash != 1