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
 General SQL Server Forums
 New to SQL Server Programming
 HOLDLOCK

Author  Topic 

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-15 : 15:15:19
I thought I knew this

How do I hold a lock on a row until a tran completes...I thought being inside a tran would do it...but BOL says no...the I tried HOLDLOCK and that doesn't seem to work

Trying to implement a non-identity "identity" column with a reusable sproc


CREATE TABLE ##t (Col1 int, Col2 char(1))
GO

--Seed the table

INSERT INTO ##t(Col1) SELECT 0
GO

-- Should put a trigger on to prevent more than 1 row

-- Update the table to lock the row

BEGIN TRAN

UPDATE ##t
WITH (HOLDLOCK)
SET Col2 = 'Y'

COMMIT TRAN


SELECT * FROM ##t

-- Check the lock in a separate thread

EXEC(' UPDATE ##t SET Col2 = ''N''')
GO

SELECT * FROM ##t




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-15 : 15:20:27
UPDLOCK + HOLDLOCK if I recall correctly.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-15 : 15:23:08
Right...but

http://www.mssqlcity.com/Articles/Adm/SQL70Locks.htm

I have to set the trans level...in the sproc???

Gotta test...

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-15 : 15:35:12
I don't think so. We have the same non-identity identity in one of our systems (yuck!), and we've only got updlock, holdlock in the update query. We are not changing the isolation level in it nor at the application level. We are using RCSI at the database level.

Our update stored procedure is working properly.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-15 : 16:01:07
does the update look like what I posted?

And is EXEC() executed in a separate thread as I believe?

I was able to update the table even when it was inside a tran WITH a lock...I think



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-15 : 16:12:26
I don't see enough of Nigel anymore

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=29228



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -