首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Oracle存在则更新,不存在则安插应用

2012-09-10 
Oracle存在则更新,不存在则插入应用更新同一张表的数据。需要注意下细节,因为可能涉及到using的数据集为nul

Oracle存在则更新,不存在则插入应用
更新同一张表的数据。需要注意下细节,因为可能涉及到using的数据集为null,所以要使用count()函数。

MERGE INTO mn aUSING (select count(*) co from mn where mn.ID=4) bON (b.co<>0)--这里使用了count和<>,注意下,想下为什么!WHEN MATCHED THENUPDATESET a.NAME = 'E'where a.ID=4WHEN NOT MATCHED THENINSERTVALUES (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 t1using (select '01002' id,'影子' name,'河北' address from dual) t2on (t1.id = t2.id)when matched then     update set t1.name = t2.name, t1.address = t2.addresswhen not matched then     insert values (t2.id, t2.name,t2.address);commit;--查询结果select * from table101001    影子    河北01002    影子2    辽宁--更新merge into table1 t1using (select '01001' id,'不是影子' name,'山西' address from dual) t2on (t1.id = t2.id)when matched then     update set t1.name = t2.name, t1.address = t2.addresswhen not matched then     insert values (t2.id, t2.name,t2.address);commit;--查询结果select * from table101001    不是影子    山西01002    影子2    辽宁--删除测试数据drop table table1;
1 楼 zui4yi1 2012-02-22   很可惜,不能像server 2008一樣,有
hen not matched by source/target/both的by語法,不然就更靈活了。

热点排行