Locking Row in SQL 2005-2008

独自空忆成欢 提交于 2019-12-18 16:48:10

问题


Is there a way to lock a row in the SQL 2005-2008 database without starting a transaction, so other processes cannot update the row until it is unlocked?


回答1:


You can use RowLock or other hints but you should be careful..

The HOLDLOCK hint will instruct SQL Server to hold the lock until you commit the transaction. The ROWLOCK hint will lock only this record and not issue a page or table lock.

The lock will also be released if you close your connection or it times out. I'd be VERY careful doing this since it will stop any SELECT statements that hit this row dead in their tracks. SQL Server has numerous locking hints that you can use. You can see them in Books Online when you search on either HOLDLOCK or ROWLOCK.




回答2:


Everything you execute in the server happens in a transaction, either implicit or explicit.

You can not simply lock a row with no transaction (make the row read only). You can make the database read only, but not just one row.

Explain your purpose and it might be a better solution. Isolation levels and lock hints and row versioning.




回答3:


Do you need to lock a row, or should Sql Server's Application locks do what you need?

An Application Locks is just a lock with a name that you can "lock", "unlock" and check if it is locked. see above link for details. (They get unlocked if your connection gets closed etc, so tend to clean themselfs up)



来源:https://stackoverflow.com/questions/111652/locking-row-in-sql-2005-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!