SQL - How to SELECT declared variables into #temp table
NickName:P.Brian.Mackey Ask DateTime:2011-09-09T00:56:44

SQL - How to SELECT declared variables into #temp table

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?

Copyright Notice:Content Author:「P.Brian.Mackey」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7351742/sql-how-to-select-declared-variables-into-temp-table

Answers
a1ex07 2011-09-08T16:58:14

INSERT INTO #TempTables SELECT @var1, @var2\nSelect ... into creates a new table (which is already created in your case). Insert ... select inserts data into existing table.\n\nUpdate\nMore explanation.\nSQL 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.\nDepends on what you want you can\n1. Drop table and create it again\n2. Remove all data (if any) from it and populate it with INSERT INTO... SELECT",


JonH 2011-09-08T16:57:46

You must have added drop table @myTempTable at a later point, so SQL Server still sees this table. Open a new query window and do a\n\ndrop table #myTempTable \n\nThen you can execute your original command:\n\nCREATE TABLE #myTempTable\n(\n [value1] varchar(10),\n [value2] varchar(20)\n)\n\ndeclare @var1 varchar(10), @var2 varchar(20)\n\nSELECT @var1 = [value1], @var2 = [value2]\nFROM somePermTable\nwhere Condition = true\n\nSELECT @var1, @var2 INTO #myTempTable\n\ndrop table #myTempTable\n\n\nLooks like you have a temp table already, simply drop it first\n\nDROP TABLE #myTempTable",


More about “SQL - How to SELECT declared variables into #temp table” related questions

View SQL Server list of declared variables in SQL batch

Does anyone know if there is a way to view the list of variables that have been declared in a SQL batch? It would be something like this: declare @Variable1 datetime2 select * from [List Of Decla...

Show Detail

How to check if a sql variable is declared?

In MS SQL Server. Please don't tell me to ctrl-f. I don't have access to the entire query, but I'm composing the columns that depend on whether certain variable is declared. Thanks. Edit: I'm wor...

Show Detail

Spark - Remove broadcast variable declared in sql hint

Is there a way in spark to remove broadcast variables from the executor memory if it has been declared in sql hint? I've seen this How to remove / dispose a broadcast variable from heap in Spark? b...

Show Detail

Set value of previously declared variables in Dynamic Select in SQL Server

I'm working with a piece of dynamic SQL, posted below. During testing when I Print @Query and run it, my variables get assigned. But when that extra level of indirection is added, and the @Query ...

Show Detail

How to assign multiple select query values into declared variables in a stored procedure

How to assign multiple select query values into declared variables in a stored procedure? This works SELECT @id = USER_ID FROM USERS WHERE EMAIL=@EMAIL but I need something like this SELECT @...

Show Detail

How to select all variables declared in XSL?

I want to loop through all variables declared in my XSL. Is this possible using XPath? I need the XPaith syntax to be used in an XSL "for each".

Show Detail

How to check if all declared variables are set?

I am rather new to the whole OOP paradigm in PHP, but I'm really loving it so far. I am currently writing a EventSender class, which should gather some information, and then fire the event to a

Show Detail

How to use locally declared variables in SQL SELECT INSERT INTO?

I am working on a project that involves retrieving data from one table, and parsing out some of the information from certain fields and placing them into other fields on another table. This involve...

Show Detail

Pass variables to SQL query LIMIT sql Pandas

I have two variables i have declared and asigned values to, I want to pass them to an sql query limits in python. Below is what i have tried so far. Any help will be very much appreciated limitsta...

Show Detail

Create scalar function with declared variables

I have a verify simple query in which I have a variable declared. I would like to create a scalar function that automatically converts whatever I write into the variable, into my finished string:

Show Detail