要求:更新采购申请明细行的行号row_id字段,按照单据分组,明细行id排序,让row_id从1开始更新
update scm_dem_purchase_req_dtl_tbl
set row_id = rownum
where bill_id =139505
order by id;
--这样写报错,应为update与order by 不能一起使用,做一下的修改
逻辑:
使用分析函数:row_number() over(order by id) rn,以id排序,查出rn,用以更新值;
然后自关联表更新 row_id 为 查出的符合要求的rn字段,即可!!
哈哈,分析函数很好用~~~
update scm_dem_purchase_req_dtl_tbl t
set row_id =(select rn
from (select bill_id, id, row_number() over(order by id) rn
from scm_dem_purchase_req_dtl_tbl tt
where bill_id = 139505)tt
where t.id = tt.id)
where bill_id = 139505;