mssql procedure try catch
BEGIN
BEGIN TRY
-- do someting
END TRY
BEGIN CATCH
--DB查詢用
SELECT ERROR_NUMBER() AS ErrorNumber,
ERROR_MESSAGE() AS ErrorMessage,
ERROR_LINE() AS ErrorLine,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState
--系統拋回訊息用
DECLARE @ErrorMessage As NVARCHAR(1000) = N'錯誤代碼:' +CAST(ERROR_NUMBER() AS VARCHAR) + ';'
+N'錯誤程序名稱:'+ ISNULL(ERROR_PROCEDURE(),'')+ ';'
+N'錯誤行號:'+ CAST(ERROR_LINE() AS VARCHAR)+ ';'
+N'錯誤訊息:'+ ERROR_MESSAGE()
DECLARE @ErrorSeverity As Numeric = ERROR_SEVERITY()
DECLARE @ErrorState As Numeric = ERROR_STATE()
RAISERROR( @ErrorMessage, @ErrorSeverity, @ErrorState);--回傳錯誤資訊
END CATCH
END
留言
張貼留言