SQL Server ROWLOCK over a SELECT if not exists INSERT transaction -


i have upgraded sql server 2005 2008. remember in 2005, rowlock did not work , had use pagelock or xlock achieve type of actual locking. know reader of ask "what did wrong?" nothing. conclusively proved edit "rowlocked" row, couldn't if escalated lock level. haven't had chance see if works in sql 2008. first question has come across issue in 2008?

my second question follows. want test if value exists , if so, perform update on relevant columns, rather insert of whole row. means if row found needs locked maintenance procedure delete row mid-process, causing error.

to illustrate principle, following code work?

begin tran  select      profileid        dbo.usesessions        (rowlock)       (profileid = @profileid) option      (optimize (@profileid unknown))  if @@rowcount = 0 begin     insert dbo.usersessions (profileid, sessionid)     values      (@profileid, @sessionid) end else begin     update      dbo.usersessions     set         sessionid = @sessionid, created = getdate()           (profileid = @profileid) end  commit tran 

an explanation...

  • rowlock/pagelock granularity
  • xlock mode

granularity , isolation level , mode orthogonal.

  • granularity = locked = row, page, table (paglock, rowlock, tablock)

  • isolation level = lock duration, concurrency (holdlock, readcommitted, repeatableread, serializable)

  • mode = sharing/exclusivity (updlock, xlock)

  • "combined" eg nolock, tablockx

xlock have locked row exclusively want. rowlock/pagelock wouldn't have.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -