Oracle对一张表进行插入(Insert)和更新(Update) 使用一条Sql语句(Merge Into)

--更新表te_m_recheck_info ,如果表te_m_recheck_info 存在记录AAA 则执行更新操作,否则执行insert操作

merge into te_m_recheck_info m--该表是需要更新或插入的表  
using te_m_manualalert_info_new bb -- 关联表  
on (m.s_manualertid ='AAA')--这种方式比较安全,给出 指定条件进行匹配
--( m.s_manualertid = bb.s_manualertid) --也可使用该语句进行全表关联( 小心会更新全表数据)  
when matched then --匹配关联条件,作更新处理  
   update set m.c_depetype=bb.c_depetype --可以多个值进行更新,用逗号分隔开(同Sql中的Update后的set写法)
 where m.s_manualertid='AAA' and bb.s_manualertid = 'AAA'   --两张表同时存在AAA时才进行更新
when not matched then --不匹配关联条件,作插入处理。 
  insert
  values
    ('a',
     bb.s_manualertid,
     to_date('2018-04-03 12:00:00','yyyy-MM-dd hh24:mi:ss'),
     'a',
     'a')

     where bb.s_manualertid='2b5d80e5-3153-44eb-a85d-7f0116071c80'


发布了28 篇原创文章 · 获赞 15 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/z3h0a5n8g8x9i9a2o3/article/details/79805407