trigger
在论坛里面搜索“触发器”,
试着写写看,不难

解决方案 »

  1.   

    我现在正用trigger,可是不管用啊。下面是我的代码,望各位执教:
    create or replace trigger LimitSalForInsert
      before insert on emp  
      for each row
      
    declare
      dp varchar2(5);--emp.deptno%type;
      sal  number(7,2);--emp.sal%type;begin
         --select sal into oldsal from emp t where t.rowid = :new.rowid;
         select deptno,sum(sal) into dp,sal from emp 
         where deptno= :new.deptno
         group by deptno;
                  if sal>20000 then
                     --:new.sal := oldsal;
                     delete from emp t where t.empno = :new.empno;
                     dbms_output.put_line('Error:the salary of '||dp||' is over 20000 yuan!');
                  end if;
    end LimitSalForInsert;我本想在这个trigger里面判断新插入的行所属的deptno工资总和是否已经超过了20000,如果超过了,我就把新插入的行删掉。可是,我试了,该插入的还是插入进去了。我插入的数据的sal=20001,deptno=20(表中有属于这个deptno的数据).请各位评判指点。另外,对于update操作我还不知道怎么控制。
      

  2.   

    //如果改为after insert on emp,则trigger在触发的时候会出错,错 
              误号为ORA-04091:表SCOTT.EMP发生了变化,触发器/函数不能读。 
             ORA-06512:在“SCOTT.LIMITSALFORINSERT“,line 12 
             ORA-04088:触发器“SCOTT.LIMITSALFORINSERT“,执行过程中出错。 
      

  3.   

    create or replace trigger LimitSalForInsert
      before insert on emp  
      for each row
    declare
      sal  number(7,2);--emp.sal%type;
    begin
         select nvl(sum(sal),0) into sal from emp 
         where deptno= :new.deptno;
         if (sal+:new.sal)>20000 then
           raise_application_error(-20010,'Error:the salary of '||:new.deptno||' is over 20000 yuan!');
         end if;
    end LimitSalForInsert;
    /
      

  4.   

    TO: bobfang(匆匆过客) 
    你的代码同样是在select nvl(sum(sal),0) into sal from emp where eptno= :new.deptno;
    的时候报错,错误与我上面列出的一样。
      

  5.   

    不会啊。
    SQL>  create table emp(deptno varchar2(3), sal number(7,2));表已创建。SQL> create or replace trigger LimitSalForInsert
      2    before insert on emp  
      3    for each row
      4  declare
      5    sal  number(7,2);--emp.sal%type;
      6  begin
      7       select nvl(sum(sal),0) into sal from emp 
      8       where deptno= :new.deptno;
      9       if (sal+:new.sal)>20000 then
     10         raise_application_error(-20010,'Error:the salary of '||:new.deptno||' is over 20000 yuan!');
     11       end if;
     12  end LimitSalForInsert;
     13  /触发器已创建SQL> insert into emp values('1',10000);已创建 1 行。SQL> insert into emp values('2',20001);
    insert into emp values('2',20001)
                *
    ERROR 位于第 1 行:
    ORA-20010: Error:the salary of 2 is over 20000 yuan!
    ORA-06512: 在"OPER_112.LIMITSALFORINSERT", line 7
    ORA-04088: 触发器 'OPER_112.LIMITSALFORINSERT' 执行过程中出错
    SQL> insert into emp values('1',10000);已创建 1 行。SQL> insert into emp values('1',1.2);
    insert into emp values('1',1.2)
                *
    ERROR 位于第 1 行:
    ORA-20010: Error:the salary of 1 is over 20000 yuan!
    ORA-06512: 在"OPER_112.LIMITSALFORINSERT", line 7
    ORA-04088: 触发器 'OPER_112.LIMITSALFORINSERT' 执行过程中出错
    SQL> select * from emp;DEP        SAL
    --- ----------
    1        10000
    1        10000
      

  6.   

    好了,这个问题我已经搞定了,包括insert和update操作都能控制了。非常感谢各位,特别是bobfang给我的启发。现在开始散分。