微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

最大递归 100 在语句完成之前已用完

如何解决最大递归 100 在语句完成之前已用完

;WITH UserSequence AS
(
    SELECT 
        LastEnrolledOn,Users,ROW_NUMBER() OVER(ORDER BY LastEnrolledOn) as 'Sequence'
    FROM 
        (select  sum(ct) users,LastEnrolledOn from (select  count(*) as ct,LastEnrolledOn from (select  serialnumber,LastEnrolledOn,count(*) over (partition by [lastenrolledon]) as total 
FROM Devices) devices group by total,LastEnrolledOn )
 devciss group by LastEnrolledOn) WS1enroll
),UserValues AS
(
  SELECT  
     u.Users AS 'UserValue',u.LastEnrolledOn,u.Sequence
  FROM UserSequence u
  WHERE Sequence = 1

  UNION ALL 

  SELECT  
     u.Users + uv.UserValue AS 'UserValue',u.Sequence
  FROM UserSequence u
  INNER JOIN UserValues uv ON u.Sequence = uv.Sequence + 1
)
SELECT
    LastEnrolledOn,Sequence,UserValue AS 'Users'
FROM 
    UserValues
ORDER BY 
    Sequence

我有设备表,其中包含几乎从 2013 年到 2021 年的数据,但不是每个日期之间的数据。我需要计算每一行的总注册人数并添加到下一行总数。查询在几个日期内运行良好。

执行整表获取时。

语句终止。最大递归 100 在语句完成前已用完。

解决方法

您可以使用 option(MAXRECURSION n)。这里的 n 是 0 到 32,767 之间的任何值。 0 表示没有限制,但 32,767。我已将 option(maxrecursion 1000) 添加到您的查询中。

;WITH UserSequence AS
(
    SELECT 
        LastEnrolledOn,Users,ROW_NUMBER() OVER(ORDER BY LastEnrolledOn) as 'Sequence'
    FROM 
        (select  sum(ct) users,LastEnrolledOn from (select  count(*) as ct,LastEnrolledOn from (select  serialnumber,LastEnrolledOn,count(*) over (partition by [lastenrolledon]) as total 
FROM Devices) devices group by total,LastEnrolledOn )
 devciss group by LastEnrolledOn) WS1enroll
),UserValues AS
(
  SELECT  
     u.Users AS 'UserValue',u.LastEnrolledOn,u.Sequence
  FROM UserSequence u
  WHERE Sequence = 1

  UNION ALL 

  SELECT  
     u.Users + uv.UserValue AS 'UserValue',u.Sequence
  FROM UserSequence u
  INNER JOIN UserValues uv ON u.Sequence = uv.Sequence + 1
)
SELECT
    LastEnrolledOn,Sequence,UserValue AS 'Users'
FROM 
    UserValues
ORDER BY 
    Sequence
OPTION (MAXRECURSION 1000)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。