如何将筛选后的查询的结果集解析为临时表

时间:2013-10-22 20:33:34

标签: sql sql-server-2008

我需要将给定union所有连接查询的结果集存储到临时表

DECLARE @no_of_days INT = 2
DECLARE @xxx DATETIME = '2009-11-04'

SELECT *
FROM   source_price_curve
WHERE  maturity_date >= DATEADD(DAY, -1 * @no_of_days, @xxx) AND maturity_date < @xxx
UNION ALL 
SELECT *
FROM   source_price_curve
WHERE  maturity_date <= DATEADD(DAY, 1 * @no_of_days, @xxx) AND maturity_date > @xxx

2 个答案:

答案 0 :(得分:0)

请尝试以下方法:

Insert Into #Temp
SELECT *
FROM   source_price_curve
WHERE  maturity_date >= DATEADD(DAY, -1 * @no_of_days, @xxx) AND maturity_date < @xxx
UNION ALL 
SELECT *
FROM   source_price_curve
WHERE  maturity_date <= DATEADD(DAY, 1 * @no_of_days, @xxx) AND maturity_date > @xxx

答案 1 :(得分:0)

临时表插入必须在第一个选择中。

这是SqlFiddle上的演示。

DECLARE @no_of_days INT = 2
DECLARE @xxx DATETIME = '2009-11-04'

SELECT * into #Temp
FROM   source_price_curve
WHERE  maturity_date >= DATEADD(DAY, -1 * @no_of_days, @xxx) AND maturity_date < @xxx
UNION ALL 
SELECT *
FROM   source_price_curve
WHERE  maturity_date <= DATEADD(DAY, 1 * @no_of_days, @xxx) AND maturity_date > @xxx