假设有一个表
tableAcol1  col2
----- -----
a      1
a      2
b      3
b      4
c      5
c      6为了保证col1里每次只出现两个相同的值
当一次性写多个insert语句时insert into tableA values('d',7);
insert into tableA values('d',8);
insert into tableA values('a',9);
commit;直到commit才让trigger进行检测,如果没有exception,就正常插入,否则插入的数据无效,不对数据度进行任何的插入或是更新。这个trigger要怎么写啊

解决方案 »

  1.   

    ? trigger可以实现,不过跟commit没有关系无非就是insert的时候计数,如果数量已经超过就报错,完全可以实现楼主的效果
      

  2.   

    强烈建议你先找一本 trigger的书看看先!
      

  3.   

    楼主的想法真的是很TMD牛逼。
      

  4.   

    直到commit才调用触发器似乎是不可能的,因此实现功能与是否commit无关。
    不过可以采用其他的方法,如创建一个临时表,把需要插入的数据插到临时表里去,然后判断插入的数据是否合法。如果合法则插入到正式的tablea里去,否则不插入。示范代码如下,视你具体情况修改(temp_tablea为事务处理期间的临时表):
    create or replace procedure testtemporary as
      repeattwice exception;
      maxrepeatTimes number;
    begin
      insert into temp_tablea values('d','7');
      insert into temp_tablea values('d','8');
      insert into temp_tablea values('a','9');
      select max(count(col1)) into maxrepeatTimes from
      (select * from  temp_tablea
       union all
       select * from tablea) group by col1;
      if(maxrepeatTimes>2) then
         raise repeattwice;
      end if;
      insert/*+Append */ into tablea  select * from temp_tablea;
      commit;
      exception
        when others then rollback;
    end;