Mr.SuicideSheep Mr.SuicideSheep - 5 months ago 9
SQL Question

SQL Server how to insert into existed temp table

I am trying to execute two select into query that pump data into temp table. The first query will have 5 columns while the second query will have only one column.

The first can be achieved by:

Select a.ID AS [a],
b.ID AS [b],
c.ID AS [c]
INTO #testingTemp
FROM
....


Now I have my second query trying to pump data into
#testingTemp
:

Select z.ID AS [c]
INTO #testingTemp
FROM
....


But my problem is
There is already an object named #testingTemp in the database
?

Tried to search solution on internet but mostly people only facing problem at my first part but apparently nobody trying to expand a temp table on second query?

Answer

Change it into a insert into statement. Otherwise you create the same temp table multiple times and that is not allowed.

Insert into #testingTemp (a,b,c)
Select a.ID AS [a], 
       b.ID AS [b], 
       c.ID AS [c]
FROM