【问题标题】:Get Current Row Data in Oracle Trigger在 Oracle 触发器中获取当前行数据
【发布时间】:2013-03-27 20:39:15
【问题描述】:

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

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

【问题讨论】:

  • 这个逻辑应该是外键的工作,而不是触发器。

标签: oracle plsql


【解决方案1】:
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;包围整个街区

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 2017-07-20
      • 2021-09-14
      • 2022-01-27
      相关资源
      最近更新 更多