oracle資料庫中有乙個表formtable_main_159_dt1結構如下
mainid,id
161,100,
161,101,
161,102,
162,103
162,104
163,105
現在增加乙個字段序號,實現如下的效果
mainid,id,xuhao
161,100,1
161,101,2
161,102,3
162,103,4
162,104,5
163,105,6
先使用如下語句實現出效果。
select row_number() over(partition by mainid order by id ) as xh,id,mainid from formtable_main_159_dt1
然後用update語句去更新新增欄位中的值;
最先用:update t1 set t1.c1=(select c2 from t2 where t1.id=t2.id) where exists (select 1 from t2 where t2.id=t1.id)
update formtable_main_159_dt1 set xuhao=(select xuhao from (select row_number() over(partition by mainid order by id ) as xuhao ,id from formtable_main_159_dt1) a1 where formtable_main_159_dt1.id=a1.id)
where
exists(select 1 from formtable_main_159_dt1,(select row_number() over(partition by mainid order by id ) as xuhao ,id from formtable_main_159_dt1) a1 where formtable_main_159_dt1.id=a1.id)
執行時間很長,而且語句寫的感覺及其囉嗦。
於是換一種寫法:update (select t1.c1,t2.c2 from t1,t2 where t1.id=t2.id) t set c1=c2
update ( select a1.xuhao , a2.xh from formtable_main_159_dt1 a1,(select row_number() over(partition by mainid order by id ) as xh,id from formtable_main_159_dt1) a2 where a1.id=a2.id ) t set xuhao=xh
於是再換一種寫法:
update ( select xuhao, row_number() over(partition by mainid order by id ) as xh from formtable_main_159_dt1) t set xuhao=xh
結果又是提示錯誤: ora-01732: 此檢視的資料操縱操作非法
原來這種update寫法需要主鍵的。
再換寫法:merge into t1 using (select t2.c2,t2.id from t2) t on (t.id=t1.id) when matched then update set t1.c1=t.c2
merge into formtable_main_159_dt1 a1 using (select row_number() over(partition by mainid order by id ) as xh,id,mainid from formtable_main_159_dt1) a2 on (a1.id=a2.id)
when matched then update
set a1.xuhao=a2.xh
現在好了,執行很快。
oracle關聯表更新
如果有a表和b表,sql server中用 update a set field2 b.filed2 from a,b where a.field1 b.field1搞定,所以用慣了sql server的更新表語句,再用oracle的那真是特別的拗口。情景一 如果只是更新固定值,oracle和sql...
oracle二表更新
oracle update stockm set stockm.current cost select erpcost from aes calist where trim aes calist.wh trim stockm.warehouse and trim aes calist.product...
oracle多表關聯更新
1.首先將其他表的資料抽取到一張臨時表裡面。create table temp dim2 as select t.stdaddr,s.dzbm from demp dim t,dzmlpxz pt s where t.sdaddr s.mc 2.進行分組查詢,看看裡面的記錄是否有重複的。select...