我试图创建一个触发器,从表2中读取并更新表1中的列。我尝试了这个方法,但是出现了一个异常:ORA-04091:table table1正在发生突变。
CREATE OR REPLACE TRIGGER "TRG1"
AFTER INSERT OR UPDATE ON table1
FOR EACH ROW
BEGIN
UPDATE table1 SET name =(SELECT name FROM table2
WHERE table1.id = table2.id);
END;
使用BEFORE UPDATE
触发器(因为您无法在插入或更新后修改值)并使用:NEW
记录(而不是尝试更新表并进入触发器的无限循环):
CREATE OR REPLACE TRIGGER TRG1
BEFORE INSERT OR UPDATE ON table1
FOR EACH ROW
BEGIN
SELECT name
INTO :NEW.name
FROM table2
WHERE :NEW.id = id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
:NEW.name := NULL;
END;
/
小提琴
您只能在before触发器中更新名称
CREATE OR REPLACE TRIGGER "TRG1"
BEFORE INSERT OR UPDATE ON table1
FOR EACH ROW
DECLARE new_name varchar2(100);
BEGIN
SELECT name INTO new_name FROM table2
WHERE :new.id = table2.id;
:new.name :=new_name;
END;
/
MTO做小提琴真是太好了