触发器例子

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');    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值