使用Oracle sys_guid()生成的ID更新列值时遇到的一个问题

        以下sql,本意是想按vessel_cd, syear, smonth, senddate分组,每组生成一个ID,然后把这个生成的ID更新到每一级数据的m_tab_id的列。执行完后,结果发现相同vessel_cd, syear, smonth, senddate的数据行m_tab_id列会存在不一样的值,如图。百思不得其解。

update ami_month_oil_lube t
   set t.M_TAB_ID =
       (select id
          from (select sys_guid() id, vessel_cd, syear, smonth, senddate
                  from ami_month_oil_lube
                 where vessel_cd is not null
                   and syear = 2018
                 group by vessel_cd, syear, smonth, senddate) t1
         where t1.vessel_cd = t.vessel_cd
           and t1.syear = t.syear
           and t1.smonth = t.smonth
           and t1.senddate = t.senddate)
 where vessel_cd is not null
   and syear = 2018
   and smonth = 2;
commit;
select fileid, M_TAB_ID, vessel_cd, syear, smonth, senddate
  from ami_month_oil_lube
 where vessel_cd = 'OKA'

        第二天突然想到,sys_guid()每执行一次,产生的值都会不一样。同组数据的m_tab_id值不一样,说明更新这一组数据的m_tab_id值时,可能是分了多次去执行来更新。为了避免这种情况出现,改进了一下sql,先把按组生成的ID保存下来,然后再执行Update。这样,终于全部的数据都更新正确了。

create table ami_month_oil_main_tem as
  select sys_guid() id, vessel_cd, syear, smonth, senddate
    from ami_month_oil_lube
   where vessel_cd is not null
     and syear = 2018
   group by vessel_cd, syear, smonth, senddate;
   
update ami_month_oil_lube t
   set t.M_TAB_ID =
       (select id
          from ami_month_oil_main_tem t1
         where t1.vessel_cd = t.vessel_cd
           and t1.syear = t.syear
           and t1.smonth = t.smonth
           and t1.senddate = t.senddate)
 where vessel_cd is not null
   and syear = 2018
   and smonth = 2;
   
drop table ami_month_oil_main_tem;

commit;

select fileid, M_TAB_ID, vessel_cd, syear, smonth, senddate
  from ami_month_oil_lube
 where vessel_cd = 'OKA'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值