Use the TOP clause to restrict the data _mssql UPDATE statement updates

Source: Internet
Author: User

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 the randomly selected rows of N. For example, suppose you want to reduce the sales burden for a senior salesperson and assign some customers to a junior salesperson. The following example assigns 10 customers who are randomly sampled from one salesperson to another.

 Use ADVENTUREWORKS2008R2;
 UPDATE top Sales.Store
 SET SalesPersonID = 276
 WHERE SalesPersonID = 275;
 Go

If you need to use top to apply updates that are in a meaningful chronological order, you must use both the top and the ordered by clauses. The following example updates the number of vacation hours for the first 10 employees hired.

 UPDATE humanresources.employee
 SET vacationhours = vacationhours + 8 from
 (SELECT up BusinessEntityID from Hum Anresources.employee ORDER by
   hiredate ASC) as th
 WHERE HumanResources.Employee.BusinessEntityID = th. BusinessEntityID;
 Go

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

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.