27,580
社区成员
发帖
与我相关
我的任务
分享;WITH cte AS (
SELECT startnumber AS number FROM #A
UNION ALL
SELECT cte.number+1 FROM #A JOIN cte ON cte.number<#A.endnumber
)
SELECT number
FROM cte
WHERE number NOT IN ( SELECT number
FROM #B )
OPTION(MAXRECURSION 0) 
--测试数据
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([startnumber] int,[endnumber] int)
Insert #A
select 1,20
GO
if not object_id(N'Tempdb..#B') is null
drop table #B
Go
Create table #B([number] int)
Insert #B
select 1 union all
select 2 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
Go
--测试数据结束
;WITH cte AS (
SELECT startnumber AS number FROM #A
UNION ALL
SELECT cte.number+1 FROM #A JOIN cte ON cte.number<#A.endnumber
)
SELECT number
FROM cte
WHERE number NOT IN ( SELECT number
FROM #B )

--测试数据
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([startnumber] int,[endnumber] int)
Insert #A
select 1,10
GO
if not object_id(N'Tempdb..#B') is null
drop table #B
Go
Create table #B([number] int)
Insert #B
select 1 union all
select 2 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
Go
--测试数据结束
SELECT number
FROM master.dbo.spt_values
WHERE type = 'P'
AND number BETWEEN ( SELECT startnumber
FROM #A
)
AND ( SELECT endnumber
FROM #A
)
AND number NOT IN ( SELECT number
FROM #B )
