Locking Row in SQL 2005-2008

被刻印的时光 ゝ 提交于 2019-11-30 13:57:55

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.

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.

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)

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