Posted on 2007-07-14 22:35
leekiang 阅读(323)
评论(0) 编辑 收藏 所属分类:
oracle
create or replace trigger auth_secure
before insert or update or delete
on auths //对整表更新前触发
begin
if(to_char(sysdate,'DY')='SUN'
RAISE_APPLICATION_ERROR(-20600,'不能在周末修改表auths');
end if;
end
RAISE_APPLICATION_ERROR的错误代码在-20000到-20999之间,这样就不会与 ORACLE 的任何错误代码发生冲突
禁止ddl操作
create or replace trigger ddl_deny
before create or alter or drop or truncate on database//系统级别的触发器
declare
v_errmsg varchar2(100):= 'You have no permission to this operation';
begin
if ora_sysevent = 'CREATE' then
raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || v_errmsg);
elsif ora_sysevent = 'ALTER' then
raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || v_errmsg);
elsif ora_sysevent = 'DROP' then
raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || v_errmsg);
elsif ora_sysevent = 'TRUNCATE' then
raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || v_errmsg);
end if;
exception
when no_data_found then
null;
end;
又一例:
create or replace trigger his_trig_u
after update ON MYDATA
FOR EACH ROW
declare
temp_count number;
BEGIN
select count(*)
into temp_count
from DATAHIS t
where t.num = :new.num
and t.time = :new.time;
if temp_count = 0 then
insert into DATAHIS
(uid, num, value, time)
values
(data_sql.nextval,
:new.num,
:new.value,
:new.time);
end if;
END;
begin前面出现的new,old不加冒号,begin和end之间出现的new和old都要在前面加上":"
用触发器完成动态数据的操作
在涉及如何实现动态库存的问题时,可用触发器解决。仓库有验收、出库、调拨、报废、退料、让售等这些数据必须与以前的库存相加减,才能完成动态库存操作。本文仅以验收单触发器为例,其它的结构雷同。它们涉及到两个基表:bi_ysd(验收单),Bj_kcb(当前库存表),前者的表结构(rq(日期),ysdh(验收单号), bjbm(备件编码),yssl(验收数量),ysdj(验收单价)),后者的表结构为(bjbm(备件编码),dqkcl(当前库存量),dqkcje(当前库存金额))触发器如下:
create or replace trigger trig_ysd
after insert or update or delete on bj_ysd
for each row
declare rq1 varchar2(8);rq2 varchar2(8);
/*限于篇幅,yssl1,yssl2,ysdj1,ysdj2,bjbm1,bjbm2,ii声明略*/
if inserting or updating then
rq1:=:new.rq;bjbm1:=:new.bjbm;yssl1:=:new.yssl;
ysdj1:=:new.ysdj;
select count(*) into ii from bj_dqkcb
where bjbm=bjbm1;
if ii=0 then
insert into bj_dqkcb(bjbm,dqkcl,dqkcje)
value(bjbm1,yssl1,ysdj1);
else
update bj_dqkcb
set dqkcl=dqkcl+yssl1;
dqkcje=dqkcje+yssl1*ysdj1;
end if
end if
if deleting or updating then
rq2:=:old.rq;
bjbm2:=:old.bjbm;
yssl2:=:old.yssl;
ysdj2:=:old.ysdj;
update bj_dqkcb
set dqkcb=dqkcl-yssl2;
dqkcje=dqkcje-yssl2*ysdj2
end if;
end ;