sql中给定范围的小数

时间:2010-12-07 19:38:55

标签: sql sql-server

是否可以编写sql查询(在mssql中),返回给定范围内的小数(每个都在单独的行中)? e.g。

11
12
13
14
...

2 个答案:

答案 0 :(得分:2)

不确定你想要什么

declare @start int=11;
declare @end int=26;

with cte as(
select @start as col union all
select COL+1 from cte where col+1<=@end)

select * from cte

答案 1 :(得分:1)

使用Itzik Ben-Gan's technique(这将为您提供最多4,294,967,296的数字)来生成数字表:

WITH
    L0   AS(SELECT 1 AS c UNION ALL SELECT 1),
    L1   AS(SELECT 1 AS c FROM L0 AS A CROSS JOIN L0 AS B),
    L2   AS(SELECT 1 AS c FROM L1 AS A CROSS JOIN L1 AS B),
    L3   AS(SELECT 1 AS c FROM L2 AS A CROSS JOIN L2 AS B),
    L4   AS(SELECT 1 AS c FROM L3 AS A CROSS JOIN L3 AS B),
    L5   AS(SELECT 1 AS c FROM L4 AS A CROSS JOIN L4 AS B),
    Nums AS(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS n FROM L5)
SELECT n FROM Nums WHERE n between 11 and 20