简体   繁体   中英

Update table using rownum

I have a table that I would like to update in oracle. It's okay to assume that the rownum are in sequential order from 1 through 7

Table Have
1
2
3
4
4
4
4


Table Want  
1
2
3
4
5
6
7

If I get you right you need something like UPDATE all your duplicated (in your case value = 4) rows by adding (rownum -1).

See example below.

create table have as 
select case when rownum <= 4 then rownum else 4 end col1 from dual connect by level <= 7;

select col1 from have order by col1;
      COL1
----------
         1 
         2 
         3 
         4 
         4 
         4 
         4 

update have
set col1 = col1 + rownum -1  where col1 = 4;

select col1 from have order by col1;

      COL1
----------
         1 
         2 
         3 
         4 
         5 
         6 
         7

It seems that you want to have unique sequential numbers in that column.

If you don't care much about conditions , you could even

update have set col1 = rownum;

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