我的 oracle 合并不起作用

发布于 2024-12-13 21:10:12 字数 2914 浏览 3 评论 0原文

我正在跑步 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲸落 2024-12-20 21:10:12

您确定要合并吗?听起来您确实想要

INSERT INTO m2( k, v, knt )
  SELECT k, v, count(*)
    FROM m1
   GROUP BY k, v

MERGE 是基于集合的操作。 M2 中的数据在执行查询时进行评估,因此您的 USING 子句不会看到作为 MERGE 一部分插入的行。由于 USING 子句返回 0 行,因此 M1 中的所有数据都将插入到 M2 中。 WHEN MATCHED 子句永远不会被触发。

Are you sure you want a MERGE? It sounds like you really want

INSERT INTO m2( k, v, knt )
  SELECT k, v, count(*)
    FROM m1
   GROUP BY k, v

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 the MERGE. Since the USING clause returns 0 rows, all the data from M1 is going to be inserted into M2. The WHEN MATCHED clause is never going to be triggered.

揽月 2024-12-20 21:10:12

叹。看起来我必须保持我插入的记录“不同”。

所以这有效。 .... 有点儿。

 merge into m1 d
 using (select distinct k,v,knt from m2) s
 on (d.k = s.k and d.v = s.v)
 when matched then
 update set d.knt = d.knt+1
 when not matched then
 insert(d.k,d.v,d.knt)
 values(s.k,s.v,s.knt)

Sigh. It looks like I have to keep the records I am inserting 'distinct'.

So this works. .... kind of.

 merge into m1 d
 using (select distinct k,v,knt from m2) s
 on (d.k = s.k and d.v = s.v)
 when matched then
 update set d.knt = d.knt+1
 when not matched then
 insert(d.k,d.v,d.knt)
 values(s.k,s.v,s.knt)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文