如何在该语句的触发器内获取受该语句影响的行数

发布于 2024-12-25 20:09:10 字数 700 浏览 1 评论 0原文

我有一个语句级触发器,每当对表(称为客户)执行 INSERT UPDATE 或 DELETE 操作时就会触发该触发器。我想显示一条消息(到 DBMS_OUTPUT),其中包含插入/更新/删除的行数。

我只想为每个触发语句一条消息,例如 “4 行已插入客户表”。

如何从触发器声明内部访问受触发语句影响的行数,即下面代码中的 XXX:

CREATE OR REPLACE TRIGGER customer_changes_trigger_2
AFTER INSERT OR UPDATE OR DELETE ON customers

DECLARE
v_operation   VARCHAR(10);
v_number_rows NUMBER;


BEGIN

v_number := XXX;

IF INSERTING THEN
   v_operation := 'inserted';
END IF;

IF UPDATING THEN
   v_operation := 'updated';
END IF;

IF DELETING THEN
   v_operation := 'deleted';
END IF;

DBMS_OUTPUT.PUT_LINE
          (v_number_rows|| ' rows were ' || v_operation || ' from customers.');
END;

在文档中找不到任何内容,感谢任何帮助!

I have a statement level trigger that fires whenever INSERT UPDATE or DELETE operations are performed on a table (called customers). I want to display a message (to DBMS_OUTPUT) containing the number of rows that were inserted/updated/deleted.

I just want one message for each triggering statement, eg
'4 rows were inserted into customers table'.

How can I access the number of rows that are affected by the triggering statement from INSIDE the trigger declaration, ie XXX in the code below:

CREATE OR REPLACE TRIGGER customer_changes_trigger_2
AFTER INSERT OR UPDATE OR DELETE ON customers

DECLARE
v_operation   VARCHAR(10);
v_number_rows NUMBER;


BEGIN

v_number := XXX;

IF INSERTING THEN
   v_operation := 'inserted';
END IF;

IF UPDATING THEN
   v_operation := 'updated';
END IF;

IF DELETING THEN
   v_operation := 'deleted';
END IF;

DBMS_OUTPUT.PUT_LINE
          (v_number_rows|| ' rows were ' || v_operation || ' from customers.');
END;

Can't find anything in the documentation, any help appreciated!

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

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

发布评论

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

评论(2

贱人配狗天长地久 2025-01-01 20:09:10

一种方法是使用全局变量来跟踪行数,因为没有其他方法可以从语句级触发器获取行计数。然后,您将需要三个触发器...一个语句级别用于在语句运行之前初始化变量,一个行级别用于为每一行的变量添加一,一个语句级别用于根据需要使用行计数。首先,设置变量和一些帮助它的过程:

create or replace package PKG_ROWCOUNT is
  NUMROWS   number;

  procedure INIT_ROWCOUNT;

  procedure ADD_ONE;

  function GET_ROWCOUNT
    return number;
end PKG_ROWCOUNT;
/

create or replace package body PKG_ROWCOUNT as
  procedure INIT_ROWCOUNT is
  begin
    NUMROWS := 0;
  end;

  procedure ADD_ONE is
  begin
    NUMROWS := Nvl(NUMROWS, 0) + 1;
  end;

  function GET_ROWCOUNT
    return number is
  begin
    return NUMROWS;
  end;
end PKG_ROWCOUNT;
/

第一个触发器用于初始化变量:

create or replace trigger CUSTOMER_CHANGES_TRIGGER_1
  before insert or update or delete
  on CUSTOMERS
begin
  PKG_ROWCOUNT.INIT_ROWCOUNT;
end;

第二个触发器用于更新每行:

create or replace trigger CUSTOMER_CHANGES_TRIGGER_2
  after insert or update or delete
  on CUSTOMERS
  for each row
begin
  PKG_ROWCOUNT.ADD_ONE;
end;
/

第三个触发器用于显示总计:

create or replace trigger CUSTOMER_CHANGES_TRIGGER_3
  after insert or update or delete
  on CUSTOMERS
begin
   Dbms_output.
   PUT_LINE(PKG_ROWCOUNT.GET_ROWCOUNT || ' rows were affected.');
end;

One way is to use a global variable to track the number of rows as there is no other way to get the row count from a statement level trigger. You would then need three triggers... one statement level to initialise the variable before the statement is run, one row level to add one to the variable for each row, one statement level to use the row count however you wish. First, set up the variable and a few procedures to help it:

create or replace package PKG_ROWCOUNT is
  NUMROWS   number;

  procedure INIT_ROWCOUNT;

  procedure ADD_ONE;

  function GET_ROWCOUNT
    return number;
end PKG_ROWCOUNT;
/

create or replace package body PKG_ROWCOUNT as
  procedure INIT_ROWCOUNT is
  begin
    NUMROWS := 0;
  end;

  procedure ADD_ONE is
  begin
    NUMROWS := Nvl(NUMROWS, 0) + 1;
  end;

  function GET_ROWCOUNT
    return number is
  begin
    return NUMROWS;
  end;
end PKG_ROWCOUNT;
/

The first trigger to initialise the variable:

create or replace trigger CUSTOMER_CHANGES_TRIGGER_1
  before insert or update or delete
  on CUSTOMERS
begin
  PKG_ROWCOUNT.INIT_ROWCOUNT;
end;

The second to update per row:

create or replace trigger CUSTOMER_CHANGES_TRIGGER_2
  after insert or update or delete
  on CUSTOMERS
  for each row
begin
  PKG_ROWCOUNT.ADD_ONE;
end;
/

The third to display the total:

create or replace trigger CUSTOMER_CHANGES_TRIGGER_3
  after insert or update or delete
  on CUSTOMERS
begin
   Dbms_output.
   PUT_LINE(PKG_ROWCOUNT.GET_ROWCOUNT || ' rows were affected.');
end;
南渊 2025-01-01 20:09:10

我不确定它是否在 AFTER 触发器主体内可用,但您可以尝试检查 sql%rowcount

I'm not 100$ sure if it's available inside AFTER trigger body, but you can try examining sql%rowcount

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