Global Temporary table delete operation

14,626

Solution 1

To check the presence of temp table and delete it

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

Solution 2

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...

Share:
14,626
Jason M
Author by

Jason M

Updated on June 04, 2022

Comments

  • Jason M
    Jason M almost 2 years

    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.

  • Janatbek Orozaly
    Janatbek Orozaly about 5 years
    what is 'U' for?