Plsql 如何编写pl/sql程序,使其不允许在星期天使用触发器进行事务处理?

Plsql 如何编写pl/sql程序,使其不允许在星期天使用触发器进行事务处理?,plsql,triggers,plsqldeveloper,datatrigger,Plsql,Triggers,Plsqldeveloper,Datatrigger,我有一张名为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_o

我有一张名为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 

    begin 
    if to_char(sysdate,'Day')='Sunday' then
    raise_application_error(-20000,'Cannot do transaction on Sunday');
    End if; 

End;
我也尝试过使用exception

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;
/

这是因为
to_char(sysdate,'day')
的结果是星期天而不是星期天。为了得到星期天,你应该写Day而不是Day。我把它改为Day和Sunday,但它仍然不起作用。触发器正在创建,但当我插入记录时inserted@jaicy-你在周日测试了你问题中的第二个触发器吗?:)我看不出有什么问题。应该行的。是的。。。我也试过了。但是每次它被插入并且没有显示“你不能访问”时,我是否以错误的方式插入了它?创建触发器后,我直接尝试插入它。哦,好的。。。谢谢。感谢您抽出宝贵的时间回答。我试试这个,然后告诉你soon@JaicyJoseph-如果测试后对你有帮助,请在回答的问题上做标记:)哇。。棒 极 了先生,非常感谢您对我的帮助。它起作用了