Oracle中merge into语法

时间:2023-03-09 17:39:55
Oracle中merge into语法

merge into 语句就是insert和update的一个封装,简单来说就是:

有则更新,无则插入

下面说怎么使用

MERGE INTO table_Name  T1(匿名)

using (另外一张表,或者是查询出来的部分数据)T2

on(条件)   注意:ON条件里的字段在后面是不能操作的,(epm.ID=T2.ID),那么在后面无论更新和操作都不能对emp.ID进行

when matched then

语句1,

语句2  (最后不能有分号)

when  not  matched then

语句1,

语句2;

下面是例子:

 --利用merge into语法往表中插入数据
MERGE INTO DIM_TIME T1
USING (SELECT DATE'2013-1-1'+(ROWNUM-1) as Date_Name FROM dual CONNECT BY rownum <=
(date'2014-1-1'-date'2013-1-1')) temp_Date
on (T1.Date_Name =temp_Date.Date_Name)
when matched then
update set T1.Date_ID=to_char(temp_Date.Date_Name,'YYYYMMDD')
when not matched then
insert (DATE_ID) values(to_char(temp_Date.Date_Name,'YYYYMMDD'));