如何使用触发器编写一个 pl/sql 程序以不允许周日交易?



我有一个名为emp1的表

Create table emp1
(
Emp_no number(3),
Name varchar(10),
Salary number(9,2),
Dept_no number(3)
);

我的触发器是

create or replace trigger tri1
before insert on emp1
for each row
begin
if to_char(sysdate,'day')='sunday' then
dbms_output.put_line('You cannot access');
End if;
End;

执行此触发器后,我插入以下语句

insert into emp1(Name, Salary, Dept_no) values ('abc',12000,101);

每次我插入它总是入。

我也尝试过使用异常

create or replace trigger tri1 
before insert on emp1 
for each row 
declare
weekday_error exception;
begin 
if to_char(sysdate,'day')='sunday' then
raise weekday_error;
end if; 
Exception
when weekday_error then
dbms_output.put_line('You cannot access');
End;

使用此方法,记录也始终入。

create or replace trigger tri1 
before insert on emp1 
for each row 
begin 
if to_char(sysdate,'Day')='Sunday' then
raise_application_error(-20000,'Cannot do transaction on Sunday');
End if; 
End;

触发器只有一个小问题。TO_CHAR(sysdate,'day'( 实际上返回 9 个字符。

星期一是 6 个字符,然后是 3 个空格字符。这是因为星期三有 9 个字符。

您的触发器只需要修剪功能

create trigger trig1 before insert on emp1
for each row
begin
if trim(to_char(sysdate, 'day')) = 'sunday' then
raise_application_error(-20000,'ikidyounot, today was - ' || to_char(sysdate, 'day'));
end if;
end;
/

相关内容

最新更新