此觸發器(Oracle 12c)旨在停止插入和更新表(MainTable aka Room)中的行,其中列(價格)大於一個變量。變量的值取決於另一列(類型)。有三種類型(S,D,K),'類型'的最大允許價格分別爲100,150和200。該觸發器通過引用具有兩列和三行的域表(DomainTable又名RoomType),如下[roomTypeCode(S,D,K),maxPrice(100,150,200)]並確保:如何使用Oracle觸發器(和域表)代替檢查約束來強制執行列範圍
.. .IF new MainTable.type ='S',THEN new MainTable.price < DomainTable.maxPrice(S);
... IF new MainTable.type ='D',THEN new MainTable.price < DomainTable.maxPrice(D);
... IF new MainTable.type ='K',THEN new MainTable.price < DomainTable.maxPrice(K);
這是我的嘗試不起作用。
CREATE TRIGGER Room_Type_Price_Range
BEFORE INSERT OR UPDATE ON room
REFERENCING NEW AS newRec
FOR EACH ROW
DECLARE
SELECT maxPrice INTO singleRmMax FROM RoomType WHERE RoomTypeCode = 'S';
SELECT maxPrice INTO doubleRmMax FROM RoomType WHERE RoomTypeCode = 'D';
SELECT maxPrice INTO kingRmMax FROM RoomType WHERE RoomTypeCode = 'K';
BEGIN
IF ( (:newRec.type = 'S' AND :newRec.price > singleRmMax)
OR (:newRec.type = 'D' AND :newRec.price > doubleRmMax)
OR (:newRec.type = 'K' AND :newRec.price > kingRmMax)
)
RAISE_APPLICATION_ERROR(-20001, 'Price constraint violated.
\nCannot Insert/Update in this table.');
END;
我的錯誤信息:
04098. 00000 - "trigger '%s.%s' is invalid and failed re-validation"
*Cause: A trigger was attempted to be retrieved for execution and was
found to be invalid. This also means that compilation/authorization
failed for the trigger.
*Action: Options are to resolve the compilation/authorization errors,
disable the trigger, or drop the trigger.
感謝您的幫助!
你在'DECLARE'部分有'SELECT's。 – 2015-02-24 02:14:04
在使用觸發器之前,編譯它並檢查它是否有效。 – Alfabravo 2015-02-24 07:05:24
編譯後,使用'show errors'命令(或SQL Developer編譯器日誌)查看遇到的實際問題;或者查詢你的對象名稱的'user_errors'視圖。 – 2015-02-24 09:11:41