Sql 选择@min和@max之间的所有N

Sql 选择@min和@max之间的所有N,sql,sql-server,Sql,Sql Server,我需要选择@min和@max之间的所有数字N个整数 有没有办法不使用某种循环来实现这一点 select * from tableName where e_id between (SELECT min(e_id) FROM tableName a) and (SELECT max(e_id) FROM tableName a) 例如: 假设@min=5,@max=9 select * from tableName where e_id between

我需要选择@min和@max之间的所有数字N个整数 有没有办法不使用某种循环来实现这一点

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
例如: 假设@min=5,@max=9

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
我需要SQL查询返回以下值:5,6,7,8,9

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
我正在使用MSSQL 2005

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
谢谢

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
编辑: 这是一个使用自定义函数的解决方案,效果很好。 但手动循环所有数字似乎太费劲了。 所以问题仍然是,没有循环是否可以实现

CREATE FUNCTION GetAllNBetween
(    
    @Min int,
    @Max int
)
RETURNS @N TABLE(n int)
AS
BEGIN
    WHILE @Min <= @Max
    BEGIN
        INSERT INTO @N VALUES(@Min)
        SET @Min = @Min + 1
    END
    RETURN
END
select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
试试这个

declare @min int

set @min= (select 5)

 declare @max int

set @max=(select 9)

select * from table

where id between @min and @max
select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)

您可以使用between关键字执行此操作。这是一个例子

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)

如果你知道最小值和最大值,那么直接把它们放进去,而不是嵌套查询。

你能利用这个函数行号吗?它在Mssql 2005中是新的

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
另外,我刚刚发现在ms sql 2005中也可以这样做:

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
select *
  from dbo.GetTableOfSequentialIntegers(100)
 where number between 5 and 9

我不明白为什么你不想使用循环,但是你可以用递归代替

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
DECLARE @min INT
DECLARE @max INT

SET @min = 5;
SET @max = 12;

WITH Nbrs ( n ) AS (
    SELECT @min UNION ALL
    SELECT 1 + n FROM Nbrs WHERE n < @max
)
SELECT n FROM Nbrs
OPTION ( MAXRECURSION 500 )

它将生成一个包含所有值的表。从中生成一个字符串列表应该不会太难

由@Eric建议的查询

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
select ROW_NUMBER() OVER (ORDER BY so1.id) from sysobjects so1,sysobjects
在我的大部分空测试数据库中返回介于1到3000之间的数字。您可以添加另一级别的sysobjects以获得数量惊人的行。然后,只需简单地过滤这个

试试这个:

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
DECLARE @min int, @max int
SELECT @Min=5,@Max= 9

SELECT TOP (@Max-@Min+1) @Min-1+row_number() over(order by t1.number) as N
FROM master..spt_values t1 
    CROSS JOIN master..spt_values t2
输出:

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
N
--------------------
5
6
7
8
9

(5 row(s) affected)

请参见前面的问题:

我没有看到任何关于CTE公共表表达式的答案,因此这里有一个:

select *
  from tableName
 where e_id between (SELECT min(e_id) FROM tableName a)
                and (SELECT max(e_id) FROM tableName a)
WITH RECURSIVE integers(n)
AS (
  SELECT @min
  UNION SELECT n + 1 FROM integers WHERE n < @max
) SELECT n FROM integers

问题是没有包含这些条目的表。所以“tableName”在我的场景中不存在,问题是没有包含这些条目的表。所以我的场景中不存在“table”。请使用函数查看我的答案。这就是我的意思。除无功能外;澄清一下:我没有一个包含所有N的表。为什么需要在sql中这样做?我甚至不知道CTEs。我看起来很有前途。但我无法让你提供的查询正常工作。你能提供完整的样品吗?