日期:2014-05-17  浏览次数:20916 次

oracle 触发器。。。。

create or replace trigger securityemp
before insert
on emps
declare
  out_worktime exception;
begin
  if to_char(sysdate,'day') in ('星期六','星期日','星期五') or
  to_number(to_char(sysdate,'hh24')) not between 9 and 18 then
  raise out_worktime;
  end if;
  exception
  when out_worktime then dbms_output.put_line('do not insert data during non-work time');
end;
/
目的是禁止在指定日期时间向emps表中插入数据。
我都抛出异常,但是为什么数据还是插入了呢?如何修改?求解,本人小白。
------解决方案--------------------
create or replace trigger securityemp
before insert
on emps
declare
  out_worktime exception;
begin
  if to_char(sysdate,'day') in ('星期六','星期日','星期五') or
  to_number(to_char(sysdate,'hh24')) not between 9 and 18 then
  raise out_worktime;
  end if;
  exception
  when out_worktime then 
     dbms_output.put_line('do not insert data during non-work time');
     raise_application_error(-20202,'规定时间不允许插入'); 
end;
/
异常不能在触发器里处理掉,需要向外抛才能终端插入操作
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:


create or replace trigger securityemp
before insert
on emps
declare
  out_worktime exception;
begin
  if to_char(sysdate,'day') in ('星期六','星期日','星期五') or
  to_number(to_char(sysdate,'hh24')) not between 9 and 18 then
  raise out_worktime;
  end if;
  exception
  when out_worktime then dbms_output.put_line('do not insert data during non-work time');
end;
/
目的是禁止在指定日期时间向emps表中插入数据。
我都抛出异常,但是为什么数据还是插入了呢?如何修改?求解,本人小白。
create or replace trigger securityemp
before insert
on cc
for each row 

begin
  if to_char(sysdate,'day') in ('星期六','星期日','星期五') or
  to_number(to_char(sysdate,'hh24')) not between 9 and 18 then
  raise_application_error(-20202,'规定时间不允许插入');
  end if;
end;
这样就ok了

为什么只能抛这种异常呢?这种异常是系统的吧?自定义异常为什么达不到目的呢,难道只有_application_error这种异常才行吗?
RAISE_APPLICATION_ERROR这个函数是将应用程序专有的错误从服务器端转达到客户端应用程序(plsql/sqlplus等)-20000到-30000是可以的自定义代码,否则会与oracle自带的冲突