复合触发器可以在不改变原始代码的情况下统计并显示修改的行数。
这里仍然存在一些问题和挑战。此解决方案可能不适用于并行 DML - 它要么无法正确计数,要么触发器将阻止直接路径写入。它可能会在多用户环境中工作,但需要进行测试。您还需要为 DELETE 和 MERGE 构建代码。这可能会减慢 DML。
示例架构
create table users(id number, username varchar2(100));
insert into users values(1, 'system');
create table table1(col1 number, updated_at date, updated_by number);
insert into table1 values(1, null, null);
insert into table1 values(2, null, null);
create table table2(col1 number, updated_at date, updated_by number);
防止DBMS_OUTPUT过多的包
不断打印输出可能会导致问题。所以我们要默认禁用输出。而且您可能不只是想使用DBMS_OUTPUT.DISABLE,这可能会关闭其他东西,而且很难始终记住运行它。
使用全局变量创建一个简单的包。
create or replace package print_feedback is
--Outputing large amounts of data can sometimes break things.
--Only enable DBMS_OUTPUT when explicitly requested.
g_print_output boolean := false;
end;
/
在运行导入之前将其设置为TRUE。
--Run this block first to enable printing.
begin
print_feedback.g_print_output := true;
end;
/
PL/SQL 块创建 INSERT 和 UPDATE 触发器
此代码动态生成触发器以捕获 INSERT 和 UPDATE。
动态 PL/SQL 有点棘手。请注意,我使用模板和替代引用机制来避免串联地狱。一旦你理解了这些技巧,代码就会变得相对可读。 (希望您的 IDE 理解 q'[ 如何比 StackOverflow 语法高亮显示更好。)
--Create automatic UPDATE and INSERT feedback triggers.
declare
c_sql_template constant varchar2(32767) :=
q'[
create or replace trigger #TABLE_NAME#_#UPD_or_INS#_trg for #UPDATE_OR_INSERT# on #TABLE_NAME# compound trigger
--Purpose: Print a feedback message after updates and inserts.
g_count number := 0;
after each row is
begin
g_count := g_count + 1;
end after each row;
after statement is
begin
if print_feedback.g_print_output then
if g_count = 1 then
dbms_output.put_line('#Inserted_or_Updated# '||g_count||' row in #TABLE_NAME#');
else
dbms_output.put_line('#Inserted_or_Updated# '||g_count||' rows in #TABLE_NAME#');
end if;
end if;
end after statement;
end;
]';
v_sql varchar2(32767);
begin
--Loop through the relevant tables
for tables in
(
select table_name
from user_tables
where table_name in ('TABLE1', 'TABLE2')
order by table_name
) loop
--Create and execute update trigger.
v_sql := replace(replace(replace(replace(c_sql_template
, '#TABLE_NAME#', tables.table_name)
, '#UPD_or_INS#', 'upd')
, '#UPDATE_OR_INSERT#', 'update')
, '#Inserted_or_Updated#', 'Updated');
execute immediate v_sql;
--Create and execute insert trigger.
v_sql := replace(replace(replace(replace(c_sql_template
, '#TABLE_NAME#', tables.table_name)
, '#UPD_or_INS#', 'ins')
, '#UPDATE_OR_INSERT#', 'insert')
, '#Inserted_or_Updated#', 'Inserted');
execute immediate v_sql;
end loop;
end;
/
样品运行
现在您未更改的脚本将显示一些输出。 (我确实对脚本做了一些微不足道的更改,但只是为了使其可运行。)
SQL> --Run this block first to enable printing.
SQL> set serveroutput on;
SQL> begin
2 print_feedback.g_print_output := true;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> DECLARE
2 v_user_id users.id%TYPE;
3 BEGIN
4 SELECT id INTO v_user_id FROM users WHERE username = 'system';
5
6 UPDATE table1
7 SET col1 = 1,--value,
8 updated_at = SYSDATE,
9 updated_by = v_user_id
10 WHERE 1=1;--some condition;
11
12 INSERT INTO table2 values(2/*val1*/, SYSDATE, v_user_id);
13 END;
14 /
Updated 2 rows in TABLE1
Inserted 1 row in TABLE2
PL/SQL procedure successfully completed.
SQL>