生成1到300个数字的方法

2018-06-17 19:47:34来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

生成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);

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:【PostgreSQL】PostgreSQL的安装

下一篇:MongoDB学习笔记—权限管理