Use the TOP clause to limit the data updated by the UPDATE statement. topupdate

Source: Internet
Author: User

Use the TOP clause to limit the data updated by the UPDATE statement. topupdate

You can use the TOP clause to limit the number of rows modified in the UPDATE statement. When the TOP (n) clause is used with UPDATE, the delete operation is performed on n randomly selected rows. For example, assume that you want to relieve the sales burden for a senior salesperson and assign some customers to a junior salesperson. In the following example, 10 customers randomly sampled are allocated to one salesperson.

 USE AdventureWorks2008R2; UPDATE TOP (10) Sales.Store SET SalesPersonID = 276 WHERE SalesPersonID = 275; GO

If you want to use TOP to apply updates in meaningful chronological ORDER, you must use both the TOP and order by clauses. The following example updates the number of vacation hours for the first 10 employees.

 UPDATE HumanResources.Employee SET VacationHours = VacationHours + 8 FROM (SELECT TOP 10 BusinessEntityID FROM HumanResources.Employee   ORDER BY HireDate ASC) AS th WHERE HumanResources.Employee.BusinessEntityID = th.BusinessEntityID; GO

Reference: http://technet.microsoft.com/zh-CN/library/ms180971


What is the purpose of using the where clause in an update statement?

The statement following the where clause indicates that only data filtered Based on the where clause is updated.
 
Analysis of Select For update statements in the database

They have already said something about it.
I would like to add a few more points:
There are two types: Lock range clause and lock action clause.

Lock range clause:
In select... After for update, you can use the of clause to select a specific data table for the select statement. By default, the "of" clause is not used to indicate that all data tables in the select statement are locked.
Lock action clause:

When we perform the for update operation, it is very different from the normal select operation. Generally, you do not need to consider whether the data is locked. You can read the previous version based on the features of Multi-version consistent read. After adding for update, Oracle requires a new transaction to start and try to lock the data. If the lock has been applied, the default action must be block wait. The nowait clause is used to avoid waiting. When the requested lock resource is found to be locked and not released, an error is returned directly.
In daily use, we still use for update, especially when manually modifying data in pl/SQL developer. At this time, I just thought it was convenient, but I didn't understand the true meaning of for update.
For update is a special statement provided by Oracle to manually increase the lock level and range. Oracle locks are currently excellent in various types of database locks. Therefore, Oracle does not require direct lock Control and Improvement by users and applications. It is even believed that most of the problems related to deadlocks are related to manual locking. Therefore, for update is not recommended for routine development in Oracle. In addition, in normal development and O & M, if you use for update but forget to submit it, many lock table faults will occur.

So when do I need to use for update? That is, you can consider using for update when data exclusive to the business layer is required. In scenarios, such as ticket booking, stamps are displayed on the screen. When a ticket is issued, you need to confirm that the data is not modified by other clients. Therefore, you can use for update during this confirmation process. This is a unified solution and requires preliminary preparation.

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.