生成1到300個數字的方法 方法一 cross join 方法二 while迴圈 方法三 CTE遞歸 ...
生成1到300個數字的方法
方法一
cross join
SELECT aa.[num]+bb.[num]+cc.[num] FROM (SELECT 0 num UNION ALL SELECT 1 num UNION ALL SELECT 2 num UNION ALL SELECT 3 num UNION ALL SELECT 4 num UNION ALL SELECT 5 num UNION ALL SELECT 6 num UNION ALL SELECT 7 num UNION ALL SELECT 8 num UNION ALL SELECT 9 num ) aa CROSS JOIN (SELECT 0 num UNION ALL SELECT 10 num UNION ALL SELECT 20 num UNION ALL SELECT 30 num UNION ALL SELECT 40 num UNION ALL SELECT 50 num UNION ALL SELECT 60 num UNION ALL SELECT 70 num UNION ALL SELECT 80 num UNION ALL SELECT 90 num ) bb CROSS JOIN (SELECT 0 num UNION ALL SELECT 100num UNION ALL SELECT 200 num ) cc ORDER BY 1
方法二
while迴圈
DECLARE @i INT DECLARE @tb TABLE(a INT) SET @i=1 INSERT INTO @tb ( [a] ) VALUES ( @i -- a - int ) WHILE (@i<300) BEGIN SET @i=@i+1 INSERT INTO @tb ( [a] ) VALUES ( @i -- a - int ) END SELECT * FROM @tb
方法三
CTE遞歸
;with cte_temp as ( select 0 as id union all select id+1 from cte_temp where id<301 ) select id from cte_temp option (maxrecursion 301);