如何在oracle 10g中为存储过程创建错误日志?

发布于 2024-10-01 17:41:28 字数 258 浏览 3 评论 0原文

我需要一个为 Oracle 中的存储过程创建错误日志文件的示例。 请给我一个有关表创建、存储过程创建和错误日志创建的示例。

预先感谢

编辑(其他问题的相关信息)

假设有一个存储过程。当我执行该存储过程时,可能会发生一些预期的错误/异常,因此我需要创建一个错误日志表,每当我执行该存储过程时,所有错误都会自动存储在其中。

例如,如果某个列不允许空值,但用户输入空值,则应生成该错误并将其存储在错误日志表中。

I need an example of creating error log file for stored procedure in oracle.
please give me an example with table creation and stored procedure creation and error log creation.

Thanks in advance

EDIT (relevant info from other question)

Suppose there is a stored procedure. When I am executing that stored procedure, some expected error/exception may occur, so I need to create an error log table in which all the errors will automatically be store whenever I will execute the stored procedure.

For example, if there is some column which does not allow null values, but the user is entering null values, then that error should be generated and it should stored in the error log table.

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

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

发布评论

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

评论(2

狼性发作 2024-10-08 17:41:28

您还没有真正给出有关您的要求的详细信息。这是一个简单的错误日志表以及将错误消息记录到其中的过程:

CREATE TABLE error_log (ts TIMESTAMP NOT NULL, msg VARCHAR2(4000));

CREATE PROCEDURE log_error (msg IN VARCHAR2) IS
BEGIN
  INSERT INTO error_log (ts, msg)
  VALUES (SYSTIMESTAMP, SUBSTR(insert_log.msg, 1, 4000));
END log_error;

您可能需要也可能不需要它作为自治事务。这取决于您是否希望日志记录回滚其更改的过程中的错误。

通常,这将在更通用的日志系统中实现,该系统不仅会记录错误,还会记录警告和调试信息。

如果您希望 DML 语句(插入/更新/删除)记录每行的错误(而不是仅在出现错误的第一行上失败),您可以使用 LOG ERRORS 子句 - 而不是语句失败,该语句将成功,并且未插入/更新/删除的行将连同适用的错误代码和错误消息一起写入您指定的错误日志表中。请参阅vettipayyan 提供的链接。

如果您希望记录过程中引发的所有异常,您可以使用 WHEN OTHERS 捕获它们:

BEGIN
  -- your code here
EXCEPTION
  WHEN OTHERS THEN
    log_error(DBMS_UTILITY.format_error_stack);
    log_error(DBMS_UTILITY.format_error_backtrace);
    RAISE;
END;

You haven't really given a lot of detail about your requirements. Here is a simple error log table and a procedure to log error messages into it:

CREATE TABLE error_log (ts TIMESTAMP NOT NULL, msg VARCHAR2(4000));

CREATE PROCEDURE log_error (msg IN VARCHAR2) IS
BEGIN
  INSERT INTO error_log (ts, msg)
  VALUES (SYSTIMESTAMP, SUBSTR(insert_log.msg, 1, 4000));
END log_error;

You might or might not need it to be an autonomous transaction. That would depend on whether you want the log to record errors from procedures that rollback their changes.

Typically, this will be implemented in a more generic logging system which would log not only errors, but warnings and debug info too.

If you want a DML statement (insert/update/delete) to log an error for each row (instead of just failing on the first row that errors), you can use the LOG ERRORS clause - instead of the statement failing, the statement will succeed, and the rows that were not inserted/updated/deleted will be written to the error log table you specify, along with the error code and error message applicable. Refer to the link provided by vettipayyan.

If you want all exceptions that are raised within a procedure to be logged, you can catch them with WHEN OTHERS:

BEGIN
  -- your code here
EXCEPTION
  WHEN OTHERS THEN
    log_error(DBMS_UTILITY.format_error_stack);
    log_error(DBMS_UTILITY.format_error_backtrace);
    RAISE;
END;
紫﹏色ふ单纯 2024-10-08 17:41:28

这是带有代码示例的页面:
DML 错误日志记录

Here's the page with code samles:
DML ErrorLogging

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