触发器例子

来源:互联网 发布:曹县大集淘宝村地图 编辑:程序博客网 时间:2024/09/21 08:57

create or replace TRIGGER UPDATE_PARTCUT
AFTER INSERT OR UPDATE OR DELETE OF par_no //凡是新增,修改,删除都触发
on PARTCUT
FOR EACH ROW  //每一笔资料变更时都触发
begin
if Inserting then //当是新增的时候,在触发器中以OLD表示以前的资料,NEW表示更新的资料
begin
 Insert into styleusage(sty_no,par_no,cus_no,difflast) values(:NEW.sty_no,:NEW.par_no,:NEW.cus_no,:NEW.difflast);
end;
end if;
if updating then
begin
  update styleusage set sty_no=:NEW.sty_no,par_no=:NEW.par_no,cus_no=:NEW.cus_no,difflast=:NEW.difflast where sty_no=:OLD.sty_no and par_no=:OLD.par_no and cus_no=:OLD.cus_no and difflast=:OLD.difflast;
end;
end if;
if deleting then
begin
  delete from styleusage where sty_no=:OLD.sty_no and par_no=:OLD.par_no and cus_no=:OLD.cus_no and difflast=:OLD.difflast;
end;
end if;
end;
以上的ORACLE的格式,其他的数据库有所不同,但原理一样. 

 

 

过Delphi建立和删除触发器

 
关键词:建立触发器 Trigger  
  adocommand1.CommandText:='CREATE TRIGGER trigger_name ON table1 FOR INSERT, UPDATE, DELETE AS insert into table2 (name,old) values('''+edit1.text+''','''+edit2.text+''')';
   showmessage(adocommand1.CommandText);
   adocommand1.Execute ;
  // adoquery1.SQL.Text:='CREATE TRIGGER trigger_name ON table1 FOR INSERT, UPDATE, DELETE AS insert into table2 (name,old) values('''+edit1.text+''','''+edit2.text+''')';
  // showmessage(adoquery1.SQL.Text);

   button1.Enabled:=false;
   button2.Enabled:=true;

   showmessage('ok');   //创建触发器

     adocommand1.Commandtext:='drop trigger trigger_name';
 adocommand1.Execute;
  button1.Enabled:=true;
  button2.Enabled:=false;   //删除触发器
 showmessage('ok');    

原创粉丝点击