关于触发器

来源:互联网 发布:印度经济知乎 编辑:程序博客网 时间:2024/04/29 15:23

 DML触发器

语句触发器:当执行DML语句时被隐含执行的触发器
语法: create (or replace) trigger t_update_A
       {BEFORE| AFTER } EVENT1  [OR EVENT2 OR EVENT3]
       ON TABLE_NAME
       pL/SQL block;

eg: 
        create or replace trigger tr_sec_emp
         before insert or update or delete on emp
        begin
            if  to_char(sysdate,'DY','nls_date_language=AMERICAN')
               IN  ('SAT','SUN') THEN
             raise_application_error(-20001,'不能在休息日改变雇员信息');
            end if;
            end;

行触发器-----当执行DML语句时,每作用一行被触发一次的触发器,当使用DML语句触发器时,
              不能记录列数据的变化,为了审计表数据的变化,就需要使用行触发器

eg:   create or replace trigger tr_emp_sal
       before update of sal on emp
      for each row
       begin
         if  :new.sal<:old.sal then
          raise_application_error(-20010,'工资只涨不降');
        end if;
        end;

限制行触发器:-----在特定条件下执行行触发器,需要使用When子句对触发条件加以限制,
  (下面以审计岗位salesman的雇员工资变化为例)
eg:  create or replace trigger  tr_sal_change
      after update or sal  on emp
      for each row
      when (old.job='salesman')
      declare
          v_temp int;
       begin
        select count(*) into v_temp from audit_emp_change where name=:old.ename;
       if v_temp=0 then
           insert into audit_emp_change
         values(:old.ename,:old.sal,:new.sal,sysdate);
        else
           update audit_emp_change
               set oldsal_:old.sal,newsal=:new.sal, time=sysdate
           where name=:old.ename;
          end if;
       end;

 

 

eg:

     

CREATE TRIGGER after_IQC_Inspection 
 after insert on iqcm.iqc_inspection
 for each row
declare
 v_specid iqc_spec.id%type;
 v_subclassno iqc_spec.subclassno%type;
 
 --??????
 v_plant iqc_inspec_set.plant%type;
 v_grade iqc_inspec_set.geadeno%type;
 v_methodno iqc_inspec_set.methodno%type;
 v_countaql iqc_inspec_set.countaql%type;
 v_methodno_jl  iqc_inspec_set.measure_methodno%type;
begin

     --?????????
    
     declare cursor acc_cursor is
             select y.plant,y.geadeno,y.methodno,y.measure_methodno,y.countaql  
             from iqc_inspec_set y
             where y.plant=trim(:new.plant) and y.p_no=trim(:new.p_no);          
    begin
         open acc_cursor;
              fetch acc_cursor into v_plant,v_grade,v_methodno,v_methodno_jl,v_countaql;
         close acc_cursor;
    end;
                       
     --??????????
     declare cursor mycursor1 is
     select a.id,a.subclassno from iqc_spec a where exists
     (select b.specid from iqc_inspec_execute_spec b
             where b.specid=a.id and b.executeid=trim(:new.executeid));
     begin
     open mycursor1;
         
     fetch mycursor1 into v_specid,v_subclassno;
     while mycursor1%found  
         
     loop
    
         --???????? AQL,???? 
         if (v_subclassno<3) then
            insert into iqc_lot_info (stutasid,subclassno,spec_id,grade_no,method_no,acc_aql,plant)
            values(:new.id,v_subclassno,v_specid,v_grade,v_methodno,v_countaql,v_plant);
         else
             insert into iqc_lot_info (stutasid,subclassno,spec_id,method_no,plant)
             values(:new.id,v_subclassno,v_specid,v_methodno_jl,v_plant);
         end if;
         
         fetch mycursor1 into v_specid,v_subclassno;
         
     end loop;
     close mycursor1;
     end;
    
     --?????????
     insert into iqc_lot_info (stutasid,subclassno,spec_id,plant)
     values(:new.id,0,'F590039595F758E1E030850A6382459B',v_plant);
 
     --?????????????????
     insert into iqc_check (stutasid,subclassno,spec_id)
     values(:new.id,0,'F590039595F758E1E030850A6382459B');
    

     --dbms_output.put_line(v_plant||v_grade||v_mothod||v_countaql);
    
end;

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 药流20天同房了怎么办 药流23天同房了怎么办 小产后不来月经怎么办 人流12天同房了怎么办 人流20天同房了怎么办 人流后5天同房了怎么办 人流后3天同房了怎么办 人流20后同房了怎么办 上环后月经量多怎么办 上环后喝啤酒了怎么办 取环当天同房了怎么办 生完孩子有外痔怎么办 怀孕了宫腔积液怎么办 做完爱4天怕怀孕怎么办 半永久眉失败了怎么办 取环前2天同房了怎么办 怀孕了发现有子宫肌瘤怎么办 宫腔中央性粘连怎么办 孕妇宫腔粘连带怎么办 内膜厚怎么办吃什么好 功血引起的贫血怎么办 吃宫血宁后月经不来了怎么办 孕晚期胎心不好怎么办 胎心不好怎么办让住院 39 5胎心不好怎么办 肺长了一个肿瘤怎么办 血糖高伤口不愈合怎么办 有轻微的狐臭该怎么办 嘴上汗毛太重怎么办 风把裙子吹起怎么办 每天三四点醒来就睡不着怎么办 打游戏手汗太多怎么办 老年人胸闷气短呼吸困难怎么办 冒险岛宠物饿了怎么办 不小心喝了黑墨怎么办 压的双眼皮开了怎么办 割了双眼皮显老怎么办 心脏供血不足怎么办呢 怀孕了哮喘犯了怎么办 喝了电解质不拉怎么办 喝电解质散吐了怎么办