有什么方法可以跟踪数据库中 oracle 函数插入的行

Is there any way to keep a track of rows inserted by a oracle function in database

我有一个程序可以将余额从开始日期更新到结束日期,并且 我还想跟踪插入的记录数。我正在使用 dbms_output.put_line 来获取插入的记录数,但它不提供任何输出,当执行完成时,将显示计数的输出。程序代码如下:

create or replace function updatebal(start_date IN DATE, end_date IN DATE)
RETURN NUMBER 
IS
difference number;
curr_r  number;
BEGIN 
difference := end_date - start_date;
curr_r := 0;
while curr_r <= difference LOOP
curr_r := curr_r + 10;
for curr_in in 1..10 LOOP
date_value := date_value +1 ;
insertAvailBal(date_value);
commit;
select count(*) into totalCount from avail_bal;
dbms_output.put_line('total count' || totalCount);
end loop;
END LOOP;
RETURN 1;
END;

现在我正尝试从此过程中打印 totalCount 以获得在此 table avail_bal 中插入的行数。但是没有输出。 请帮助我,在此先感谢

这就是 dbms_output 的工作原理,它在 运行 完成后显示所有输出,您无法实时监控它。

如果您真的需要实时监控进度,您可以使用具有自治事务的过程将消息插入特殊的 table,然后 从另一个会话 你可以在进程还在 运行ning 时查看那个 table 的内容。

此类过程的示例:

procedure log_message (p_message varchar2) is
   pragma autonomous_transaction;
begin
   insert into message_table (message) values (p_message);
   commit;
end;

正如 Tony 已经回答的那样:您无法更改 dbms_output 的行为。

向存储过程外部发送进度信号的推荐方法是使用 dbms_application_info 包来管理 v$session_longops

中的信息

您甚至可以为外循环和内循环管理单独的进度指示器。 v$session_longops 甚至会根据一段时间内的平均持续时间显示该过程需要多长时间的估计值。如果每个(报告的)步骤的运行时间相当恒定,那么这些估计就相当准确。

您可以像这样增强您的功能:

create or replace function updatebal(start_date IN DATE, end_date IN DATE)
  RETURN NUMBER 
IS
  difference number;
  curr_r  number;
  main_index binary_integer;
  sub_index  binary_integer;
  main_slno  binary_integer;
  sub_slno   binary_integer;

BEGIN 
  difference := end_date - start_date;
  curr_r := 0;

  -- initialize the module information
  dbms_application_info.set_module('updatebal', 'Calculate Balance');

  -- initialize two different "handles" for the inner and outer loop
  main_index := dbms_application_info.set_session_longops_nohint;
  sub_index := dbms_application_info.set_session_longops_nohint;

  while curr_r <= difference LOOP
    curr_r := curr_r + 10;

    -- report each outer step
    dbms_application_info.set_session_longops(rindex => main_index, 
            slno => main_slno, 
            op_name => 'main loop', 
            sofar => curr_r, 
            totalwork => difference);

    for curr_in in 1..10 LOOP

      date_value := date_value +1;

      insertAvailBal(date_value);
      commit;

      select count(*) into totalCount from avail_bal;

      -- report each inner step with the totalcount
      dbms_application_info.set_session_longops(
                      rindex => sub_index, 
                      slno => sub_slno, 
                      op_name => 'Sub Loop, totalcount'||totalcount, 
                      sofar => curr_in, totalwork => 10);

    end loop;
  END LOOP;

  RETURN 1;

  dbms_application_info.set_module(null,null);
END;
/

查看手册了解更多详情:
https://docs.oracle.com/database/121/ARPLS/d_appinf.htm#ARPLS003