Question

I'm creating a temporary table in a stored procedure. I understand that they get created and destroyed for each session, however something is not clear. Let's say two users access the web page where I call the stored procedure that creates the temporary table, would there be a conflict when the two users create the same temp table?

Thanks

Was it helpful?

Solution 2

Temporary tables are created per SQL connection so two users calling the same stored procedure would create an individual instance of the table in question.

The simplest way to demonstrate this is to run the following query in 2 seperate query windows:

select 1 as someid
into #temp

Each window will have it's own connection, so will create a unique temp table.

If you look in System Databases > TempDB > Temporary Tables (you may have to refresh the table list), you will see 2 tables uniquely named, something like:

#temp________xxx1
#temp________xxx2

If you then close one of the query windows and refresh the temp table list, you will see one table has been dropped.

OTHER TIPS

If you create a local temp table (like #temp) then there is no problem. A global temp table (##Temp) however can be accessed by other sessions and as such I never use them unless I have no choice. From Books Online:

Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top