Update Oracle table column with row number

两盒软妹~` 提交于 2019-12-04 08:56:08

First, this is not the correct syntax for the row_number() function, since you're missing the over clause (resulting in an ORA-30484 error). Even if it was, this would not work, as you cannot directly use window functions in a set clause (resulting in an ORA-30483 error).

For this usecase, however, you could just use the rownum pseudo-column:

UPDATE employee SET empid = ROWNUM;

SQLFiddle

You could do something like the following. You can change the ORDER BY order the rows if needed.

UPDATE emp
SET empid = emp.RowNum
FROM (SELECT empid, ROW_NUMBER() OVER (ORDER BY empid) AS rowNum FROM employee) emp

UPDATE employee SET empid = row_number();

Firstly, it is syntactically incorrect.

Secondly, you cannot use ROW_NUMBER() analytic function without the analytic_clause.

As you replied to my comment that the order doesn't matter to you, you could simply use ROWNUM.

UPDATE employee SET empid = ROWNUM;

It will assign the pseudo-column value by randomly picking the rows. Since you are assigning EMPID, I would suggest you should consider ordering.

Usually employee ids are generated using a SEQUENCE object. There are two ways to implement the auto-increment functionality:

you could also do this

create table your_table_name as
select row_number() over( order by 1) as serial_no, a.* from your_query a

this creates the serial number when you write the table itself. ( note this is not set as PK if you want it to act as pk)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!