你的位置:首页 > 数据库

[数据库]生成1到300个数字的方法


生成1到300个数字的方法

 

方法一

cross join

SELECT aa.[num]+bb.[num]+cc.[num] FROM (SELECT 0 num UNION ALLSELECT 1 num UNION ALLSELECT 2 num UNION ALLSELECT 3 num UNION ALLSELECT 4 num UNION ALLSELECT 5 num UNION ALLSELECT 6 num UNION ALLSELECT 7 num UNION ALLSELECT 8 num UNION ALLSELECT 9 num ) aaCROSS JOIN(SELECT 0 num UNION ALLSELECT 10 num UNION ALLSELECT 20 num UNION ALLSELECT 30 num UNION ALLSELECT 40 num UNION ALLSELECT 50 num UNION ALLSELECT 60 num UNION ALLSELECT 70 num UNION ALLSELECT 80 num UNION ALLSELECT 90 num ) bbCROSS JOIN(SELECT 0 num UNION ALLSELECT 100num UNION ALLSELECT 200 num ) ccORDER BY 1

 

 

 

方法二

while循环

DECLARE @i INTDECLARE @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       )ENDSELECT * FROM @tb