Short answer:
Isolation of temporary tables is guaranteed for each request, and there is nothing to worry about, neither about the stream, nor about the locks, nor about simultaneous access.
I'm not sure why the answers here talk about the meaning of “connections” and threads, as these are programming concepts, while query isolation is handled at the database level.
Local temporary objects are shared by the session on the SQL server. If you have two requests running at the same time, then they are two completely separate sessions and will not interact with each other. Logging in does not matter, for example, if you use the same connection string using ADO.NET (this means that several parallel queries will use the same SQL "login" query), your queries will still be executed in separate sessions. The connection pool is also irrelevant. Local temporary objects (tables and stored procedures) are completely safe for viewing by other sessions.
To clarify how this works; while your code has one common name for local temporary objects, SQL Server adds a unique line for each object in each session to keep them separate. You can see this by doing the following in SSMS:
CREATE TABLE
You will see the following for the name:
T _______________ 00000000001F
Then, without closing this query tab, open a new query tab and paste the same query and run it again. You should now see something like the following:
T _______________ 00000000001F
T _______________ 000000000020
So, every time your code references #T, SQL Server translates it into the corresponding name based on the session. Separation is carried out automatically, magically.
Chris Halcrow Jul 18 '17 at 4:28 2017-07-18 04:28
source share