Wednesday, March 7, 2012

keeping a lock on table or row

I need to update a row but keep a lock on the table (so no one else can update it) while I do run some more code. In Oracle, it always locks whatever you update until you hit commit, but sql server works opposite. How do I tell it not to commit a statement, or how would I explicitly get a lock and then release it later?You can do it with optimizer hints:

HOLDLOCK, UPDLOCK, PAGLOCK, TABLOCK, TABLOCKX|||thanks for your reply!

Would you elborate for me how to do that, I'm not familiar with SQL Server.

I would like to be able to do this in a single statement if possible, perhaps something to append to a regular update statement to hold the lock? (I tried appending 'holdlock' to the statement but that dosen't appear to be correct syntax)

thanks|||Not True

BEGIN TRAN

Some code....

won't COMMIT until you COMMIT the TRAN with COMMIT TRAN...

Sure in SQL you have to start the transaction..

and in Oracle that's a setting that you can change...

Also, I really hope this is not for an application your building and just in SQL+

In any event getting in and out as FAST as possible should be the way to think...regardless of what you're doing...|||begin tran
select * from your_table (TABLOCK) where 1=2
update your_table set...where...
commit tran

No comments:

Post a Comment