增值服务包含哪些产品/福鼎网站优化公司
Merge into是Oracle 9i引入的功能
问题来源:在插入表中数据时,需要检查这个表中是否已经存在这条数据,如果有则进行更新,如果没有则进行插入。
那么问题来了,都有哪些方式可以实现:
1.用简单的办法:
①先查询 select count(1) from dual where XXX;
②在进行插入或者更新
2.用sql server中的一个办法
if exists(select 1 from where T.a='1001' )update T set T.b=2 Where T.a='1001'
else insert into T(a,b) values('1001',2);
3.简单粗暴的用Oracle 9i之后引入的功能 Merge into
MERGE INTO A t1USING (select count(*) c from A t2 where t2.key='key1' )xON (x.c > 0)when matched then update set t1.value='test', t1.version = sysdate where t1.key='key1when not matched then insert values('值1','值2','值3',sysdate,systimestamp);commit;select t.*,t.rowid from A t where t.key='key1';
示例脚本,效果图:
MERGE INTO PICTURE_SOURCE_PATH p1
USING ( select count(*) c from picture_recognition_info p2 where p2.receive_num='10010' ) x
ON (x.c > 0) --表示条数大于1条
when matched then update set p1.channel_mark='01',p1.function_code='A02',p1.insert_date=sysdate,p1.picture_path='E:\picture\bank.jpg',p1.identify_sign='0' where p1.receive_num='10010'
when not matched then insert values(sys_guid(),'01','10010','A02',sysdate,'E:\picture\bank.jpg','0',systimestamp);
commit;