简体   繁体   中英

Update table based on row_number in SQL Server

I have a table TBL(int x, int y)

I want to update x based on row number

Something like this

UPDATE TBL 
SET X = "BLA"
WHERE ROW_NUMBER BETWEEN 1 AND 20

In the example given in question, you need to update X based on row number. So consider Y is your primary key :

update t1
set t1.X = 'BLA'
from TBL  t1
join (select X, 
            Y, 
           row_number() over(order by ID) rnum
      from TBL) t2
on t1.Y = t2.Y /*since Y is the primary key*/
where t2.rnum between 1 and 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM