日期:2014-05-16  浏览次数:20743 次

一个简单的触发器问题
我要用触发器实现每天下班时间不能访问表:
create or replace trigger hospital14_dui
before insert or update or delete on hospital14
declare 
v_time varchar2(20);
begin
v_time:=to_char(sysdate,'hh24:mi:ss');
 if (v_time>17:00:00 or v_time<8:00:00)
 then
 raise_application_error(-2000,'a table can not be modified');
 end if;
end;

结果编译出错,是不是 if (v_time>17:00:00 or v_time<8:00:00)
这句写错了?该怎么改呢?

------解决方案--------------------
if (v_time>'17:00:00' or v_time<'8:00:00')
------解决方案--------------------
其实你可以把触发器加一个 when ,这样比较简单

SQL code

create or replace trigger hospital14_dui
before insert or update or delete on hospital14
when (to_char(sysdate,'hh24') < '08' or to_char(sysdate, 'hh24') > '17') --每天 8 点以前和 17 点以后不得操作表
declare
begin
    raise_application_error(-20001,'a table can not be modified');
end;
/

------解决方案--------------------
范围不对哦
create or replace trigger hospital14_dui
before insert or update or delete on hospital14
declare
v_time varchar2(20);
begin
 v_time:=to_char(sysdate,'hh24:mi:ss');
 if (v_time>'17:00:00' or v_time<'08:00:00')
 then
 raise_application_error(-20001,'a table can not be modified');
 end if;
end;


------解决方案--------------------
那就 to_char(sysdate,'hh24') >= 17
代码是灵活的呀。。

探讨

引用:
create or replace trigger hospital14_dui
before insert or update or delete on hospital14
when (to_char(sysdate,'hh24') < '08' or to_char(sysdate, 'hh24') > '17') --每天 8 点以前和 ……