Oracle - 如果不存在如何插入?

Oracle - how to insert if not exists?

示例 dB:https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=49af209811bce88aa67b42387f1bb5f6

我想添加插入这一行

1002 9 1 未知

因为线存在

1002 5 1 吉姆

我在想类似的事情 select 来自 STATS_CLIENT_TEST 的代码客户端,其中 CODEAXESTAT=5 并插入 codeclient, 9,1,UNKNOWN.

但不确定该怎么做?和简单的查询或 PL/SQL?

获得它的最佳方式是什么?

谢谢

这是一个选项:使用 MINUS 集合运算符,找到缺失的 codeclient 值,然后插入适当的行。

之前:

SQL> select * From stats_client_Test order by codeaxestat, codeclient;

CODECLIENT           CODEAXESTAT CODEELEMENTSTAT VALEURAXESTATISTIQUECLIENT
-------------------- ----------- --------------- ----------------------------------------
1000                           5               1 JOHN
1001                           5               1 ALICE
1002                           5               1 JIM
1003                           5               1 BOB
1000                           9               1 MAN
1001                           9               1 WOMAN
1002                           9               1 unknown
1003                           9               1 MAN

8 rows selected.

查询:

SQL> insert into stats_client_test
  2    (codeclient, codeaxestat, codeelementstat, VALEURAXESTATISTIQUECLIENT)
  3  select x.codeclient, 9, 1, 'unknown'
  4  from (select codeclient from stats_client_Test
  5        where codeaxestat = 5
  6        minus
  7        select codeclient from stats_client_Test
  8        where codeaxestat = 9
  9       ) x;

0 rows created.

之后:

SQL> select * From stats_client_Test order by codeaxestat, codeclient;

CODECLIENT           CODEAXESTAT CODEELEMENTSTAT VALEURAXESTATISTIQUECLIENT
-------------------- ----------- --------------- ----------------------------------------
1000                           5               1 JOHN
1001                           5               1 ALICE
1002                           5               1 JIM
1003                           5               1 BOB
1000                           9               1 MAN
1001                           9               1 WOMAN
1002                           9               1 unknown     --> here it is
1003                           9               1 MAN

8 rows selected.

SQL>

INSERT .. SELECT 语句与 PARTITIONed 外连接一起使用:

INSERT INTO stats_client_test (
  codeclient, codeaxestat, codeelementstat, valeuraxestatistiqueclient
)
  SELECT cc.codeclient,
         s.codeaxestat,
         s.codeelementstat,
         'UNKNOWN'
  FROM   (SELECT DISTINCT codeclient FROM stats_client_test) cc
         LEFT OUTER JOIN stats_client_test s
         PARTITION BY (s.codeaxestat, s.codeelementstat)
         ON (s.codeclient = cc.codeclient)
  WHERE  s.rowid IS NULL;

MERGE 语句:

MERGE INTO stats_client_test dst
USING (
  SELECT cc.codeclient,
         s.codeaxestat,
         s.codeelementstat,
         s.ROWID AS rid
  FROM   (SELECT DISTINCT codeclient FROM stats_client_test) cc
         LEFT OUTER JOIN stats_client_test s
         PARTITION BY (s.codeaxestat, s.codeelementstat)
         ON (s.codeclient = cc.codeclient)
) src
ON (dst.ROWID = src.rid)
WHEN NOT MATCHED THEN
  INSERT (codeclient, codeaxestat, codeelementstat, valeuraxestatistiqueclient)
  VALUES (src.codeclient, src.codeaxestat, src.codeelementstat, 'UNKNOWN');

db<>fiddle here