Sunday, 13 July 2014

THROW In SQL Server 2012


THROW is a newly introduced key word in SQL Server 2012 which used to throw the error from catch block. It’s a new enhancement in Error Handling in SQL Server.


It used as the same way as RAISEERROR() function in SQL Server 2008.

Example with Raiseerror:


BEGIN TRY
       SELECT 1/0
END TRY
BEGIN CATCH
DECLARE
   @ErMessage NVARCHAR(2048),
   @ErSeverity INT,
   @ErState INT

 SELECT
   @ErMessage = ERROR_MESSAGE(),
   @ErSeverity = ERROR_SEVERITY(),
   @ErState = ERROR_STATE()

 RAISERROR (@ErMessage,
             @ErSeverity,
             @ErState )
END CATCH

(0 row(s) affected)
Msg 50000, Level 16, State 1, Line 15
Divide by zero error encountered.

Example with THROW:


BEGIN TRY
       SELECT 1/0
END TRY
BEGIN CATCH
       THROW
END CATCH


(0 row(s) affected)
Msg 8134, Level 16, State 1, Line 2
Divide by zero error encountered.

No comments:

Post a Comment