此触发器(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