我的合并在Oracle 11G中不正确

时间:2014-09-26 19:22:20

标签: sql oracle insert oracle11g merge

有人可以向我解释我在这个陈述中做错了什么。它非常基本......

merge into week1wrs 
using 
(select wr_id,fname,lname from wrname_id) on 
(week1wrs.fname=wrname_id.fname and week1wrs.lname=wrname_id.LNAME)
when matched then update set week1wrs.WR_ID=wrname_id.wr_id
when not matched update set week1wrs.Name_id='';

* 我正在使用oracle 11g

2 个答案:

答案 0 :(得分:2)

使用表别名作为Target(T)和Source(S)

merge into week1wrs T
using 
(select wr_id,fname,lname from wrname_id) S on 
(S.fname=T.fname and S.lname=T.LNAME)
when matched then update set T.WR_ID=S.wr_id
when not matched --need to be insert here.

答案 1 :(得分:1)

when not matched update set week1wrs.Name_id=''

如果不匹配,您只能插入,则无法更新。

合并语法:

 MERGE INTO table
 USING table | subquery
 ON condition
 WHEN MATCHED THEN UPDATE SET col = expression | DEFAULT
 where_clause
 DELETE where_clause
 WHEN NOT MATCHED THEN INSERT (col, col2)
 VALUES (expr1, expr2 | DEFAULT)
 where_clause
 WHERE condition;