处理存储过程异常 [英] Handling Stored Procedure Exception

查看:93
本文介绍了处理存储过程异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果书面存储过程中有任何异常,那么该如何处理?

解决方案

这些链接将帮助您继续进行下去:
SQL Server 2000和更高版本中的异常处理2005 [ ^ ]

SQL Server 2000和2005-2中的异常处理 [ 解决方案

These links would help you to go ahead:
Exception Handling in SQL Server 2000 and 2005[^]

Exception Handling in SQL Server 2000 and 2005-2[^]

About handling in ASP.NET, you can pass on the desired return value from error handled in SQL or based on the return value you can handle it easily in UI level.


You can''t technically throw an exception from a stored proc. You candle them *in* the stored proc and return some sort of an error message back to the calling front end (through the SqlCommand.Parameters property).

If you don''t handle the exceptions in the stored proc itself, the framework will throw database-related exceptions that you can handle in the front end-code.

Where you handle the exception is dependent on what your code is doing. If you want to be able to rollback a transaction, you could do it in either the stored proc or the front-end, but again, that depends on how you''re performing transactions.


Hi
Inside the store procedure use beging catch to catch the exception as shown below which will return you the error number, errormessage, error line....

BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH

hope this will help you. :)


这篇关于处理存储过程异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆