插入临时表而不从联合结果创建它

时间:2013-06-10 04:05:14

标签: sql-server-2008-r2

我有以下查询从多个选择中获取结果。 现在我希望这些都在临时表中。

有没有办法在不创建表的情况下将这些插入到临时表中?

我知道如何选择

Select * into #s --like that

但是如何做一个以上的选择?

SELECT  Ori.[GeoBoundaryAssId],  Ori.[FromGeoBoundaryId], Ori.Sort 
From [GeoBoundaryAss] As Ori where  Ori.[FromGeoBoundaryId] = (select distinct [FromGeoBoundaryId] from inserted )

Union 

SELECT  I.[GeoBoundaryAssId],  I.[FromGeoBoundaryId], I.Sort 
From [inserted] I ;

2 个答案:

答案 0 :(得分:9)

在第一个INTO之后添加SELECT

SELECT  Ori.[GeoBoundaryAssId],  Ori.[FromGeoBoundaryId], Ori.Sort 
INTO #s
From [GeoBoundaryAss] As Ori where  Ori.[FromGeoBoundaryId] = (select distinct [FromGeoBoundaryId] from inserted )

Union 

SELECT  I.[GeoBoundaryAssId],  I.[FromGeoBoundaryId], I.Sort 
From [inserted] I ;

答案 1 :(得分:1)

试试这个,

INSERT INTO #s ([GeoBoundaryAssId], [FromGeoBoundaryId], Sort)
(
    SELECT  Ori.[GeoBoundaryAssId],  Ori.[FromGeoBoundaryId], Ori.Sort 
    FROM [GeoBoundaryAss] AS Ori WHERE  Ori.[FromGeoBoundaryId] in (SELECT DISTINCT [FromGeoBoundaryId] FROM inserted )

    UNION 

    SELECT  I.[GeoBoundaryAssId],  I.[FromGeoBoundaryId], I.Sort 
    FROM [inserted] I 
)