用cte创建while循环 [英] create while loop with cte
本文介绍了用cte创建while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从while循环中创建sql server cte
这样的循环
how to create sql server cte from a while loop
my loop like this
declare @ind as int
declare @code as nvarchar
set @ind = 0
while @ind < 884
begin
select @ind = @ind + 1
--here execute Procedure
--and set return value to variable
set @code = cast (@ind as nvarchar)
end
推荐答案
需要表:
;WITH Sec(Number) AS
(
SELECT 0 AS Number
UNION ALL
SELECT Number + 1
FROM Sec
WHERE Number < 884
)
SELECT * FROM Sec
OPTION(MAXRECURSION 0)
如果您需要一个字符串:
If you need one string:
;WITH Sec(Number) AS
(
SELECT 0 AS Number
UNION ALL
SELECT Number + 1
FROM Sec
WHERE Number < 884
)
SELECT STUFF(a.[Str], 1, 1, '')
FROM
(
SELECT (SELECT ',' + CAST(Number AS NVARCHAR(3))
FROM Sec
FOR XML PATH(''), TYPE
).value('.','varchar(max)') AS [Str]
) AS a
OPTION(MAXRECURSION 0)
这篇关于用cte创建while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文