Temporary table in SQL server causing ' There is already an object named' error

SqlSql ServerTsqlTemp Tables

Sql Problem Overview


I have the following issue in SQL Server, I have some code that looks like this:

DROP TABLE #TMPGUARDIAN
CREATE TABLE #TMPGUARDIAN(
LAST_NAME NVARCHAR(30),
FRST_NAME NVARCHAR(30))  
                 
SELECT LAST_NAME,FRST_NAME INTO #TMPGUARDIAN  FROM TBL_PEOPLE

When I do this I get an error 'There is already an object named '#TMPGUARDIAN' in the database'. Can anyone tell me why I am getting this error?

Sql Solutions


Solution 1 - Sql

You are dropping it, then creating it, then trying to create it again by using SELECT INTO. Change to:

DROP TABLE #TMPGUARDIAN
CREATE TABLE #TMPGUARDIAN(
LAST_NAME NVARCHAR(30),
FRST_NAME NVARCHAR(30))  

INSERT INTO #TMPGUARDIAN 
SELECT LAST_NAME,FRST_NAME  
FROM TBL_PEOPLE

In MS SQL Server you can create a table without a CREATE TABLE statement by using SELECT INTO

Solution 2 - Sql

I usually put these lines at the beginning of my stored procedure, and then at the end.

It is an "exists" check for #temp tables.

IF OBJECT_ID('tempdb..#MyCoolTempTable') IS NOT NULL
begin
        drop table #MyCoolTempTable
end

Full Example:

CREATE PROCEDURE [dbo].[uspTempTableSuperSafeExample]
AS
BEGIN
	SET NOCOUNT ON;


	IF OBJECT_ID('tempdb..#MyCoolTempTable') IS NOT NULL
	BEGIN
			DROP TABLE #MyCoolTempTable
	END


	CREATE TABLE #MyCoolTempTable (
		MyCoolTempTableKey INT IDENTITY(1,1),
		MyValue VARCHAR(128)
	)  

	INSERT INTO #MyCoolTempTable (MyValue)
		SELECT LEFT(@@VERSION, 128)
		UNION ALL SELECT TOP 10 LEFT(name, 128) from sysobjects

	SELECT MyCoolTempTableKey, MyValue FROM #MyCoolTempTable


	IF OBJECT_ID('tempdb..#MyCoolTempTable') IS NOT NULL
	BEGIN
			DROP TABLE #MyCoolTempTable
	END


	SET NOCOUNT OFF;
END
GO

Solution 3 - Sql

You must modify the query like this

CREATE TABLE #TMPGUARDIAN(
LAST_NAME NVARCHAR(30),
FRST_NAME NVARCHAR(30))  

INSERT INTO #TMPGUARDIAN(FRST_NAME,LAST_NAME)
SELECT LAST_NAME,FRST_NAME  FROM TBL_PEOPLE

-- Make a last session for clearing the all temporary tables. always drop at end. In your case, sometimes, there might be an error happen if the table is not exists, while you trying to delete.

DROP TABLE #TMPGUARDIAN

Avoid using insert into Because If you are using insert into then in future if you want to modify the temp table by adding a new column which can be filled after some process (not along with insert). At that time, you need to rework and design it in the same manner.

Use Table Variable http://odetocode.com/articles/365.aspx

declare @userData TABLE(
 LAST_NAME NVARCHAR(30),
    FRST_NAME NVARCHAR(30)
)

Advantages No need for Drop statements, since this will be similar to variables. Scope ends immediately after the execution.

Solution 4 - Sql

Some times you may make silly mistakes like writing insert query on the same .sql file (in the same workspace/tab) so once you execute the insert query where your create query was written just above and already executed, it will again start executing along with the insert query.

This is the reason why we are getting the object name (table name) exists already, since it's getting executed for the second time.

So go to a separate tab to write the insert or drop or whatever queries you are about to execute.

Or else use comment lines preceding all queries in the same workspace like

CREATE -- …
-- Insert query
INSERT INTO -- …

Solution 5 - Sql

In Azure Data warehouse also this occurs sometimes, because temporary tables created for a user session.. I got the same issue fixed by reconnecting the database,

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArt FView Question on Stackoverflow
Solution 1 - SqlHart COView Answer on Stackoverflow
Solution 2 - SqlgranadaCoderView Answer on Stackoverflow
Solution 3 - SqlkbvishnuView Answer on Stackoverflow
Solution 4 - SqlKalaivani MathivananView Answer on Stackoverflow
Solution 5 - SqldgcharithaView Answer on Stackoverflow