Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Solution to save and restore the isolation level

Author  Topic 

codism
Starting Member

11 Posts

Posted - 2008-01-15 : 13:10:31
I am looking for a way to save and restore the isolation level in my stored procedure. I need the serilizable level for some statements in the stored procedure and need to restore to the original level (usually read committed but I don't want to hardcode this in my code).

Thanks in advance!

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-01-15 : 15:20:08
Here is one way. There may be a better way but I don't know it

------------------------------------------------------------
--capture existing value (if current session wasn't changed from the default then it will not be returned)
if object_id('tempdb..#useroptions') > 0
drop table #useroptions
create table #useroptions (setOption varchar(200), value varchar(50))

insert #userOptions
exec ('DBCC USEROPTIONS')
------------------------------------------------------------

--do your stuff
set transaction isolation level SERIALIZABLE


------------------------------------------------------------
--now set it back to whatever it was
declare @isolationlevel varchar(50)
select @isolationlevel = value
from #useroptions
where setoption = 'isolation level'

if isNull(@isolationlevel, 'read uncommitted') = 'read uncommitted'
set transaction isolation level read uncommitted
else if @isolationlevel = 'READ COMMITTED'
set transaction isolation level read committed
else if @isolationlevel = 'REPEATABLE READ'
set transaction isolation level REPEATABLE READ
else if @isolationlevel = 'SERIALIZABLE'
set transaction isolation level SERIALIZABLE
------------------------------------------------------------


Be One with the Optimizer
TG
Go to Top of Page

codism
Starting Member

11 Posts

Posted - 2008-01-16 : 09:21:29
I saw (you don't believe I typed "saw" as "was" just now:) your solution yesterday and wanted to wait longer for more possibly simpler solutions. Last night, I started thinking - if I cannot have a decent answer, probably I asked a wrong question. Then I reviewed my problem and realized that all I need is to use lock hint to override the lock behavior provided by the current isolation level.
Just share with you what I came up with and your code is definitely the ultimate solution to my original question for now!
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-01-16 : 09:32:56
Cool - glad you have a simple solution. and thanks, I appreciate the feedback!

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -