Deleting the Global Temp table and then creating the Global temp table with same name is giving ambiguity error - sql

Below is the SQL statement for Deleting the global temp table and creating it again.
if object_ID('tempdb..##TempTable123') is not null
begin drop table tempdb..##TempTable123 end
GO
Select * into ##TempTable123 from ##TempTable1
After executing the Select statement, I am getting the following Error
"The reference to temp table name '##TempTable123' is ambiguous and cannot be resolved. Use either '##TempTable123' or '##TempTable123'."

Try to add tempdb.. to the select:
Select * into tempdb..##TempTable123 from tempdb..##TempTable1

Related

storing query outputs dynamically TSQL

I have a loop over different tables which returns results
with different number of columns.
Is it possible to store the output of a query without creating a concrete table?
I've read some posts regarding temporary tables so I tried this simple example:
create table #temp_table1 (id int)
insert into #temp_table1 ('select * from table1')
table1 above could be any table
I get the following error message:
Column name or number of supplied values does not match table definition.
Is there anyway to avoid having hard code table definitions exactly matching the output of your query?
Thanks!
You could do a select into - that will create the temporary table automatically:
SELECT * INTO #Temp
FROM TableName
The problem is that since you are using dynamic SQL , your temporary table will only be available inside the dynamic SQL scope - so doing something like this will result with an error:
EXEC('SELECT * INTO #Temp FROM TableName')
SELECT *
FROM #Temp -- The #Temp table does not exists in this scope!
To do this kind of thing using dynamic SQL you must use a global temporary table (that you must drop once done using!):
EXEC('SELECT * INTO ##GlobalTempFROM TableName')
SELECT * INTO #Temp
FROM ##GlobalTemp -- Since this is a global temporary table you can use it in this scope
DROP TABLE ##GlobalTemp

Temp table already exists

Hi I have a temporary table in which I am trying to insert records based on a where condition but it throws an error that it already exists.
I have tried to change the names but that is not the issue as the temporary tables are delete when the session ends.
I think I am writing the query right.
SELECT [Name]
INTO #TEMP_REJECT
FROM #TEMP_VALIDATION
WHERE Name = #Name
I am trying to insert #TEMP_REJECT FROM #TEMP_VALIDATION
Error message
"There is already an object named '#TEMP_REJECT' in the database."
Please suggest.
Thank you for your help.
R
SQL Server won't tell you that a table doesn't exist if it doesn't.
I suggest that you add
IF OBJECT_ID('tempdb..#TEMP_REJECT') IS NOT NULL
DROP TABLE #TEMP_REJECT
in front of your select statement. This guarantees that the temp table won't exist when the select is executed.
So your statement becomes
IF OBJECT_ID('tempdb..#TEMP_REJECT') IS NOT NULL
DROP TABLE #TEMP_REJECT
SELECT [Name]
INTO #TEMP_REJECT
FROM #TEMP_VALIDATION
WHERE Name = #Name
This answer will help you :
https://stackoverflow.com/a/8560644/3635715
To make short : SELECT INTO creates table then insert records.
INSERT INTO only insert the records.
So in your case, since #TEMP_REJECT already exists, SELECT INTO is rejected because it cannot create the table again, so you have to use INSERT INTO after first SELECT INTO.
IF OBJECT_ID('tempdb..#TEMP_REJECT') IS NOT NULL
BEGIN
INSERT INTO #TEMP_REJECT
SELECT [Name]
FROM #TEMP_VALIDATION
WHERE Name = #Name
END
ELSE
BEGIN
SELECT [Name]
INTO #TEMP_REJECT
FROM #TEMP_VALIDATION
WHERE Name = #Name
END
References :
INTO Clause
INSERT Clause
Try checking before inserting
if object_id('tempdb..#TEMP_REJECT') is not null
drop table #TEMP_REJECT
SELECT [Name]
INTO #TEMP_REJECT
FROM #TEMP_VALIDATION
WHERE Name = #Name
if SQL says,there is temp table,there must be a table prior to your insert
I've noticed that I get the same error when I am working consecutively through the same SQL Server tab/file.
For example, I have a large SQL file that I use to execute a series of updates using temp tables. When I execute it consecutively, it errors out complaining that my #tempTable already exists (even if I use a "if this table exists, drop it" statement as DeanOC describes).
Exiting out of the file/tab between consecutive runs resolves the issue.
If this poses an issue for your use case, I'd suggest bundling your statements in a stored proc and implementing DeanOC's drop logic therein.

How can i remove temp table after creating it

Hi How can i remove temp table after creating it
because i get this error There is already an object named '#tmp_statement' in the database.
SELECT * FROM #tmp_statement
DROP TABLE #tmp_statement
You are getting error on creating it, not when dropping it. Try putting this before creating table:
IF object_id('tempdb..#tmp_statement') IS NOT NULL
DROP TABLE #tmp_statement
Or just put it before any other statements you have.

Alter table add field then error whilst using it

I am trying to add a field to a table. If I create the table and then call a sp and add the field inside the sp. I cannot use the field in a where clause
run part one and part two together: no error
run part one then part two inside sp: error (dont forget to drop #table when done)
run part one then part two: error (dont forget to drop #table when done)
Example:
/*PART 1*/
select 1 as number
into #table
union all
select 2 as number
/*******/
/*PART 2*/
ALTER TABLE #table ADD rowNo INT IDENTITY(1,1);
select * from #table
where rowNo between 0 and 10
drop table #table
/********/
Here is what I mean by run in a stored proc (new to sql fiddle):
http://sqlfiddle.com/#!3/545ff/1
It seems the error is thrown during code compilation. If you run the select (or any other queries that directly reference the new column) inside an exec() command, it works:
http://sqlfiddle.com/#!3/76b8c/1

SQL server-declare local variable: "there is already an object named '' in the database"

I wrote the following stored procedure, in which I use a local variable 'syncParam':
declare #syncParam bit
select isSync into syncParam from MyTable where id=#id
if (#syncParam='True')...
else ...
return #syncParam
When I executed this stored procedure at the first time it worked, but after that I get the following error: "there is already an object named 'syncParam' in the database".
What did I miss?
Thanks in advance.
You want
select #syncParam = isSync from MyTable where id=#id
SELECT INTO will insert records into a new table. Go look, you should have a syncParam table now.
you might consider using a temp table .. just rename the table #syncParam