SQL Server how to update a column for a desired row number

Any idea how I can update a column, but only for row number=1 to row number=10 for example?

+6
source share
2 answers

This uses a view to isolate the 10 rows that you want to update. Note: they both have ORDER BY to define 10 rows

 UPDATE T SET SomeColumn = @newValue --or constant etc FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY something) AS rn FROM SomeTable WHERE ... ) T WHERE rn <= 10 
+4
source

Typically, on an Sql server, an update statement is written as

 Update <Table Name> Set <Column Name> = <Value> where <Search Condition>. 
+1
source

Source: https://habr.com/ru/post/892973/


All Articles