使用 SQL 查询从多个表中删除记录

Deleting Records from multiple tables with the SQL Query

我在 Oracle DB 中有一个暂存 table,其中加载了数据,table 名称是 pc_stg_auth。它有4个PK。我必须将这 4 个 PK 与另一个名为 autho_activity_msc 的 table 匹配,如果 PK 匹配,则我必须从名为 pc_stg_auth 的暂存 table 以及来自 autho_activity_msc.

我正在使用以下查询,但它给我一个语法错误:

delete  autho_activity_msc , pc_stg_auth from autho_activity_msc inner join pc_stg_auth 
where autho_activity_msc.reference_number =  pc_stg_auth.reference_number AND
    autho_activity_msc.external_stan =   pc_stg_auth.external_stan   AND
    autho_activity_msc.routing_code =    pc_stg_auth.routing_code   AND
    autho_activity_msc.capture_code =    pc_stg_auth.capture_code;
    commit;

错误如下:

line 1: ORA-00933: SQL command not properly ended

如果有更简单的实现方法,请帮助或建议。

Oracle 不支持该语法。

您需要使用多个 DELETE 语句。您可以从一个 table 中删除匹配的行并将匹配的键收集到集合中,然后遍历集合并删除第二个 table:

中的所有行
DECLARE
  rns SYS.ODCINUMBERLIST;
  ess SYS.ODCINUMBERLIST;
  rcs SYS.ODCINUMBERLIST;
  ccs SYS.ODCINUMBERLIST;
BEGIN
  DELETE FROM pc_stg_auth
  WHERE (reference_number, external_stan, routing_code, capture_code)
        IN (SELECT reference_number, external_stan, routing_code, capture_code
            FROM   autho_activity_msc)
  RETURNING reference_number, external_stan, routing_code, capture_code
  BULK COLLECT INTO rns, ess, rcs, ccs;
  
  FORALL i IN 1 .. rns.COUNT
    DELETE FROM autho_activity_msc
    WHERE reference_number = rns(i)
    AND   external_stan    = ess(i)
    AND   routing_code     = rcs(i)
    AND   capture_code     = ccs(i);
  
  COMMIT;
END;
/

db<>fiddle here