一:语句级触发器 语句级触发器是指当执行DML操作时,以语句为单位执行的触发器 (注意与下面提到的行级触发器比较) 先看代码
create or replace trigger xland_trigger
before insert
or update
or delete
on labor.xland
begin
if(to_char(sysdate,'DAY') in ('星期六','星期日'))
or (to_char(sysdate,'HH24') not between 8 and 18) then
raise_application_error(-20001,'不是上班时间');
end if;
end;
执行以下代码测试
insert into labor.xland (xland.title,xland.content,xland.state) values ('123','234',3);
ORACLE抛出异常
二:行级触发器 行级触发器是指执行DML操作时,以数据行为单位执行的触发器,每一行都执行一次触发器 先看代码:
create or replace trigger xland_trigger
before insert
on labor.xland
for each row
begin
if :new.title = 'xland' then
insert into labor.xland (xland.title,xland.content,xland.state) values ('123','cha2',3);
end if;
end;
执行以下代码测试
insert into labor.xland (xland.title,xland.content,xland.state) values ('xland','cha1',3);
结果:
在行级触发器中可以对列的值进行访问(很重要!) 列名前加 :old. 表示变化前的值 列名前加 :new. 表示变化后的值 在when子句中不用冒号。 三:instead of 触发器(视图上的触发器) 先看代码
create or replace trigger t_xland
instead of insert
on v_xland
for each row
begin
insert into xland (title,content,state) values ('1','1',1);
end;
其实就是取代了insert语句和其他触发器没什么大区别 四:删除触发器
drop trigger t_xland;
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有