记录SQL Server报错时的调用堆栈
这是问题的后续 嵌套存储过程包含 TRY CATCH ROLLBACK 模式?
在 catch 中块我使用存储过程通过读取 ERROR_MESSAGE()、ERROR_PROCEDURE()、ERROR_LINE() 等来报告(重新引发)错误。如上所述 此处 我还进行了一项检查,以便确定错误是否已被重新抛出(这种情况发生在嵌套存储过程中,如下所示)错误信息通过每个 TRY CATCH 块向下传递)。
我想要做的,无论是直接在“ReportError”中,还是间接地用我的模式(如第一个问题中所述)记录堆栈跟踪 - 因此,当 ReportError 检测到它正在接收自己抛出的错误时,它会附加堆栈的下一层到错误消息。这将帮助我避免看到来自某个小实用程序存储过程的错误消息,而无法知道它的名称。如果我尝试直接在 ReportError 中执行此操作,则会失败,因为重新抛出的错误将其自身报告为来自 ReportError - 只有原始错误可见。
ReportError 是否有某种方法可以在 SQL Server 中执行堆栈跟踪,而无需向每个存储过程传递参数,也无需使用 #temp 表手动维护此类跟踪?基本上我想要递归调用 ERROR_PROCEDURE() 和 ERROR_LINE()。
This is a follow up to the question
Nested stored procedures containing TRY CATCH ROLLBACK pattern?
In the catch block I use a stored procedure to report (reraise) the error by reading from ERROR_MESSAGE(), ERROR_PROCEDURE(), ERROR_LINE(), etc. As described here I also have a check so that it can determine if the error has already been rethrown (this happens with nested stored procedures as the error information is passed down through each TRY CATCH block).
What I would like to do, either directly in 'ReportError', or indirectly with my pattern (as described in the first question), is record a stack trace - so when ReportError detects that it is receving an error thrown by itself, it appends the next level of the stack to the error message. This would help me avoid cases where I see an error message coming from some little utility stored procedure, without any way of knowing what called it. If I try doing this directly in ReportError it fails, since the rethrown error reports itself as coming from ReportError - only the original error is visible.
Is there some way for ReportError to perform a stack trace in SQL Server, without passing an argument to every single stored procedure, and without manually maintaining such a trace with #temp table? Basically I want a recursive call of ERROR_PROCEDURE() and ERROR_LINE().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我将在 :-) 中添加错误处理:
ERROR_%() 函数对于 CATCH 块的范围是可见的。这意味着您可以在每个 CATCH 块中的存储过程或函数调用中使用它们。
对于嵌套存储过程,了解导致错误的原因以及记录错误的内容很有用。
无论如何,这是基本思想:每个 CATCH 块都很简单,错误处理程序中的工作继续进行。例如,如果您愿意,请附加
ERROR_NUMBER()
Ok, I'll add our error handling back in :-)
The ERROR_%() functions are visible to the scope of the CATCH block. This means you can use them in a stored proc or function call in each CATCH block
And with nested stored procs, it's useful to know what caused the error and what's logging the error
This is the basic idea anyway: each CATCH block is simple, the work goes on in the error handler. Eg append
ERROR_NUMBER()
if you want to对此的有限答案是将 OBJECT_NAME(@@PROCID) 传递给 ReportError 过程 - 当 ReportError 检测到它正在接收递归错误(自身引发的错误)时,它可以使用该值并将其附加到错误消息中,提供部分堆栈跟踪(堆栈跟踪除了第一个元素之外没有行号)
A limited answer to this would be to pass OBJECT_NAME(@@PROCID) to the ReportError procedure - when ReportError detects that it is receving a recursive error (an error thrown by itself), it can use this value and append it to the error message, providing a partial stack trace (stack trace won't have line numbers except for the first element)