2015-02-24 23 views
0

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

感谢您的帮助!

+2

你在'DECLARE'部分有'SELECT's。 – 2015-02-24 02:14:04

+1

在使用触发器之前,编译它并检查它是否有效。 – Alfabravo 2015-02-24 07:05:24

+1

编译后,使用'show errors'命令(或SQL Developer编译器日志)查看遇到的实际问题;或者查询你的对象名称的'user_errors'视图。 – 2015-02-24 09:11:41

回答

2

当您创建触发器时,您会看到类似'编译时出现警告'或'错误:检查编译器日志'的消息。此时,您可以执行show errors以查看编译失败的原因,或查看SQL Developer的编译器日志窗口。

当您插入或更新the invalid trigger is automatically recompiled,但由于它仍然无效,您会收到ORA-04098错误。你仍然可以看到什么是错的通过查询user_errors观点:

select line, position, text 
from user_errors 
where type = 'TRIGGER' 
and name = 'ROOM_TYPE_PRICE_RANGE' 
order by sequence; 

与您的代码中给出了三个错误;只显示每个的第一行:

LINE POSITION TEXT 
---- -------- ------------------------------------------------------------------------------------------------- 
    2  5 PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: 
    10  9 PLS-00103: Encountered the symbol "RAISE_APPLICATION_ERROR" when expecting one of the following: 
    11  50 PLS-00103: Encountered the symbol ";" when expecting one of the following: 

正如大卫·费伯在评论中指出,第一个错误是因为你在你的声明部分的select语句;也许在这一点上审查the structure of a subprogram将是有用的。

第二个错误是因为您的IF没有THEN关键字,第三个错误是因为您没有END IF。只是清理你必须正确地申报和填充的变量是什么,你会得到这样的:

DECLARE 
    singleRmMax RoomType.MaxPrice%TYPE; 
    doubleRmMax RoomType.MaxPrice%TYPE; 
    kingRmMax RoomType.MaxPrice%TYPE; 
BEGIN 
    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'; 

    IF ( (:newRec.type = 'S' AND :newRec.price > singleRmMax) 
    OR (:newRec.type = 'D' AND :newRec.price > doubleRmMax) 
    OR (:newRec.type = 'K' AND :newRec.price > kingRmMax) 
    ) THEN 
    RAISE_APPLICATION_ERROR(-20001, 'Price constraint violated. 
     \nCannot Insert/Update in this table.'); 
    END IF; 
END; 

你并不真正需要三个变量,虽然,你可以查询你感兴趣的房型:

DECLARE 
    roomTypeMax RoomType.MaxPrice%TYPE; 
BEGIN 
    SELECT maxPrice INTO roomTypeMax 
    FROM RoomType 
    WHERE RoomTypeCode = :newRec.type; 

    IF :newRec.price > roomTypeMax THEN 
    RAISE_APPLICATION_ERROR(-20001, 
     'Price constraint violated. Cannot Insert/Update in this table.'); 
    END IF; 
END; 

我也采取了\n出来的,不管用什么插件可能把那两个文字字符,而不是换行的错误消息。

您可能还想考虑捕获no_data_found并引发您自己的例外情况,因为这会表明新房型不存在,因此无论如何都无效。

+0

非常感谢Alex! – stacker 2015-02-24 16:53:52

+1

其实,如果你要出门去查询表格,不妨也可以拿它来进行比较。 '从RootType中选择count(*)到roomCount,其中RoomTypeCode =:newRec.Type和MaxPrice> =:newRec.price;'。这样,返回零意味着价格超过了未定义类型代码的最大值*或*。 “这或那是问题”错误信息是好的。 – TommCatt 2015-02-25 17:42:01