简体   繁体   中英

Locking Row in SQL 2005-2008

有没有办法在不启动事务的情况下锁定SQL 2005-2008数据库中的行,因此其他进程在解锁之前无法更新该行?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM