2 过程 PLSQL 中的合并语句

2 Merge Statements inside a Procedure PLSQL

我正在尝试在过程中使用 2 个合并语句。 一个应该删除不在另一个 table.

中的行

另一个应该插入不在table中的那些。

在这种情况下,我收到了缺少关键字的错误。

create or replace noneditionable procedure merge_test
as
BEGIN

Execute immediate 'alter table
   table person
DISABLE constraint
FK_FGH_ID ';


Merge into (select distinct * from 
person_test) t1
Using (select * from person) p1
On (p1.person_id = t1.person_id)
When not matched then
DELETE
FROM
    person
WHERE
    Person_id = p1.person_id;
COMMIT;

Merge into person p
USING (select distinct * 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);
COMMIT;

Execute immediate 'alter table
   person
ENABLE constraint
   FK_FGH_ID ';


End merge_test;

第一个合并中的 'WHEN NOT MATCHED' 处出现关键字丢失错误。 另一个问题是是否有可能在一个过程中有 2 个合并语句。

是的,在一个过程中有两个 MERGE 语句绝对是可能的,但为什么要在这种情况下使用它们?只需使用一个简单的 DELETE 然后 INSERT 类似于下面的语句。

CREATE OR REPLACE NONEDITIONABLE PROCEDURE merge_test
AS
BEGIN
    EXECUTE IMMEDIATE 'alter table person DISABLE constraint FK_FGH_ID';

    DELETE FROM person
          WHERE person_id NOT IN (SELECT person_id FROM person_test);

    INSERT INTO person (person_id,
                        vorname,
                        nachname,
                        mobil,
                        telefon,
                        fax,
                        e_mail,
                        fgh_id)
        SELECT person_id,
               vorname,
               nachname,
               mobil,
               telefon,
               fax,
               e_mail,
               fgh_id
          FROM person_test
         WHERE person_id NOT IN (SELECT person_id FROM person);

    EXECUTE IMMEDIATE 'alter table person ENABLE constraint FK_FGH_ID';
END merge_test;

在存储过程中使用多个 MERGE 语句没有问题。

您仍然可以通过转换相关部分来使用第一个MERGE语句

WHEN NOT MATCHED THEN
DELETE FROM person
WHERE Person_id = p1.person_id

至:

 WHEN MATCHED THEN UPDATE SET t1.person_id = p1.person_id 
                   DELETE WHERE person_id = p1.person_id

但是 DELETE 声明不能在 WHEN NOT MATCHED THEN

之后发生

顺便说一句,在一次交易中两个人拥有多个 COMMIT 并不是一个好习惯,而只保留最后一个。

像这样:

create or replace noneditionable procedure merge_test
IS

BEGIN

    Execute immediate 'alter table person DISABLE constraint FK_FGH_ID ';

    Merge into person_test t1
    Using (select * from person) p1
    On (t1.person_id = p1.person_id)
    when matched then 
         update set t1.Person_id = t1.Person_id
             where t1.Person_id = p1.person_id
         delete 
             where t1.Person_id = p1.person_id;

    COMMIT;
    
    Merge into person p
    USING (select * 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);
    
    COMMIT;
    
    Execute immediate 'alter table person ENABLE constraint FK_FGH_ID ';

End merge_test;
/

请注意,您的第一个 Execute immediate 语句有误。应该是:

Execute immediate 'alter table person DISABLE constraint FK_FGH_ID ';