合并语句 SQL 语法

Merge Statement SQL Syntax

我正在尝试比较来自不同表的 2 个 ID,如果它们不匹配,则应该添加不在源中的行。

但是我收到这个错误:

PLS-00103: Encountered the symbol "Person" when expecting one of the following: : = ( @ % ;

代码:

CREATE OR REPLACE PROCEDURE merge_test AS
BEGIN
  MERGE person AS p
  USING person_test AS t 
     ON t.person_id = p.person_id 
   WHEN NOT MATCHED BY TARGET THEN
   INSERT(person_id,vorname, nachname, mobil, telefon, fax, e_mail, fgh_id)
   VALUES(t.person_id, t.vorname, t.nachname, t.mobil, t.telefon, t.fax, t.e_mail, t.fgh_id);
END;
/

也许我的语法有误,或者我必须添加“立即执行”? 我是 SQL 和 PL/SQL 的新手。

您的 MERGE 声明中存在一些问题:

  • t.person_id = p.person_id放入括号中
  • Oracle 不允许使用 AS 关键字为表设置别名。摆脱 他们
  • 这种用法 (Using person_test as t) 是不可能的,但请使用 子查询(Using (select ... from person_test) t
  • When not matched by target then
  • 中删除子句 by target
  • MERGE 语句后需要跟一个 INTO 子句
  • ON后的匹配条件t.person_id = p.person_id 子句应加括号

所以使用:

MERGE INTO person p
USING (SELECT person_id,
              vorname,
              nachname,
              mobil,
              telefon,
              fax,
              e_mail,
              fgh_id
         from person_test) t
ON (t.person_id = p.person_id)
WHEN NOT MATCHED 
THEN INSERT(person_id, vorname, nachname, mobil, telefon, fax, e_mail, fgh_id) 
     VALUES(t.person_id, t.vorname, t.nachname, t.mobil, t.telefon, t.fax, t.e_mail, t.fgh_id);