问题代码:
create or replace trigger securityemp
before insert
on emp
begin
if to_char(sysdate,'day') in ('tuesday') or
to_number(to_char(sysdate,'hh24')) not between 9 and 24 then
--禁止insert
raise_application_error(-20001,'禁止星期二或者0~9间插入新员工');
end if;
end securityemp;
问题:当在星期二时,仍可以进入插入数据操作
原因:他的返回值右面补了两个空格,也就是说他返回的不是“tuesday”,而是“tuesday ”
解决:加两个空格或加trim(..)
解决代码1:
create or replace trigger securityemp
before insert
on emp
begin
if to_char(sysdate,'day') in ('tuesday ') or
to_number(to_char(sysdate,'hh24')) not between 9 and 24 then
--禁止insert
raise_application_error(-20001,'禁止在非工作时间插入新员工');
end if;
end securityemp;
解决代码2:
create or replace trigger securityemp
before insert
on emp
begin
if trim(to_char(sysdate,'day')) in ('tuesday') or
to_number(to_char(sysdate,'hh24')) not between 9 and 24 then
--禁止insert
raise_application_error(-20001,'禁止在非工作时间插入新员工');
end if;
end securityemp;