[英]Update Oracle table column with row number
I want to update a table column with row number. 我想用行号更新表列。 Each row in
empid
column should update with related row number. empid
列中的每一行都应使用相关的行号进行更新。 I tried following query. 我试过以下查询。
UPDATE employee SET empid = row_number();
But this is not working. 但这不起作用。 Any idea?
任何的想法?
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). 首先,这不是
row_number()
函数的正确语法,因为您缺少over
子句(导致ORA-30484错误)。 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). 即使它是,这也行不通,因为你不能直接在
set
子句中使用窗口函数(导致ORA-30483错误)。
For this usecase, however, you could just use the rownum
pseudo-column: 但是,对于此用例,您可以使用
rownum
伪列:
UPDATE employee SET empid = ROWNUM;
You could do something like the following. 您可以执行以下操作。 You can change the ORDER BY order the rows if needed.
如果需要,您可以更改行的ORDER BY顺序。
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();
UPDATE员工SET empid = row_number();
Firstly, it is syntactically incorrect. 首先,它在语法上是不正确的。
Secondly, you cannot use ROW_NUMBER()
analytic function without the analytic_clause . 其次,您不能使用
ROW_NUMBER()
没有analytic_clause解析函数。
As you replied to my comment that the order doesn't matter to you, you could simply use ROWNUM . 当您回复我的评论时,订单对您无关紧要,您可以简单地使用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.
由于您要分配EMPID ,我建议您考虑订购。
Usually employee ids are generated using a SEQUENCE object. 通常,员工ID是使用SEQUENCE对象生成的。 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)
(注意如果你想让它充当pk,这不会被设置为PK)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.