在Oracle Trigger中获取当前行数据

时间:2013-03-27 20:39:16

标签: oracle plsql

我有一个Oracle 10g触发器,在每一行之后有一个INSERT触发事件,我想在触发器的基础上将一些逻辑基于当前插入的行。我想要插入Users表标志列值,然后在触发器中,使用if-then-else仅在flag为null时执行某些操作。有关如何获取正在插入的行的标志列值的任何想法?我的目标是当标志列值为空时不执行下面触发器中的任何逻辑。

Table: USERS

Columns:
id (PK generated from a DB sequence)
.... (more columns)
flag VARCHAR2(1 BYTE) and is nullable

触发:

//goal is to not do any of the logic in the trigger below when flag is null


CREATE OR REPLACE TRIGGER "DBUSER"."TI_USERS" 
  AFTER INSERT
  on Users

  for each row

declare numrows INTEGER;
begin
    select count(*) into numrows
      from Customer
      where
        /* %JoinFKPK(:%New,Customer," = "," and") */
        :new.Customer_Key = Customer.Customer_Key;
    if (
      /* %NotnullFK(:%New," is not null and") */

      numrows = 0
    )
    then
      raise_application_error(
        -20002,
        'Cannot INSERT Users because Customer does not exist.'
      );
    end if;



end;
ALTER TRIGGER "SIMPLEX"."TI_USERS" ENABLE

2 个答案:

答案 0 :(得分:2)

CREATE OR REPLACE TRIGGER "DBUSER"."TI_USERS" 
  AFTER INSERT
  on Users

  for each row

declare numrows INTEGER;
begin

IF ( :new.flag IS NOT NULL ) Then

    select count(*) into numrows
      from Customer
      where
        /* %JoinFKPK(:%New,Customer," = "," and") */
        :new.Customer_Key = Customer.Customer_Key;
    if (
      /* %NotnullFK(:%New," is not null and") */

      numrows = 0
    )
    then
      raise_application_error(
        -20002,
        'Cannot INSERT Users because Customer does not exist.'
      );
    end if;

end if;

end;
ALTER TRIGGER "SIMPLEX"."TI_USERS" ENABLE

答案 1 :(得分:2)

使用if :new.flag is not null then ... end if;

围绕整个块