![](/img/trans.png)
[英]Update 3 column in same time 2nd table all field varchar update help of phone number
[英]Update Oracle table column with a sequence number based on varchar field in same table
我需要有關語法的幫助,該語法根據另一個字段VARCHAR2的順序使用新的數字序列更新oracle表中的現有字段。
當前表:
CODE CODE_ORDER
A00 3
A00.0 4
A00.1 6
A00.9 8
期望的結果:
CODE CODE_ORDER
A00 1
A00.0 2
A00.1 3
A00.9 4
此處的SELECT語句為我提供了所需的結果,但是當我將它與UPDATE語句一起使用時,會收到錯誤消息。
UPDATE Table SET code_order = (select row_number() over (order by
code asc) from Table)
UPDATE語句顯示此錯誤:
ORA-01427: single-row subquery returns more than one row
預先感謝您的任何建議。
您可以這樣做:
merge
into table
using (
select code,row_number() over (order by code asc) as new_code_order from table
) new_table
on (table.code = new_table.code)
when matched then update set
table.code_order = new_table.new_code_order
我認為這在以下簡單情況下也可以:
update (select * from table order by code) set code_order = rownum;
您需要在SET子句中使用子查詢,如下所示:
UPDATE Table t SET t.code_order = (
Select tin1.rn from
(select row_number() over (order by tin.code asc) rn, tin.code from Table tin) tin1
Where t.code = tin1.code)
注意 :在代碼列中不存在重復值的情況下,該查詢將適用於示例數據。
干杯!!
您可以使用with..as
子句
update tab t
set code_order = ( with tb as (
select t.*,row_number() over (order by code) as rn from tab t
)
select rn
from tb
where tb.code=t.code)
考慮到code
列的值是唯一的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.