Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global temporary tables in SQL Server

Tags:

sql

sql-server

I have a ##table which can be accessed across all the sessions but sometimes I am getting error

There is already an object named '##table' in the database.

WHY and how to resolve it.

like image 597
Jeevan Bhatt Avatar asked Oct 08 '10 05:10

Jeevan Bhatt


1 Answers

Found an interesting reference here:

Global temporary tables operate much like local temporary tables; they are created in tempdb and cause less locking and logging than permanent tables. However, they are visible to all sessions, until the creating session goes out of scope (and the global ##temp table is no longer being referenced by other sessions). If two different sessions try the above code, if the first is still active, the second will receive the following:

Server: Msg 2714, Level 16, State 6, Line 1 There is already an object named '##people' in the database.

I have yet to see a valid justification for the use of a global ##temp table. If the data needs to persist to multiple users, then it makes much more sense, at least to me, to use a permanent table. You can make a global ##temp table slightly more permanent by creating it in an autostart procedure, but I still fail to see how this is advantageous over a permanent table. With a permanent table, you can deny permissions; you cannot deny users from a global ##temp table.

like image 121
Chris Porter Avatar answered Sep 30 '22 06:09

Chris Porter