日期:2014-05-16 浏览次数:20426 次
MERGE INTO mn a USING (select count(*) co from mn where mn.ID=4) b ON (b.co<>0)--这里使用了count和<>,注意下,想下为什么! WHEN MATCHED THEN UPDATE SET a.NAME = 'E' where a.ID=4 WHEN NOT MATCHED THEN INSERT VALUES (4, 'E');
--测试数据 create table table1(id varchar2(100),name varchar2(1000),address varchar2(1000)); insert into table1(id,name,address)values('01001','影子','河北') ; commit; --插入 merge into table1 t1 using (select '01002' id,'影子' name,'河北' address from dual) t2 on (t1.id = t2.id) when matched then update set t1.name = t2.name, t1.address = t2.address when not matched then insert values (t2.id, t2.name,t2.address); commit; --查询结果 select * from table1 01001 影子 河北 01002 影子2 辽宁 --更新 merge into table1 t1 using (select '01001' id,'不是影子' name,'山西' address from dual) t2 on (t1.id = t2.id) when matched then update set t1.name = t2.name, t1.address = t2.address when not matched then insert values (t2.id, t2.name,t2.address); commit; --查询结果 select * from table1 01001 不是影子 山西 01002 影子2 辽宁 --删除测试数据 drop table table1;