SQL Server Update lock)

Source: Internet
Author: User

Updlock

The update lock is used when reading the table, instead of the shared lock, and the lock is kept until the statement or transaction ends. Updlock allows you to read data (without blocking other transactions) and update data later. It also ensures that the data has not been changed since the last read.

This is the description of the update lock in sqlserver2000.

When we use updlock to read records, we can add an update lock to the obtained records, therefore, the lock record cannot be changed in other threads and can only be changed after the transaction of the thread ends. For example:

Begin transaction -- start a transaction
Select Qty
From mytable with (updlock)
Where ID in (1, 2, 3)

Update mytable set qty = qty-A. Qty
From mytable as
Inner join @ _ table as B on A. ID = B. ID

Commit transaction -- submit a transaction

In this way, other threads or transactions cannot change the records whose IDs are 1, 2, and 3 before these statements are executed. other statements can be modified and read. values 1, 2, and 3 can only be read. If they are modified, they can only be operated after these statements are completed. this ensures that the data is correctly modified.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.