【发布时间】:2015-04-25 13:09:18
【问题描述】:
此触发器 (Oracle 12c) 旨在停止在列(价格)大于变量的表(MainTable aka Room)中插入和更新行。变量的值取决于另一列(类型)。共有三种“类型”(S、D、K),“类型”允许的最高价格分别为 100、150 和 200。触发器通过引用具有两列和三行的域表(DomainTable aka RoomType)来工作,如下所示 [roomTypeCode(S, D, K), maxPrice(100, 150, 200)] 并确保:
...IF new MainTable.type = 'S', THEN new MainTable.price
...IF new MainTable.type = 'D', THEN new MainTable.price
...IF new MainTable.type = 'K', THEN new MainTable.price
这是我失败的尝试。
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部分中有SELECTs。 -
在使用触发器之前,编译它并检查它是否有效。
-
编译后使用
show errors命令(或SQL Developer编译日志)查看实际遇到的问题;或在user_errors视图中查询您的对象名称。
标签: sql oracle plsql triggers oracle-sqldeveloper