我已經寫了一個觸發器,它不允許超過兩個'完整'排名的教授作爲教師的一部分(例如,如果添加新的(第三)完整教授或觸發器,觸發器應該觸發已更新爲全職副教授之一)觸發器不允許任何插入
它確實編譯,但不讓我知道添加任何數據到我的表中。它只需要使用一次。我爲此使用語句級觸發器嗎? 此外,目前它不允許我更新或插入Professor Rank,無論是Full還是Associate。我將如何解決這個問題?
我也被告知,之前/之後,我的比較邏輯是錯誤的。請幫忙!
這裏是觸發:
CREATE OR REPLACE TRIGGER TRG_PROF_RANK
after insert or update of F_RANK
on FACULTY
DECLARE
FULL_RANK_COUNT integer;
MAX_FULL_RANK_COUNT number :=2;
begin
select count(*) into FULL_RANK_COUNT
from FACULTY
where F_RANK ='Full';
if FULL_RANK_COUNT < MAX_FULL_RANK_COUNT
then
return;
else
if (FULL_RANK_COUNT >= MAX_FULL_RANK_COUNT) then
RAISE_APPLICATION_ERROR (-20000, 'Can only have 2 professors with ranking "Full".');
end if;
end if;
end;
/
我用下面的語句測試:
insert into FACULTY values(6, 'John', 'Bonny', 'M', 13, '4079347153', 'Associate', 80000, 2, 6034, Null);
但它不允許我插入任何記錄到表中。這是我得到的錯誤:
Error starting at line : 240 in command -
insert into FACULTY values(6, 'John', 'Bonny', 'M', 13, '4079347153', 'Associate', 80000, 2, 6034, Null)
Error report -
SQL Error: ORA-20000: Can only have 2 professors with ranking "Full".
ORA-06512: at "IT337104.TRG_PROF_RANK", line 16
ORA-04088: error during execution of trigger 'IT337104.TRG_PROF_RANK'
20000. 00000 - "%s"
*Cause: The stored procedure 'raise_application_error'
was called which causes this error to be generated.
*Action: Correct the problem as described in the error message or contact
the application administrator or DBA for more information.
請幫忙,我只是爲了確保我可以插入數據。
謝謝
你有'> ='我認爲你需要'>'。另外,你可能已經有兩位或更多的全職教授,即使插入的教授沒有完整排列,情況也會發生。您可能需要「引用新的as」子句(或Oracle SQL中的等價物)並檢查新插入的記錄是否是「完整」教授。從Full到Associate的變化應該總是OK(根據這個觸發器 - 另一個觸發器可能會堅持始終有至少一個完整的教授,儘管這可能過於熱心)。 –