【数据库】Oracle实现判断条件批量修改功能

背景:

    由于拆库项目发展,需要对表进行简单重构;业务要求迁移的表需要存在 ‘添加人ID’,‘添加人’,‘添加时间’,‘修改人ID’,‘修改人’,‘修改时间’几个字段,原表不存在的这几个字段需要进行新增字段。并且要求这几个字段都应设置‘不可为null。

   就需要在保证新增的数据已经存在默认值的情况下,对原来的数据进行修复再做数据转移(如图将每个为null的字段分别进行赋值);


update TABLE_NAME
set 
  CREATE_USER_ID = case  when CREATE_USER_ID is null then 333 else CREATE_USER_ID end,
  CREATE_USER_NAME =  case when CREATE_USER_NAME is null then'系统' else CREATE_USER_NAME end,
  CREATE_DATETIME= case when CREATE_DATETIME is null then sysdate else CREATE_DATETIME end,
  UPDATE_USER_ID= case when UPDATE_USER_ID is null then 333 else UPDATE_USER_ID  end,
  UPDATE_USER_NAME= case when UPDATE_USER_NAME is null then '系统' else UPDATE_USER_NAME  end,
  UPDATE_DATETIME= case when UPDATE_DATETIME is null then sysdate else UPDATE_DATETIME end
where 
CREATE_USER_ID is null or CREATE_USER_NAME is null or CREATE_DATETIME  is null or 
UPDATE_USER_ID is null or UPDATE_USER_NAME is null or UPDATE_DATETIME is null  ;

猜你喜欢

转载自blog.csdn.net/CountryShi/article/details/109065930