Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Temporary table delete operation

How to check if the global Temporary table exists in SQL server, if yes then delete that global temporary table?

I am trying to execute this:

IF OBJECT_ID('##Table', 'U') IS NOT NULL  
  DROP TABLE ##Table

...but it is not working.

like image 863
Jason M Avatar asked Oct 16 '09 17:10

Jason M


2 Answers

To check the presence of temp table and delete it

IF OBJECT_ID('tempdb..##Table' , 'U') IS NOT NULL
   drop TABLE ##Table
like image 116
anishMarokey Avatar answered Oct 25 '22 22:10

anishMarokey


You can detect temp table presence with

IF OBJECT_ID('tempdb.dbo.##Table', 'U') IS NOT NULL

and, surprisingly to me, you can drop it from any connection with

DROP TABLE ##Table

However, I can't help but think that doing so would be a bad idea, since presumably the connection/user who created it might still be using it...

like image 27
Philip Kelley Avatar answered Oct 25 '22 23:10

Philip Kelley