我正在学习如何制作PL/SQL触发器。这就是我编码的,但我会遇到很多错误

发布于 2025-02-12 08:21:20 字数 572 浏览 0 评论 0原文

这是我的教练给我的描述

为“工作”表创建一个触发器,该表将启动插入或更新或删除在“工作”表上执行的操作。该触发器将显示旧值和新值之间的薪水差异。编写一个集合PL/SQL以测试触发器的执行,以响应某些数据库操作(DML)语句(删除,插入或更新)。

这是代码:

create or replace trigger salary_diff
before delete or insert or update on Worker
for each row 
when (new.Worker_id > 0 )

declare
sal_diff number;
begin
sal_diff := :new.salary - :old.salary;
dbms_output.put_line('new salary ' ||  :new.salar)
dbms_output.put_line('old salary ' ||  :old.salary)
dbms_output.put_line('difference between salary is ' || sal_diff);
end;

this is the description my instructor gave me

Create a trigger for the “Worker” table that would fire for INSERT or UPDATE or DELETE operations performed on the “Worker” table. This trigger will display the salary difference between the old values and new values. Write a bloc PL/SQL to test the execution of the trigger in response to some database manipulation (DML) statement (DELETE, INSERT, or UPDATE).

and this is the code:

create or replace trigger salary_diff
before delete or insert or update on Worker
for each row 
when (new.Worker_id > 0 )

declare
sal_diff number;
begin
sal_diff := :new.salary - :old.salary;
dbms_output.put_line('new salary ' ||  :new.salar)
dbms_output.put_line('old salary ' ||  :old.salary)
dbms_output.put_line('difference between salary is ' || sal_diff);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2025-02-19 08:21:20

您提到了“吨错误”,但是 - 实际上,只有两个:

  • 一个防止触发的编译(dbms_output.put_line呼叫之后丢失的semi -colon)
  • 另一个会阻止触发时触发删除行(因为 - 在这种情况下 - 没有:任何列的new值),因此 - 条款

so:示例表:

SQL> CREATE TABLE worker
  2  AS
  3     SELECT 1 worker_id, 100 salary FROM DUAL;

Table created.

触发器:testing:

SQL> CREATE OR REPLACE TRIGGER salary_diff
  2     BEFORE DELETE OR INSERT OR UPDATE
  3     ON Worker
  4     FOR EACH ROW
  5  DECLARE
  6     sal_diff  NUMBER;
  7  BEGIN
  8     sal_diff := :new.salary - :old.salary;
  9     DBMS_OUTPUT.put_line ('new salary ' || :new.salary);
 10     DBMS_OUTPUT.put_line ('old salary ' || :old.salary);
 11     DBMS_OUTPUT.put_line ('difference between salary is ' || sal_diff);
 12  END;
 13  /

Trigger created.

testing:

SQL> SET SERVEROUTPUT ON
SQL> UPDATE worker
  2     SET salary = 50
  3   WHERE worker_id = 1;
new salary 50
old salary 100
difference between salary is -50

1 row updated.

SQL> DELETE FROM worker
  2    WHERE worker_id = 1;
new salary
old salary 50
difference between salary is

1 row deleted.

SQL>

(删除时,没有“差异”,因为工人不再存在。

You mentioned "tons of errors", but - really - there are only two:

  • one that prevents trigger to compile (missing semi-colon after DBMS_OUTPUT.PUT_LINE calls)
  • another that will prevent trigger to fire when you delete rows (because - in that case - there's no :new value for any column) so - remove the when clause

So: sample table:

SQL> CREATE TABLE worker
  2  AS
  3     SELECT 1 worker_id, 100 salary FROM DUAL;

Table created.

Trigger:

SQL> CREATE OR REPLACE TRIGGER salary_diff
  2     BEFORE DELETE OR INSERT OR UPDATE
  3     ON Worker
  4     FOR EACH ROW
  5  DECLARE
  6     sal_diff  NUMBER;
  7  BEGIN
  8     sal_diff := :new.salary - :old.salary;
  9     DBMS_OUTPUT.put_line ('new salary ' || :new.salary);
 10     DBMS_OUTPUT.put_line ('old salary ' || :old.salary);
 11     DBMS_OUTPUT.put_line ('difference between salary is ' || sal_diff);
 12  END;
 13  /

Trigger created.

Testing:

SQL> SET SERVEROUTPUT ON
SQL> UPDATE worker
  2     SET salary = 50
  3   WHERE worker_id = 1;
new salary 50
old salary 100
difference between salary is -50

1 row updated.

SQL> DELETE FROM worker
  2    WHERE worker_id = 1;
new salary
old salary 50
difference between salary is

1 row deleted.

SQL>

(when deleting, there's no "difference" because worker doesn't exist any more. If you'd want to see the difference anyway, use NVL function)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文