7

I have the below query that get results from more than one select. Now I want these to be in a temp table.

Is there any way to insert these into a temp table without creating the table?

I know how to do that for select

Select * into #s --like that

However how to do that one more than one select?

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 ;
4

2 回答 2

12

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 ;
于 2013-06-10T05:37:55.190 回答
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 
)
于 2013-06-10T05:54:47.520 回答