I have the following logic in one of my SPs:
if object_id('tempdb..##val','u') is null begin
create table ##val (
name varchar(512)
,val nvarchar(max)
)
end
insert ##val (name,val)values('some unique name','abcdef')
This question has answer that indicates checking if object_id(
, but I have these concerns:
1) is this check thread-safe from race condition point of view ? Specifically, do I need to use sp_GetAppLock
/sp_ReleaseAppLock
?
2) Does if condition reserve reference on table ##val
? Specifically, is it possible that all other sessions release reference on ##val
right after negative result of if
condition making insert
statement fail due to not existing ##val table ? And if yes how to reserve and keep reference on ##val
from current session without locking rows in that table.