oracle 中的触发器,确保 table ExpIt 中的 ExpDate 值小于或等于 ExpReport 中的 ERSubDate
trigger in oracle that ensures that the value of ExpDate from the table ExpIt is less than or equal to the ERSubDate from the ExpReport
我正在尝试在 oracle 中创建一个触发器,以确保来自 table ExpIt
的 ExpDate
的值小于或等于来自 ERSubDate
的值INSERT
和 UPDATE
上的 ExpReport
语句更改了 ExpIt
table.
中的 ExpDate
在命令提示符运行时,出现以下
warning: Trigger created with compilation errors.
这是我到目前为止尝试过的方法,我哪里出错了?
提前致谢。
CREATE OR REPLACE TRIGGER Expense_Date
BEFORE INSERT OR UPDATE OF ExpDate
ON ExpIt
FOR EACH ROW
DECLARE
anExpDate ExpIts.ExpDate%TYPE;
anERSubDate ExpReport.ERSubDate%TYPE;
DateError EXCEPTION;
ExMessage VARCHAR(200);
BEGIN
SELECT ExpDate, ERSubDate
INTO anExpDate, anERSubDate
FROM ExpIt, ExpReport
WHERE ExpIt.ExpDate = :NEW.ExpDate;
IF anExpDate <= anERSubDate THEN
RAISE DateError;
END IF;
EXCEPTION
WHEN DateError THEN
ExMessage := ExMessage || 'Expense Date is Incorrect as it is after the Expense Report Submition date' ||
to_date(anExpDate);
raise_application_error(-20001, ExMessage);
END;
/
在你走得太远之前 - 请注意,你通常无法从触发器本身访问你正在触发的 table。
在你的例子中,你的触发器在 EXPIT 上,你想查询 EXPIT。那不行。
这是一个简单的例子:
SQL> create table t (x int );
Table created.
SQL> insert into t values (1);
1 row created.
SQL> commit;
Commit complete.
SQL>
SQL> create or replace
2 trigger TRG
3 before insert on T
4 for each row
5 declare
6 blah int;
7 begin
8 select count(*) into blah from t;
9 end;
10 /
Trigger created.
SQL>
SQL> insert into t values (2);
1 row created.
它看起来很好,但实际上,有很多情况它不起作用
SQL> insert into t
2 select rownum from dual
3 connect by level <= 5;
insert into t
*
ERROR at line 1:
ORA-04091: table MCDONAC.T is mutating, trigger/function may not see it
ORA-06512: at "MCDONAC.TRG", line 4
ORA-04088: error during execution of trigger 'MCDONAC.TRG'
这是一个大话题,有关此问题及其解决方法的更多详细信息,请参见此处
https://asktom.oracle.com/pls/apex/asktom.search?file=MutatingTable.html#presentation-downloads-reg
我正在尝试在 oracle 中创建一个触发器,以确保来自 table ExpIt
的 ExpDate
的值小于或等于来自 ERSubDate
的值INSERT
和 UPDATE
上的 ExpReport
语句更改了 ExpIt
table.
ExpDate
在命令提示符运行时,出现以下
warning: Trigger created with compilation errors.
这是我到目前为止尝试过的方法,我哪里出错了?
提前致谢。
CREATE OR REPLACE TRIGGER Expense_Date
BEFORE INSERT OR UPDATE OF ExpDate
ON ExpIt
FOR EACH ROW
DECLARE
anExpDate ExpIts.ExpDate%TYPE;
anERSubDate ExpReport.ERSubDate%TYPE;
DateError EXCEPTION;
ExMessage VARCHAR(200);
BEGIN
SELECT ExpDate, ERSubDate
INTO anExpDate, anERSubDate
FROM ExpIt, ExpReport
WHERE ExpIt.ExpDate = :NEW.ExpDate;
IF anExpDate <= anERSubDate THEN
RAISE DateError;
END IF;
EXCEPTION
WHEN DateError THEN
ExMessage := ExMessage || 'Expense Date is Incorrect as it is after the Expense Report Submition date' ||
to_date(anExpDate);
raise_application_error(-20001, ExMessage);
END;
/
在你走得太远之前 - 请注意,你通常无法从触发器本身访问你正在触发的 table。
在你的例子中,你的触发器在 EXPIT 上,你想查询 EXPIT。那不行。
这是一个简单的例子:
SQL> create table t (x int );
Table created.
SQL> insert into t values (1);
1 row created.
SQL> commit;
Commit complete.
SQL>
SQL> create or replace
2 trigger TRG
3 before insert on T
4 for each row
5 declare
6 blah int;
7 begin
8 select count(*) into blah from t;
9 end;
10 /
Trigger created.
SQL>
SQL> insert into t values (2);
1 row created.
它看起来很好,但实际上,有很多情况它不起作用
SQL> insert into t
2 select rownum from dual
3 connect by level <= 5;
insert into t
*
ERROR at line 1:
ORA-04091: table MCDONAC.T is mutating, trigger/function may not see it
ORA-06512: at "MCDONAC.TRG", line 4
ORA-04088: error during execution of trigger 'MCDONAC.TRG'
这是一个大话题,有关此问题及其解决方法的更多详细信息,请参见此处
https://asktom.oracle.com/pls/apex/asktom.search?file=MutatingTable.html#presentation-downloads-reg