Updating Top Records in SQL Server Using TOP Clause
To update a specific number of records, such as the top 100, in SQL Server, you can utilize the TOP clause. This clause allows you to specify a limit on the number of rows affected by an update statement.
For instance, consider the scenario where you have a table named T1 with fields F1 and F2, containing a total of 200 records. To update the F1 field in the top 100 records, you can use the following query:
UPDATE TOP (100) T1 SET F1 = 1
Note that parentheses are mandatory for UPDATE statements. By specifying TOP (100), you ensure that only the first 100 rows will be updated, leaving the remaining 100 records unaffected.
The above is the detailed content of How Can I Update Only the Top N Records in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!