欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

oracle merge语句

程序员文章站 2022-04-13 11:06:55
...

用OP_TYPE和DOC_TYPE跟表TR_RUNNING_MONITOR中的这两个字段比较,相同时进行update,不同时进行insert

merge into 表名 a using

(select ? as 字段1,? as 字段2,..... from dual)

b on (a.字段1=b.字段1 and ....)(判断的条件)

when matched then

update set 字段1=字段1,......

when not matched then

insert (字段1,字段2,...) values(b.字段1,b.字段2,....)

例子如下:表名:TR_RUNNING_MONITOR ;字段名:LAST_TASK_ROWID,LAST_CRAWL_DATE,START_TIME,DOC_COUNT,HIT_COUNT,OP_TYPE,DOC_TYPE

merge into TR_RUNNING_MONITOR a using
(select ? as LAST_TASK_ROWID,? as LAST_CRAWL_DATE,? as START_TIME,? as DOC_COUNT,? as HIT_COUNT,? as OP_TYPE,? as DOC_TYPE,sysdate as UPDATE_TIME from dual) b on (a.OP_TYPE=b.OP_TYPE and a.DOC_TYPE=b.DOC_TYPE) when matched then
update set DOC_COUNT=DOC_COUNT,HIT_COUNT=HIT_COUNT,START_TIME=START_TIME,UPDATE_TIME=UPDATE_TIME,
LAST_CRAWL_DATE=LAST_CRAWL_DATE,LAST_TASK_ROWID=LAST_TASK_ROWID when not matched then
insert (OP_TYPE,DOC_TYPE,DOC_COUNT,HIT_COUNT,START_TIME,UPDATE_TIME,LAST_CRAWL_DATE,LAST_TASK_ROWID)
values(b.OP_TYPE,b.DOC_TYPE,b.DOC_COUNT,b.HIT_COUNT,b.START_TIME,b.UPDATE_TIME,b.LAST_CRAWL_DATE,b.LAST_TASK_ROWID)


说明:用OP_TYPE和DOC_TYPE跟表TR_RUNNING_MONITOR中的这两个字段比较,,相同时进行update,不同时进行insert

oracle merge语句