Oracle 使用MERGE INTO 语句更新数据
转http://www.cnblogs.com/jiangguang/archive/2012/12/24/2830581.html
/*Merge into 详细介绍
MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。
通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,
连接条件匹配上的进行UPDATE,无法匹配的执行INSERT。
这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE。
*/
/*语法:
MERGE [INTO [schema .] table [t_alias]
USING [schema .] { table | view | subquery } [t_alias]
ON ( condition )
WHEN MATCHED THEN merge_update_clause
WHEN NOT MATCHED THEN merge_insert_clause;
*/
merge into users
using doctor
on (users.user_id = doctor.doctorid)
when matched then
update set users.user_name = doctor.doctorname --update 和 insert 语句均可以简写,省略 表名等
when not matched then
insert
values
(doctor.doctorid,
doctor.doctorid,
'8736F1C243E3B14941A59FF736E1B5A8',
doctor.doctorname,
sysdate,
'T',
' ',
doctor.deptid,
'b319dac7-2c5c-496a-bc36-7f3e1cc066b8');
限定条件位置:
merge into MDC_DiabetesRecord a
using EHR_HealthRecord b
on (a.empiid = b.empiid and a.phrid='3301850040200000' ) --限制条件 a.phrid='3301850040200000' 可以 置于 condition 处
when matched then
update set a.status = '9' ;
merge into MDC_DiabetesRecord a
using EHR_HealthRecord b
on (a.empiid = b.empiid )
when matched then
update set a.status = '9' where a.phrid='3301850040200000' ; --可以 置于 update 处