我想创建一个列表
@list = [1], [2], ... ,[8]
基于在sql server中包含数值MAX = 8
的变量。
我该怎么办?
答案 0 :(得分:3)
一种方法
DECLARE @var INT = 8
DECLARE @list VARCHAR(MAX)
;WITH C AS
(
SELECT ROW_NUMBER() OVER (ORDER BY [object_id]) AS n
FROM sys.all_objects
)
SELECT @list =
STUFF((SELECT ', [' + CAST(n AS VARCHAR(5)) + ']' AS [text()]
FROM
(SELECT n FROM C WHERE n <= @var
) x
For XML PATH ('')), 1, 1, '')
SELECT @list