【问题标题】:How to call procedure using triggers如何使用触发器调用过程
【发布时间】:2023-04-02 08:40:01
【问题描述】:

表:

create table department(deptno number, deptname varchar2(50), deptloc varchar2(50));

insert into department values(1,'A','X');
insert into department values(2,'B','Y');
insert into department values(3,'C','Z');

存储过程:

create or replace procedure secure_dml(i_month IN varchar2)
is

begin
if i_month <> 'March' then
dbms_output.put_line('You can modify or add a department only at the end of a financial year');
else 
--should I write insert/update DML statement?

end;

触发器:

create or replace trigger tr_check_dept before insert on department
begin
dbms_output.put_line('Record inserted');
end;

要求:

在过程和触发器的帮助下实现以下业务规则:-

  • 我。仅允许在 3 月份更改 Department 表中的数据。

  • 二。创建一个名为 SECURE_DML 的过程,以防止 DML 语句在除 3 月之外的任何其他月份执行。如果用户尝试在除 3 月之外的任何其他月份修改表,则该过程应显示一条消息

    “您只能在财政年度结束时修改或添加部门”

  • 三。在调用上述过程的 Department 表上创建一个名为 TR_CHECK_DEPT 的语句级触发器。

  • 四。通过在 Department 表中插入一条新记录来测试它

【问题讨论】:

    标签: oracle stored-procedures plsql triggers


    【解决方案1】:

    基本上,您可以在触发器内完成所有操作,但没关系 - 这是某种家庭作业。我是这样理解的。

    程序不会做任何“智能”的事情,只是显示消息。请注意,DBMS_OUTPUT.PUT_LINE 调用会显示一条消息,它不会阻止任何人做任何事情 - 而不是它,您应该 raise_application_error

    你的问题的答案

    我应该编写插入/更新 DML 语句吗?

    是 - 在我看来 - ,你不应该。


    触发器调用该过程。您的不检查月份,而应该检查(即将该控件从程序移至触发器)。


    所有东西放在一起可能看起来像这样:

    SQL> create or replace procedure secure_dml
      2  is
      3  begin
      4    raise_application_error(-20000,
      5      'You can modify or add a department only at the end of a financial year');
      6  end;
      7  /
    
    Procedure created.
    
    SQL> create or replace trigger tr_check_dept
      2    before insert on department
      3    for each row
      4  begin
      5    if extract(month from sysdate) <> 3 then
      6       secure_dml;
      7    end if;
      8  end;
      9  /
    
    Trigger created.
    
    SQL> insert into department(deptno, deptname, deptloc)
      2    values (4, 'D', 'W');
    insert into department(deptno, deptname, deptloc)
                *
    ERROR at line 1:
    ORA-20000: You can modify or add a department only at the end of a financial year
    ORA-06512: at "SCOTT.SECURE_DML", line 4
    ORA-06512: at "SCOTT.TR_CHECK_DEPT", line 3
    ORA-04088: error during execution of trigger 'SCOTT.TR_CHECK_DEPT'
    
    
    SQL>
    

    仅出于测试目的,因为今天是 9 月,让我们修改触发器代码,使其适用于本月(而不是 3 月),看看 INSERT 在这种情况下做了什么。

    SQL> create or replace trigger tr_check_dept
      2    before insert on department
      3    for each row
      4  begin
      5    if extract(month from sysdate) <> 9 then      --> this line was changed
      6       secure_dml;
      7    end if;
      8  end;
      9  /
    
    Trigger created.
    
    SQL> insert into department(deptno, deptname, deptloc)
      2    values (4, 'D', 'W');
    
    1 row created.
    
    SQL>
    

    对;现在可以了。

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      相关资源
      最近更新 更多