Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are table variables thread safe on sql server 2008 r2?

For example, I am going to use the following table variable inside one of my stored procedures;

DECLARE @MyTempTable TABLE 
(
   someField int,
   someFieldMore nvarchar(50)
)

Is this variable thread safe? if multiple request comes at the same time, do you think there would be a conflict?

like image 821
tugberk Avatar asked Dec 13 '22 10:12

tugberk


2 Answers

Yes.

"Thread safe" would be "scope-safe" or "connection-safe" in SQL Server. Scope-safe implies connection-safe too.

Table variables are like normal variables: local to that scope. Each connection is isolated from each other, and each connection is a series of scopes

The best example of this is the difference between

  • SCOPE_IDENTITY = scope safe
  • @@IDENTITY = connection safe, not scope safe
  • IDENT_CURRENT = not safe either way

See: @@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT [sql server 2005]

like image 56
gbn Avatar answered Dec 26 '22 12:12

gbn


The table variable is local in the scope where it is created. Two simultanious connections does not share the table variable.

like image 40
Mic Avatar answered Dec 26 '22 11:12

Mic