希望确保在我的存储过程中正确捕获错误
Looking to be sure that errors get caught properly in my stored procedure
我想看看我是否能够在此存储过程中正确捕获我的错误:
ALTER PROCEDURE [dbo].[sp_UpdateText]
(@aID AS INT,
@CompanyID AS INT,
@CompanyName AS VARCHAR(MAX))
AS
BEGIN
DECLARE @Result VARCHAR(MAX)
BEGIN TRY
SET @Result = (SELECT dbo.[udf_StripHTMLTags](@CompanyName)) -- UDF function that strips HTML tags off my text field
BEGIN TRANSACTION
UPDATE __TestTable1
SET CompanyName = @Result
WHERE aid = @aid AND CompanyID = @CompanyID
COMMIT TRANSACTION
END TRY
BEGIN CATCH
DECLARE @ErrorNumber INT = ERROR_NUMBER();
DECLARE @ErrorLine INT = ERROR_LINE();
PRINT 'ERROR NUMBER: ' + CAST(@ErrorNumber as Varchar(10));
PRINT 'ERROR LINE: ' + CAST (@ErrorLine as Varchar(10));
END CATCH
END
Go
我基本上希望这些 BEGIN TRY BEGIN CATCH 错误捕获方法能够成功捕获错误,如果出现的话?有什么想法吗?
你应该看看 Erland's Guide to Error Handling
本包容性指南的建议是将您的 CATCH
至少更改为
BEGIN CATCH
IF @@trancount > 0 ROLLBACK TRANSACTION --roll back the tran
DECLARE @msg nvarchar(2048) = error_message() --error message is usually more helpful
DECLARE @ErrorNumber INT = ERROR_NUMBER();
DECLARE @ErrorLine INT = ERROR_LINE();
RAISERROR(@msg,16,1) --RAISE the error
RETURN 55555 --return a non-zero to application as non-success
END CATCH
里面还有很多内容值得一读。
我差点忘了,SET XACT_ABORT, NOCOUNT ON
在你的程序的顶部。
When you activate XACT_ABORT ON, almost all errors have the same
effect: any open transaction is rolled back and execution is aborted.
There are a few exceptions of which the most prominent is the
RAISERROR statement.
请注意,“打印”错误不会将其存储或记录在任何地方,例如 SQL 服务器错误日志,因此您根本不会“捕获”它。
我想看看我是否能够在此存储过程中正确捕获我的错误:
ALTER PROCEDURE [dbo].[sp_UpdateText]
(@aID AS INT,
@CompanyID AS INT,
@CompanyName AS VARCHAR(MAX))
AS
BEGIN
DECLARE @Result VARCHAR(MAX)
BEGIN TRY
SET @Result = (SELECT dbo.[udf_StripHTMLTags](@CompanyName)) -- UDF function that strips HTML tags off my text field
BEGIN TRANSACTION
UPDATE __TestTable1
SET CompanyName = @Result
WHERE aid = @aid AND CompanyID = @CompanyID
COMMIT TRANSACTION
END TRY
BEGIN CATCH
DECLARE @ErrorNumber INT = ERROR_NUMBER();
DECLARE @ErrorLine INT = ERROR_LINE();
PRINT 'ERROR NUMBER: ' + CAST(@ErrorNumber as Varchar(10));
PRINT 'ERROR LINE: ' + CAST (@ErrorLine as Varchar(10));
END CATCH
END
Go
我基本上希望这些 BEGIN TRY BEGIN CATCH 错误捕获方法能够成功捕获错误,如果出现的话?有什么想法吗?
你应该看看 Erland's Guide to Error Handling
本包容性指南的建议是将您的 CATCH
至少更改为
BEGIN CATCH
IF @@trancount > 0 ROLLBACK TRANSACTION --roll back the tran
DECLARE @msg nvarchar(2048) = error_message() --error message is usually more helpful
DECLARE @ErrorNumber INT = ERROR_NUMBER();
DECLARE @ErrorLine INT = ERROR_LINE();
RAISERROR(@msg,16,1) --RAISE the error
RETURN 55555 --return a non-zero to application as non-success
END CATCH
里面还有很多内容值得一读。
我差点忘了,SET XACT_ABORT, NOCOUNT ON
在你的程序的顶部。
When you activate XACT_ABORT ON, almost all errors have the same effect: any open transaction is rolled back and execution is aborted. There are a few exceptions of which the most prominent is the RAISERROR statement.
请注意,“打印”错误不会将其存储或记录在任何地方,例如 SQL 服务器错误日志,因此您根本不会“捕获”它。