I want to do
SELECT @var1, @var2
INTO #myTempTable
I keep getting error
There is already an object named '#myTempTable' in the database.
My code goes
CREATE TABLE #myTempTable
(
[value1] varchar(10),
[value2] varchar(20)
)
declare @var1 varchar(10), @var2 varchar(20)
SELECT @var1 = [value1], @var2 = [value2]
FROM somePermTable
where Condition = true
SELECT @var1, @var2 INTO #myTempTable
drop table #myTempTable
What am I doing wrong?
INSERT INTO #TempTables SELECT @var1, @var2
Select ... into
creates a new table (which is already created in your case). Insert ... select
inserts data into existing table.
Update
More explanation.
SQL Server doesn't allow creating 2 tables with the same name. In your case you have local temporary table which is created twice (1st time CREATE TABLE
, 2nd SELECT ... INTO..
), so you get an error.
Depends on what you want you can
1. Drop table and create it again
2. Remove all data (if any) from it and populate it with INSERT INTO... SELECT