主页 M

oracle触发器old,new 与行级与语句级及case when

2014-12-02 网页编程网 网页编程网

42

在表中添加一条数据,提示“添加了一条数据”
create or replace triger addTriger
after insert on tableAddUser
--for each row
begin
dbms_output.put_line('add a row');
end;
上例中未用for each row,但若加多条时也只提示一次。
故若要提示多次,要加上for each row


语句级的,只会提示一次。
若是加上for each row,只会提示多次的,是行级语句 


在每周的星期六与星期日不能修改表。
create triger trq
before delete on emp
begin 
if to_char(sysdate,'day') in ('星期六','星期日') then

dbms_output.put_line('can not delete today');
RAISE_APPLICATION_ERROR(-20001,'can not delete.');
end;
挡不住的,因为只是提示一下。要出来一个过程。自定义错误编号 


2.使用条件谓词来精确提示操作
create trigger tr3
before
insert or update or delete on emp
begin
case 
when inserting then dbms_output.put_line('你要执行insert')
raise_application_error(-2002,'你要执行insert');
when updating then
dbms_output.put_line('你要执行update')
raise_application_error(-2003,'你要执行update');
when deleting then
dbms_output.put_line('你要执行delete')
raise_application_error(-2005,'你要执行delete');
end case;
end;
3.old new
特性 insert update delete
old   null    有效   有效
new    有效    有效   null

--给所有员工涨工资之触发器
create or replace trigger tri5
before update on scott.emp
for each row
begin
if :new.sal<:old.sql then
dbms_output.put_line('工资不能低于原来的工资 ');
else
dbms_output.put_line('原来工资是'||:old.sal||:new.sal);
end if;
end;

//注意,再更新一套语句试试。不要用Insert.

阅读原文
阅读 6460
123 显示电脑版