日期:2014-05-17 浏览次数:20484 次
select number
from mster..spt_values s left join t where s.number=t.value and type='p'
where t.value is null
declare @i int
declare @n int
declare @min int
declare @max int
set @i=0 --生成数据最小值
set @n=10000 --生成数据最大值
set @min=5000 --查询范围最小值
set @max=10000 --查询范围最大值
CREATE TABLE #t(
[ID] [int] IDENTITY(1,1) NOT NULL,
[number] [int] NULL
)
CREATE TABLE #y(
[ID] [int] IDENTITY(1,1) NOT NULL,
[number] [int] NULL
)
---插入测试数据
insert into #t
select 1 union all
select 2 union all
select 3 union all
select 4
while (@i<=@n)
begin
insert into #y ([number]) values(@i)
set @i=@i+1
end
---查询测试结果
select y.number from #y y
left join #t T on y.number=T.[number]
where y.number>=@min and y.number<=@max and T.[number] is null
drop table #t
drop table #y
--自己创建一个数字辅助表代替master..spt_values
IF OBJECT_ID('dbo.Nums') IS NOT NULL
DROP TABLE dbo.Nums;
GO
CREATE TABLE dbo.Nums(n INT NOT NULL PRIMARY KEY);
DECLARE @max AS INT, @rc AS INT;
SET @max = 1000000;
SET @rc = 1;
INSERT INTO Nums VALUES(1);
WHILE @rc * 2 <= @max
BEGIN
INSERT INTO dbo.Nums SELECT n + @rc FROM dbo.Nums;
SET @rc = @rc * 2;
END
INSERT INTO dbo.Nums
SELECT n + @rc FROM dbo.Nums WHERE n + @rc <= @max;
GO