ORACLE SQL:合并语句中的多个匹配条件

ORACLE SQL:合并语句中的多个匹配条件,sql,oracle,merge,Sql,Oracle,Merge,我想在pl/sql中使用merge语句,并且必须在特定条件下更新目标表。我知道我们可以在sql server中使用带and运算符的多个匹配条件,但我对Oracle不太确定。我正在oracle中寻找这种功能 create table table1 (id number, text varchar2(20)); create table table2 (id number, text varchar2(20), text2 varchar2(20)); insert into table1 val

我想在pl/sql中使用merge语句,并且必须在特定条件下更新目标表。我知道我们可以在sql server中使用带and运算符的多个匹配条件,但我对Oracle不太确定。我正在oracle中寻找这种功能

create table table1 (id number, text varchar2(20));
create table table2 (id number, text varchar2(20), text2 varchar2(20));

insert into table1 values (1,'');
insert into table1 values (2,'');
insert into table1 values (3,'');
insert into table2 values (1,'a','b');
insert into table2 values (2,'a','b');
insert into table2 values (3,'a','b');
insert into table2 values (4,'a','b');
我知道你想获得类似下面代码的注释行

merge into table1 t1
using table2 t2
on (t1.id = t2.id)
when matched then update set
t1.text = t2.text where t1.id = 1
--t1.text = t2.text2 where t1.id = 2
when not matched then insert values (t2.id, t2.text);
我会这样做:

merge into table1 t1
using (select id, case id when 1 then text when 2 then text2 else null end as tt, text from table2) t2
on (t1.id = t2.id)
when matched then update set
t1.text = t2.tt where t2.tt is not null
--t1.text = t2.text2 where t1.id = 2
when not matched then insert values (t2.id, t2.text);

select * from table1;

在oracle中匹配时,您只能拥有一个
,但是您可以在
上的
部分中定义多个逻辑条件,并使用
运算符连接您也可以在更新部分中使用where子句来指定条件是的,我可以在更新部分中使用where子句,但不管更新部分中的where子句如何,我都可以在merge语句中具有多个where matched条件在这里,我可以更新特定条件下的某些行,并使用另一个条件更新某些行,并在不匹配时插入。。。