简体   繁体   中英

update column using ROW_NUMBER

I'm trying to update a column using ROW_NUMBER(), but just works sometimes, what's wrong? This is the instruction:

update temp1 set temp1.RowNumber = m.RowID
from
(
    select ROW_NUMBER() OVER (ORDER BY t.id_pv) as 'RowID', id_pv
    from temp1 t
) m
where temp1.id_pv = m.id_pv

Here are two columns of the table before and after update

在此处输入图片说明

If I run only:

select ROW_NUMBER() OVER (ORDER BY t.id_pv) as 'RowID', id_pv from temp1 t

it works like a charm. Ideas?

If id_pv is not unique, then the select won't produce stable results. You may need to add another column that is unique to the ORDER BY clause to stabilize the sort. That should produce repeatable results.

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