2013-03-27 48 views
0

我有一个Oracle 10g触发器,每行之后都有一个INSERT触发事件,并且我想在触发器中插入当前行的某些逻辑。我想要插入用户表标志列值,然后在触发器中,只有当标志为空时,才使用if-then-else来执行某些操作。有关如何获取插入的行的标志列值的任何想法?我的目标是在flag列值为null时,不要执行下面触发器中的任何逻辑。在Oracle触发器中获取当前行数据

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 
+1

这个逻辑应该是一个外键的工作,而不是一个触发器。 – 2013-03-27 23:28:06

回答

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 
2

包围整个区块与if :new.flag is not null then ... end if;