我想在不使用循环的情况下生成从1到100的数字。我怎么能这样做?
答案 0 :(得分:2)
递归CTE:
with nums as (
select 1 as num
union all
select num+1 from nums where num < 100
)
select num from nums
现在哲学问题是:你的任务是否认为递归是一个循环?
答案 1 :(得分:0)
您可以在T-SQL中执行此操作 - 这是此博客条目中的第二个语句:
http://trycatchfinally.net/2012/03/return-a-list-of-all-dates-between-a-start-and-end-date/
基本上,您正在使用系统表中的行,然后使用RANK()OVER()在您想要的任何范围之间对它们进行编号:
DECLARE @Start INT,
@End INT
SET @Start = 1
SET @End = 100
;WITH numberlist(number)
AS (SELECT RANK() over(order by c1.object_id,
c1.column_id,
c2.object_id,
c2.column_id)
from sys.columns c1
cross
join sys.columns c2)
SELECT @Start + number - 1
FROM numberlist
WHERE @Start + number - 1 <= @End
答案 2 :(得分:0)
创建并执行以下存储过程:
DECLARE @sequence int
UPDATE YourTable SET sequence = sequence + 1, @sequence = sequence + 1
SELECT @sequence AS ‘sequence’