我的 oracle 合并不起作用
我正在跑步 Oracle 数据库 10g 企业版版本 10.1.0.5.0 - 产品。 我想将 m2 中的数据合并到 m1 中。我预计会看到 3 条记录 合并后的 m1 中,一个用于 'c',knt 为 4,一个用于 'a',knt 为 1 一个代表“b”,结点为 1。
但我得到了一切。好像没有检查更新或插入发生。
见下文。
最好的问候,
菲尔
SQL> desc m1;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> desc m2;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> select * from m1;
no rows selected
SQL> select * from m2;
K V KNT
------ ------ ----------
a aaa 0
b bbb 0
c ccc 0
c ccc 0
c ccc 0
a aaa 0
b bbb 0
c ccc 0
c ccc 0
SQL> merge into m1 d
2 using (select k,v,knt from m2) s
SQL> desc m1;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> desc m2;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> select * from m1;
no rows selected
SQL> select * from m2;
K V KNT
------ ------ ----------
a aaa 0
b bbb 0
c ccc 0
c ccc 0
c ccc 0
a aaa 0
b bbb 0
c ccc 0
c ccc 0
SQL> merge into m1 d
2 using (select k,v,knt from m2) s
3 on (d.k = s.k)
4 when matched then
5 update set d.knt = d.knt+1
6 when not matched then
7 insert(d.k,d.v,d.knt)
8 values(s.k,s.v,s.knt)
9 ;
SQL> select * from m1;
K V KNT
------ ------ ----------
b bbb 0
b bbb 0
c ccc 0
c ccc 0
c ccc 0
c ccc 0
c ccc 0
a aaa 0
a aaa 0
I am running
Oracle Database 10g Enterprise Edition Release 10.1.0.5.0 - Prod.
I want to merge data from m2 into m1. I expect to see 3 records
in m1 after the merge, one for 'c' with a knt of 4, one for 'a' with a knt of 1
and one for 'b' with a knt of 1.
But I get everything. As if no checking for update or insert occurred.
See below.
Best regards,
Phil
SQL> desc m1;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> desc m2;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> select * from m1;
no rows selected
SQL> select * from m2;
K V KNT
------ ------ ----------
a aaa 0
b bbb 0
c ccc 0
c ccc 0
c ccc 0
a aaa 0
b bbb 0
c ccc 0
c ccc 0
SQL> merge into m1 d
2 using (select k,v,knt from m2) s
SQL> desc m1;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> desc m2;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> select * from m1;
no rows selected
SQL> select * from m2;
K V KNT
------ ------ ----------
a aaa 0
b bbb 0
c ccc 0
c ccc 0
c ccc 0
a aaa 0
b bbb 0
c ccc 0
c ccc 0
SQL> merge into m1 d
2 using (select k,v,knt from m2) s
3 on (d.k = s.k)
4 when matched then
5 update set d.knt = d.knt+1
6 when not matched then
7 insert(d.k,d.v,d.knt)
8 values(s.k,s.v,s.knt)
9 ;
SQL> select * from m1;
K V KNT
------ ------ ----------
b bbb 0
b bbb 0
c ccc 0
c ccc 0
c ccc 0
c ccc 0
c ccc 0
a aaa 0
a aaa 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定要合并吗?听起来您确实想要
MERGE 是基于集合的操作。 M2 中的数据在执行查询时进行评估,因此您的
USING
子句不会看到作为MERGE
一部分插入的行。由于USING
子句返回 0 行,因此M1
中的所有数据都将插入到M2
中。WHEN MATCHED
子句永远不会被触发。Are you sure you want a MERGE? It sounds like you really want
A MERGE is a set-based operation. The data in M2 is evaluated at the time the query is executed so your
USING
clause is not going to see the rows that are being inserted as part of theMERGE
. Since theUSING
clause returns 0 rows, all the data fromM1
is going to be inserted intoM2
. TheWHEN MATCHED
clause is never going to be triggered.叹。看起来我必须保持我插入的记录“不同”。
所以这有效。 .... 有点儿。
Sigh. It looks like I have to keep the records I am inserting 'distinct'.
So this works. .... kind of.