如何在两个数字之间生成一系列数字?我有两个数字作为用户的输入,例如1000和1050.如何使用SQL查询在分隔行中生成这两个数字之间的数字?我想要这个: 1000
1001
1002
1003
.
.
1050
3 回答
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
VALUES
JOIN
SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.nFROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n), (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) tens(n), (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) hundreds(n), (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) thousands(n) WHERE ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n BETWEEN @userinput1 AND @userinput2ORDER BY 1
WITH x AS (SELECT n FROM (VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) v(n)) SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.nFROM x ones, x tens, x hundreds, x thousandsORDER BY 1
慕标琳琳
TA贡献1830条经验 获得超9个赞
DECLARE @startnum INT=1000DECLARE @endnum INT=1050;WITH gen AS ( SELECT @startnum AS num UNION ALL SELECT num+1 FROM gen WHERE num+1<=@endnum)SELECT * FROM genoption (maxrecursion 10000)
饮歌长啸
TA贡献1951条经验 获得超3个赞
SELECT DISTINCT n = number FROM master..[spt_values] WHERE number BETWEEN @start AND @end
;WITH Nums AS( SELECT n = ROW_NUMBER() OVER (ORDER BY [object_id]) FROM sys.all_objects )SELECT n FROM Nums WHERE n BETWEEN @start AND @endORDER BY n;
添加回答
举报
0/150
提交
取消