触发条款中的IF语句

时间:2014-12-25 13:00:43

标签: sql oracle triggers

我想在内部触发器中使用if语句,但是如果比较的值将来自另一个select语句。

我做了以下事情:

create or replace 
Trigger MYTRIGGER
After Insert On Table1
Referencing Old As "OLD" New As "NEW" 
For Each Row 
Begin
  Declare Counter Int;
  Select Count(*) From Table2 Where Table2."Email" = :New.U_MAIL  Into Counter;
  IF Counter < 1 THEN
    //INSERT Statement here... 
  END IF;
End;

我的逻辑很简单,如果存在相同的电子邮件用户,插入将无效。

以上代码无效。我们怎么做到这一点?

2 个答案:

答案 0 :(得分:2)

一些语法错误。会更接近这样的事情:

create or replace 
Trigger MYTRIGGER
After Insert On Table1
Referencing Old As "OLD" New As "NEW" 
For Each Row 

DECLARE

  v_count NUMBER;

BEGIN

  SELECT COUNT(*)
    INTO v_count
    FROM Table2
    WHERE Email = :New.U_MAIL
  ;

  IF v_count > 0
  THEN
      RAISE_APPLICATION_ERROR(-20000, 'Not inserted...');
  END IF;

END;

答案 1 :(得分:1)

你的做法是错误的。不应使用触发器进行参照完整性,它不能按要求工作。见例:

Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 
Connected as test@soft12c1

SQL> create table mail_1 (email varchar2(100));

Table created

SQL> create table mail_2 (email varchar2(100));

Table created

SQL> create trigger mail_1_check
  2    before insert on mail_1
  3    for each row
  4  declare
  5    cnt integer;
  6  begin
  7    select count(*) into cnt from mail_2 where email = :new.email;
  8    if cnt > 0 then
  9      raise_application_error(-20100, 'Email already exists');
 10    end if;
 11  end;
 12  /

Trigger created

SQL> insert into mail_2 values ('president@gov.us');

1 row inserted

SQL> insert into mail_1 values ('king@kingdom.en');

1 row inserted

SQL> insert into mail_1 values ('president@gov.us');

ORA-20100: Email already exists
ORA-06512: at "TEST.MAIL_1_CHECK", line 6
ORA-04088: error during execution of trigger 'TEST.MAIL_1_CHECK'

看起来触发器工作正常,但事实并非如此。看看当多个用户同时工作时会发生什么。

-- First user in his session

SQL> insert into mail_2 values ('dictator@country.by');

1 row inserted

-- Second user in his session

SQL> insert into mail_1 values ('dictator@country.by');

1 row inserted

-- First user is his session

SQL> commit;

Commit complete

-- Second user is his session

SQL> commit;

Commit complete

-- Any user in any session 

SQL> select * from mail_1 natural join mail_2;

EMAIL
--------------------------------------------------------------------------------
dictator@country.by

如果对此任务使用触发器,则应序列化任何使用此数据的尝试,例如,执行LOCK TABLE IN EXCLUSIVE MODE,除非提交。一般来说,这是一个糟糕的决定。对于这个具体任务,您可以使用更好的方法:

Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 
Connected as test@soft12c1

SQL> create table mail_1_2nd(email varchar2(100));

Table created

SQL> create table mail_2_2nd(email varchar2(100));

Table created

SQL> create materialized view mail_check
  2    refresh complete on commit
  3  as
  4    select 1/0 data from mail_1_2nd natural join mail_2_2nd;

Materialized view created

行。让我们看看,如果我们尝试使用相同的电子邮件该怎么办:

-- First user in his session

SQL> insert into mail_1_2nd values ('dictator@gov.us');

1 row inserted

-- Second user in his session

SQL> insert into mail_2_2nd values ('dictator@gov.us');

1 row inserted

SQL> commit;

Commit complete

-- First user in his session

SQL> commit;

ORA-12008: error in materialized view refresh path
ORA-01476: divisor is equal to zero

SQL> select * from mail_1_2nd natural join mail_2_2nd;

EMAIL
--------------------------------------------------------------------------------

no rows selected