0

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

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
4

2 回答 2

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
于 2013-10-22T21:01:29.890 回答
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
于 2013-10-22T20:36:45.617 回答