【问题标题】:PL/SQL Oracle: access new values in a before insert triggerPL/SQL Oracle:在插入前触发器中访问新值
【发布时间】:2012-11-14 10:46:53
【问题描述】:

面临以下错误:

[Error] PLS-00049 (12: 11): PLS-00049: bad bind variable 'NEW.OPTION_D'

我正在尝试执行以下操作:

create or replace trigger check_option_for_stage
before insert on lot_option
for each row
when (new.stage_id > 0 and new.option_id > 0)
declare 
not_existing_option exception;
num_count number;
begin
    select count(*) into num_count 
    from option_cost os
    where :new.option_id = os.option_id and :new.stage_id = os.stage_id;
    if num_count = 1 then
        DBMS_OUTPUT.PUT_LINE('The option can be applied to the lot at the current stage');
    ELSE
        raise not_existing_option;    
    end if;
exception
    when not_existing_option then
        DBMS_OUTPUT.PUT_LINE('The option is not available on this stage, therefore rejected');
    when others then
        DBMS_OUTPUT.PUT_LINE('Oops!, something went wrong, it needs your attention!');
end;
/

我为什么要面对这个?为什么它是一个糟糕的绑定变量?我知道我应该可以通过输入 :new.whateverthecolumnname 来访问新值

我正在使用 Oracle 11g。

我正在玩的桌子的定义

SQL> desc option_cost
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 COST                                      NOT NULL NUMBER
 OPTION_ID                                 NOT NULL NUMBER(38)
 STAGE_ID                                  NOT NULL NUMBER(38)

SQL> desc lot_option;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 LOT_ID                                    NOT NULL NUMBER(38)
 OPTION_ID                                 NOT NULL NUMBER(38)
 COST                                      NOT NULL NUMBER
 DATE_CREATED                                       DATE
 STAGE_ID                                  NOT NULL NUMBER(38)

【问题讨论】:

    标签: plsql triggers oracle11g


    【解决方案1】:

    列是option_id(末尾有id)还是只有option_d(没有i)? option_id 似乎更有意义。假设option_id 是正确的,您的SELECT 语句中有错字,您在id 中缺少i。你想要类似的东西

    select count(*) 
      into count 
      from option_cost oc
     where :new.option_id = oc.option_id 
       and :new.stage_id = oc.stage_id;
    

    当然,由于count是保留字,所以声明一个名为count的局部变量并不是一个好主意。将变量命名为l_count 或使用其他命名约定来标识局部变量并避免使用保留字会更有意义。

    【讨论】:

    • +1 :好地方,看起来也有 2 个较早的对新值的引用。
    • 将 count 变量更改为不同的值,但仍然出现相同的错误。你能帮我进一步吗?
    • @Bob - 因此,您更正了 option_id 中缺少的 i 的错字,并且您更改了局部变量的名称,但您仍然在同一行上遇到相同的错误option_d 参考拼写错误?这似乎不太可能,因为触发器不再引用 option_d
    • 第 4 行的变量引用前面也缺少冒号。 "new" 和 ":new" 是两个不同的东西。前者被解释为一个未定义的变量,后者是你试图引用的绑定变量。
    猜你喜欢
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2013-07-01
    相关资源
    最近更新 更多