Oracle 行触发器到复合触发器

Oracle row trigger to compound trigger

我在从我的行触发器创建复合触发器时遇到问题,或者如果有人有比我想听到的复合触发器更好的解决方案。想法是,当有人更新 'KONTAKT' table 中的列 'kupac_pib' 时触发触发器,这将更改 'KONTAKT' table 中的 'naziv_firme_kupac',但是 naziv_firme_kupac来自 'KUPAC' table.

错误是 ORA-04091: table C##NIKOLA.KONTAKT 正在变异,trigger/function 可能看不到它

CREATE TABLE Kupac (
    kupac_PIB INT PRIMARY KEY,
    naziv_firme_kupac VARCHAR2(255)
);

CREATE TABLE Kontakt (
    id_kontakt INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    mail VARCHAR2(100),  
    telefon VARCHAR2(100),
    kupac_PIB INT, 
    partner_PIB INT, 
    naziv_firme_kupac VARCHAR2(255),
    naziv_poslovni_partner VARCHAR2(255),
    CONSTRAINT kontakt_kupac
        FOREIGN KEY(kupac_PIB)
        REFERENCES Kupac(kupac_PIB),
    CONSTRAINT kontakt_pPartner
        FOREIGN KEY(partner_PIB)
        REFERENCES PPartner(partner_PIB)
);

CREATE OR REPLACE TRIGGER IzmenaNazivaKupcaNaOsnovuPIB_a
AFTER UPDATE OF kupac_PIB ON Kontakt
FOR EACH ROW
DECLARE
noviNazivKupca VARCHAR2(255);
kupac_PIB_tmp INT;
    BEGIN
        noviNazivKupca:= :new.naziv_firme_kupac;
        kupac_PIB_tmp := :new.kupac_PIB;
        UPDATE Kontakt
        SET naziv_firme_kupac = noviNazivKupca
        WHERE kupac_PIB_tmp = (SELECT kupac_PIB FROM Kupac
        WHERE kupac_PIB_tmp = kupac_PIB);
    END;

您需要将触发器更新为 -

CREATE OR REPLACE TRIGGER IzmenaNazivaKupcaNaOsnovuPIB_a
BEFORE UPDATE OF kupac_PIB ON Kontakt
FOR EACH ROW
DECLARE
kupac_PIB_tmp VARCHAR2(255);
BEGIN
        SELECT naziv_firme_kupac
          INTO kupac_PIB_tmp
          FROM Kupac
         WHERE kupac_PIB = :new.kupac_PIB;
         
        :new.naziv_firme_kupac := kupac_PIB_tmp;
END;
/

Demo.