如何在 SQL 代理中公开有关存储过程失败的更多信息
我有一个 SQL 代理作业设置,在该作业中有一个执行存储过程的步骤。如果该存储过程失败,则 SQL 代理作业将显示一条错误消息,但没有其他信息。像堆栈跟踪或至少正在运行的存储过程和行号之类的东西将非常有用。
例如 如果执行以下存储过程,则会出现错误消息,例如“以用户身份执行:NT AUTHORITY\NETWORK SERVICE。启动[SQLSTATE 01000](消息0)无效的对象名称'NonExistentTable'。[SQLSTATE 42S02](错误208)。该步骤失败的。”没有任何迹象表明故障具体发生在哪里。
CREATE PROCEDURE TestSpLogging AS
BEGIN
PRINT 'Start'
SELECT * FROM NonExistentTable
PRINT 'End'
END
公开此信息的最佳方式是什么?
I have a SQL agent job setup and in that job there is a step to execute a stored proc. If that stored proc fails then the SQL agent job will display an error message but there is no other information. Something like a stacktrace or at least the stored proc that was running and the line number would be highly useful.
e.g.
If the following stored proc is executed then an error message like "Executed as user: NT AUTHORITY\NETWORK SERVICE. Start [SQLSTATE 01000] (Message 0) Invalid object name 'NonExistentTable'. [SQLSTATE 42S02] (Error 208). The step failed." with no indication where exactly the failure occured.
CREATE PROCEDURE TestSpLogging AS
BEGIN
PRINT 'Start'
SELECT * FROM NonExistentTable
PRINT 'End'
END
What's the best way to expose this information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 http://www.sommarskog.se/error_handling_2005.html 中详细介绍的方法似乎到目前为止工作足够。它只需要更新顶级存储过程,并将失败的存储过程的名称和行号输出到 SQL 代理。
输出错误将如下所示:
以用户身份执行:NT AUTHORITY\NETWORK SERVICE。 *** [InnerInnerStoredProc2], 5. Errno 208: 无效的对象名称“NonExistentTable”。 [SQLSTATE 42000](错误 50000)启动 [SQLSTATE 01000](错误 0)。这一步失败了。
步骤摘要:
创建以下错误处理程序存储过程:
将顶级存储过程包装在 try catch 中,如下所示
Using the approach detailed at http://www.sommarskog.se/error_handling_2005.html seems to be working sufficiently so far. It has only required an update to the top level stored procedure and will output the name of the stored procedure that failed and the line number to SQL agent.
The output error will look like this:
Executed as user: NT AUTHORITY\NETWORK SERVICE. *** [InnerInnerStoredProc2], 5. Errno 208: Invalid object name 'NonExistentTable'. [SQLSTATE 42000] (Error 50000) Start [SQLSTATE 01000] (Error 0). The step failed.
Summary of steps:
Create the following error handler stored procedure:
Wrap the top level stored proc in a try catch as follows
实现此目的的一种方法是向存储过程添加一些错误处理。这是我们在这里使用的一个简单方法,如下所示
One way to do this would be add some error handling to the stored procedure. Here is a simple method we use here is something like this