gpt4 book ai didi

oracle - 如何将 DBMS_OUTPUT.PUT_LINE 的输出重定向到文件?

转载 作者:行者123 更新时间:2023-12-02 12:40:03 35 4
gpt4 key购买 nike

我需要在 pl/sql 中调试来计算程序的时间,我想使用:

SELECT systimestamp FROM dual INTO time_db;
DBMS_OUTPUT.PUT_LINE('time before procedure ' || time_db);

但我不明白输出到哪里以及如何将其重定向到包含我想要收集的所有数据的日志文件?

最佳答案

DBMS_OUTPUT 不是最好的调试工具,因为大多数环境本身并不使用它。但是,如果您想捕获 DBMS_OUTPUT 的输出,则只需使用 DBMS_OUTPUT.get_line 过程即可。

这是一个小例子:

SQL> create directory tmp as '/tmp/';

Directory created

SQL> CREATE OR REPLACE PROCEDURE write_log AS
2 l_line VARCHAR2(255);
3 l_done NUMBER;
4 l_file utl_file.file_type;
5 BEGIN
6 l_file := utl_file.fopen('TMP', 'foo.log', 'A');
7 LOOP
8 EXIT WHEN l_done = 1;
9 dbms_output.get_line(l_line, l_done);
10 utl_file.put_line(l_file, l_line);
11 END LOOP;
12 utl_file.fflush(l_file);
13 utl_file.fclose(l_file);
14 END write_log;
15 /

Procedure created

SQL> BEGIN
2 dbms_output.enable(100000);
3 -- write something to DBMS_OUTPUT
4 dbms_output.put_line('this is a test');
5 -- write the content of the buffer to a file
6 write_log;
7 END;
8 /

PL/SQL procedure successfully completed

SQL> host cat /tmp/foo.log

this is a test

关于oracle - 如何将 DBMS_OUTPUT.PUT_LINE 的输出重定向到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1453538/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com