I am trying to execute two select statements into a query that pumps data into a 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
?
I tried to search for a solution on the Internet but mostly people are only facing the problem at my first part but apparently nobody trying to expand a temp table on second query?
INSERT INTO SELECT statement reads data from one table and inserts it into an existing table. Such as, if we want to copy the Location table data into a temp table using the INSERT INTO SELECT statement, we have to specify the temporary table explicitly and then insert the data.
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
And if you want to insert everything:
INSERT INTO #TempTableName
SELECT * FROM MyTable
The second query should just be a normal insert.
INSERT INTO #testingTemp
(a,
b,
c)
select etc.
dont forget to drop the temptable when you are done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With