Is there anyway to execute stored procedure with SET LOCK_TIMEOUT
explicitly?
E.g. I have sproc called dbo.LoadData
. It just takes data from table dbo.Abc
and insert them in table dbo.Xyz
.
Now I want to execute the sproc dbo.LoadData
, but if the table dbo.Abc
is locked
for e.g. more than 10 seconds, stop the execution of the stored procedure.
I know the option SET LOCK_TIMEOUT
can be implemented in the sproc dbo.LoadData
itself, but I am wondering if there is any way how to call it explicitly (= not implementing the option in the stored procedure itself).
Best Answer
LOCK_TIMEOUT
is not only a session-level setting, it also needs to be set in a separate batch. But, it cannot be set via a variable. So, this can be accomplished by using Dynamic SQL in the "master" Stored Procedure. This will allow for settingLOCK_TIMEOUT
and then executing whatever other Stored Procedure that should run within this particular setting. It has to be a single execution of Dynamic SQL since the setting will revert back to the value of the top-most / outer-most process. For example:Run this in one query tab in SSMS:
Run the following in another query tab in SSMS:
Now go back to the first query tab (where you created the two temporary Stored Procedures) and run the following two tests:
Now go back to the second query tab and execute the
ROLLBACK
;-).