Oracle 用一个表字段更新另一个表字段三种方法

  ---方法一,无法修改与非键值保存表对应的列
  update (select t1.F_HRORG a1, t2.F_GZBM_ID b1
        from hr_users_common t1 , IC_HRORG t2

       where t1.F_RZBM =t2.F_ICS_DEPT_NO and  t1.F_RZBM !=F_BY_T08 AND F_YGH not in ( SELECT DISTINCT F_YGBH FROM  HR_YGDD) 
       )
   set a1 = b1;
   结论:

      用A表去更新B表的数据,A表的关联条件必须为主键,Oracle这样做的目的是保证表A的满足关联条件的数据是唯一的,

      这样在更新B表数据时才有意义(自己都不确定怎么影响别人,是吧,hehe),

      当然,如果两表关联的字段都为主键,则无论谁更新谁都没有问题。

      结论:用A表数据更新B表数据,则A与B的对应关系为:1:1 或 1:n。

  ---方法二, 2.只能单行子查询s

update  CITY_PROJECT_SCALE_INFO c  set
(c.value) = (
       select d.value from CITY_PROJECT_SCALE_INFO @test d where d.project_id = '7d7fd580a06240b2a9137dc2bbe831e9'
          and d.project_id = c.project_id and c.company_code = d.company_code
)
 where exists (
  select 1 from  CITY_PROJECT_SCALE_INFO @test d where d.project_id = '7d7fd580a06240b2a9137dc2bbe831e9'
          and d.project_id = c.project_id and c.company_code = d.company_code
)
  ---方法三
  merge into hr_users_common  t1
using IC_HRORG t2
on( t2.F_ICS_DEPT_NO=t1.F_RZBM )
when matched then
update SET t1.F_HRORG=t2.F_GZBM_ID;

  
  UPDATE hr_users_common T SET T.F_HRORG =(SELECT F_GZBM_ID from IC_HRORG U WHERE U.F_ICS_DEPT_NO=T.F_RZBM) 
  WHERE  T.F_RZBM !=F_BY_T08 AND F_YGH not in ( SELECT DISTINCT F_YGBH FROM  HR_YGDD)  ;

 

 

 

1. update (select .....) set column1 = column2;

update (select iy.company_name company_name1, cc.company_name_jc company_name2
        from income_year_item iy , city_company cc

       where iy.company_code = cc.code
       )
   set company_name1 = company_name2;

2.只能单行子查询s

update  CITY_PROJECT_SCALE_INFO c  set
(c.value) = (
       select d.value from CITY_PROJECT_SCALE_INFO @test d where d.project_id = '7d7fd580a06240b2a9137dc2bbe831e9'
          and d.project_id = c.project_id and c.company_code = d.company_code
)
 where exists (
  select 1 from  CITY_PROJECT_SCALE_INFO @test d where d.project_id = '7d7fd580a06240b2a9137dc2bbe831e9'
          and d.project_id = c.project_id and c.company_code = d.company_code
)

3.使用merg inot 语句

--更新生产基础字段
merge into city_cfg_data_column_common cf1 using
      city_cfg_data_column_common2 cf2 on
      (cf1.resourceid = cf2.resourceid)
   when matched then
        update set   cf1.template_type = cf2.template_type,
                     cf1.chinese_name = cf2.chinese_name,
                     cf1.column_name = cf2.column_name,
                     cf1.column_type = cf2.column_type,
                     cf1.column_size = cf2.column_size
  when not matched then
       insert (cf1.resourceid,cf1.template_type,cf1.chinese_name,cf1.column_name,cf1.column_type,cf1.column_size
              ,cf1.is_can_edit,cf1.is_unique,cf1.is_can_cover,cf1.show_order)
       values (cf2.resourceid,cf2.template_type,cf2.chinese_name,cf2.column_name,cf2.column_type,cf2.column_size
              ,cf2.is_can_edit,cf2.is_unique,cf2.is_can_cover,cf2.show_order)

 

转载:https://blog.csdn.net/e_wsq/article/details/52062562

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值