Oracle Merge Into使用
merge into的作用就是当条件存在,则更新,反之则新增。
1.两张表关系
merge into arvin_test ats
using (
select distinct
cov_1.vendor_id,--供应商ID
cov_1.segment1, --供应商编码
cov_1.vendor_name, --供应商名称
cov_1.vendor_name_alt, --供应商简称
to_date('1900-1-1 00:00:00','yyyy-mm-dd,hh24:mi:ss') as start_date_active, --生效时间
cov_1.end_date_active, --失效时间
'Z020001' as data_source --数据来源
from apps.cux_oms_vendor_site_v cov_1) cov
on (ats.vendor_id=cov.vendor_id)
when matched then
update set
ats.vendor_name=cov.vendor_name,
ats.vendor_name_alt=cov.vendor_name_alt,
ats.end_date_active=cov.end_date_active
when not matched then
insert(
ats.vendor_id,
ats.vendor_code,
ats.vendor_name,
ats.vendor_name_alt,
ats.start_date_active,
ats.end_date_active,
ats.data_source
)
values(
cov.vendor_id,
cov.segment1,
cov.vendor_name,
cov.vendor_name_alt,
cov.start_date_active,
cov.end_date_active,
cov.data_source)
2.单张表
merge into t_sys_test ts
using dual
on (ts.name = 'lisi')
when not matched then
insert (id, name, code) values (sys_guid(), 'lisi', '23432')
这里的using dual也可以写成using (select 1 from dual),on 后面的条件必须使用括号包住,里面的条件可以为多个
3.在mybatis中使用
Mybatis中使用,需要使用update标签。