简体   繁体   中英

How to check correctly if a temporary table exists in SQL Server 2005?

I have a query where I insert some values from a table:

SELECT ID, NAME INTO #tmpTable1
FROM TableOriginal

First execution is fine, if I press F5(Run) in MSSMS (Microsoft Sql Server Management Studio), the error occured:

Msg 2714, Level 16, State 6, Line 4
There is already an object named '#tmpTable1' in the database.

Good. I decided to check before insert data from TableOriginal to #tmpTable1 using:

IF OBJECT_ID('tempdb.#tmpTable1') IS NOT NULL  
  DROP TABLE #tmpTable1

Not working, the error shows again as above.

I saw in tempdb database the following temporary table name:

dbo.#tmpTable1__________________0000007

Why? Every time when create a temporary table (using first query), the table name will be generated automatically in MSSMS ?

How to remove the existing temporary table to do a new table with new values ?

You're darn close - you need to use two dots in your check:

IF OBJECT_ID('tempdb..#tmpTable1') IS NOT NULL  
                    ** 
                    |
                  use two dots here!

Basically, this is saying: check in the tempDB and I don't care what schema the table is in

As Joe rightfully said: this is not 100% correct: it doesn't check in every schema - it will only check in the default owner's schema - normally dbo . So this would work, too:

IF OBJECT_ID('tempdb.dbo.#tmpTable1') IS NOT NULL  

If you happen to create your objects in a schema other than the default owner's, then you'll need to explicitly specify the schema you're referring to. But the temp tables in tempDB are indeed creating in the dbo schema.

This isn't an answer to the question, just wanted to temporarily post a response to the bit about puting .dbo. vs. .. when referencing a #temp table.

I couldn't find a way to make a #temp table actually be owned by anything other than dbo. Try it:

CREATE SCHEMA blat;
GO

CREATE TABLE blat.#pound(id INT);
GO

SELECT 
   OBJECT_ID('tempdb..#pound'), 
   OBJECT_ID('tempdb.dbo.#pound'), 
   OBJECT_ID('tempdb.blat.#pound');

USE tempdb;
GO

SELECT [object_id], SCHEMA_NAME([schema_id]) 
  FROM sys.objects 
  WHERE name LIKE '#pound%';

Results:

-1222354987    -1222354987    -1222354987

-1222354987    dbo

This was on SQL Server 2012. I tested this on SQL Server 2005 and the only difference was the object_id values were positive. I also tried with:

  • a schema actually existing in tempdb called blat
  • the table blat.#pound created by a user whose default schema is blat
  • both of the above

In all three cases, the same results as above were achieved.

You also can't create two #temp tables with the same name in different schemas:

CREATE TABLE blat.#flab(id INT);
CREATE TABLE dbo.#flab(id INT);

Result:

Msg 2714, Level 16, State 6
There is already an object named '#flab' in the database.

This is not a parsing problem (like many #temp table issues are); you can run those two statements separately and receive the same error.

So, this is a long-winded way of saying, you don't ever need to specify the schema when resolving a #temp table name, it will always be created under dbo and resolution at least under OBJECT_ID will ignore the schema you specify ( OBJECT_SCHEMA_NAME also always returns dbo when run in the context of #tempdb, but not in any other database). All bets are off if you try to query schema_id in tempdb.sys.objects .

CREATE TABLE #temptable (col1 int);
GO
INSERT INTO #temptable
VALUES (10);
GO
SELECT * FROM #temptable;
GO
IF OBJECT_ID(N'tempdb..#temptable', N'U') IS NOT NULL 
DROP TABLE #temptable;
GO
--Test the drop.
SELECT * FROM #temptable;

Temp table defined that way will exist as long as the connection that created it is open. Usually there's no need to check if it exists or drop it manually because you have full control inside your connection, but if you really need to check it you can check for tempdb.dbo.#tempTable.

IF OBJECT_ID('tempdb.dbo。#tmpTable1%')IS NOT NULL :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM