I have a table in Oracle SQL whose ids are in increasing, sequential order, but there are gaps in the ids due to editing, e.g. the ids are currently something like
You could use a merge
, but you'd need to join on something other than emp_id
as you want to update that column. If there are no other unique columns you can use the rowid
:
merge into employee target
using (select rowid, row_number() over (order by emp_id) as emp_id from employee) source
on (source.rowid = target.rowid)
when matched then update set target.emp_id = source.emp_id;
This solution to the same question you referenced shows how to do it:
update employee set emp_id = (
with tab as (
select emp_id, rownum r
from (select emp_id from employee order by emp_id)
)
select r from tab where employee.emp_id = tab.emp_id
);
That works. You cannot update a view that contains an analytic function like row_number - see Oracle 12C docs, look for "Notes on Updatable Views".
I think this would be easiest approach :
update mytable set id = ROWNUM;
Oracle SQL - update ids in oracle sql to be in sequential order