I\'m using SQL Server 2000, and many of the stored procedures it use temp tables extensively. The database has a lot of traffic, and I\'m concerned about the thread-safety of cr
The short answer is:
Isolation of temporary tables is guaranteed per query, and there's nothing to worry about either in regard to threading, locks, or concurrent access.
I'm not sure why answers here talk about a significance of 'connections' and threads as these are programming concepts, whereas query isolation is handled at the database level.
Local temporary objects are separated by Session in SQL server. If you have two queries running concurrently, then they are two completely separate sessions and won't intefere with one another. The Login doesn't matter, so for example if you are using a single connection string using ADO.NET (meaning that multiple concurrent queries will use the same SQL server 'login'), your queries will all still run in separate sessions. Connection Pooling also doesn't matter. Local temporary objects (Tables and Stored Procedures) are completely safe from being seen by other sessions.
To clarify how this works; while your code has a single, common name for the local temporary objects, SQL Server appends a unique string to each object per each session to keep them separate. You can see this by running the following in SSMS:
CREATE TABLE #T (Col1 INT)
SELECT * FROM tempdb.sys.tables WHERE [name] LIKE N'#T%';
You will see something like the following for the name:
Then, without closing that query tab, open up a new query tab and paste in that same query and run it again. You should now see something like the following:
So, each time your code references #T, SQL Server will translate it to the proper name based on the session. The separation is all handled auto-magically.