篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MS SQL Try Catch相关的知识,希望对你有一定的参考价值。
BEGIN TRY;
SET NOCOUNT ON;
SELECT 'Some sql statement ...';
RAISERROR('Raise a custom error', 16, 1) ;
Select 'This code wont execute';
END TRY
BEGIN CATCH
SELECT
ERROR_MESSAGE() AS [Message],
ERROR_PROCEDURE() AS [Procedure],
ERROR_LINE() AS Line,
ERROR_NUMBER() AS Number,
ERROR_SEVERITY() AS Severity,
ERROR_STATE() AS State;
END CATCH;
SELECT 'The end';
-- Traditional error handling
DECLARE @ErrorCode INT;
PRINT 'Some error prone sql statement.'
SET @ErrorCode= @@ERROR;
IF @ErrorCode <> 0
BEGIN
-- ERROR HANDLING CODE GOES HERE
PRINT @ErrorCode
END