Solved

drop temp table

Posted on 2014-12-02
7
201 Views
Last Modified: 2014-12-02
I have this .... and it is returning null for the temp table even when it has been created.  Why?

 select OBJECT_ID('#TempTable')

IF OBJECT_ID('#TempTable') IS NOT NULL

Drop Table #TempTable

Create Table #TempTable

(
  Name Varchar(max)
  , Age Int
  )


INSERT INTO #TempTable  Values('ddd' , 1 )

Select * From #TempTable
0
Comment
Question by:vbnetcoder
  • 3
  • 3
7 Comments
 
LVL 20

Accepted Solution

by:
dsacker earned 500 total points
ID: 40477131
Change your IF statement to:

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL

If you're simply wanting to see the object_id of that table, you'll also need to change that top SELECT statement to:

select OBJECT_ID('tempdb..#TempTable')
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40477147
Assuming you just want to drop/recreate the table, and aren't concerned about the OBJECT_ID, give this a whirl..
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
   DROP TABLE #TempTable

CREATE TABLE #TempTable (blah, blah, blah...) 

Open in new window


<edit:  Somewhat redundant with the above post>
0
 

Author Comment

by:vbnetcoder
ID: 40477165
I did the same thing  but with the adventure works 2012 table and it give me a null even when there is a table


IF OBJECT_ID('AdventureWorks2012..#TempTable') IS NOT NULL

Drop Table #TempTable

Create Table #TempTable

(
  Name Varchar(max)
  , Age Int
  )
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 20

Expert Comment

by:dsacker
ID: 40477175
Do not put:

IF OBJECT_ID('AdventureWorks2012..#TempTable') IS NOT NULL

#TempTable names are ALWAYS created in tempdb, no matter where you query them from (i.e., even from AdventureWorks2012).

Only use:

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL

The very nature of a # in the #TempTable creates it in tempdb. Every SQL Server has a tempdb, used for temp tables, temp result sets, i.e., the majority of temporary operations or storage means within SQL Server.
0
 

Author Closing Comment

by:vbnetcoder
ID: 40477183
ty
0
 
LVL 20

Expert Comment

by:dsacker
ID: 40477196
Very welcome. Glad that helped.

You may also be interested in DECLARE tables. In a sense, they are even more temp than #temp tables, because they are memory tables (mostly). You create those as follows:

DECLARE @TempTable TABLE
(
  Name Varchar(max)
  , Age Int
  )

INSERT INTO @TempTable  Values('ddd' , 1 )
SELECT * FROM @TempTable
Nice thing about those: They completely disappear after you finish executing the script (or proc). Never a worry about whether they existed before. You can't even code an "IF OBJECT_ID('tempdb..@TempTable') IS NOT NULL" statement and it make sense to SQL Server.
0
 

Author Comment

by:vbnetcoder
ID: 40477204
thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now