I have been quite a few comments on my Facebook page and here is one of the questions which instantly caught my attention.
“We have a legacy application and it has been a long time since we are using SQL Server. Recently we have upgraded to the latest version of SQL Server and we are updating our code as well.
Here is the question for you, there are plenty of places we have been using old style RAISEERROR code and now we want to convert it to use THROW. Would you please suggest a sample example for the same.”
Very interesting question. THROW was introduced in SQL Server 2012 to handle the error gracefully and return the error message. Let us see quickly two examples of SQL Server 2012 and earlier version.
Earlier Version of SQL Server
DECLARE @ErrorMessage NVARCHAR(2000), @ErrorSeverity INT
SELECT @ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY()
RAISERROR (@ErrorMessage, @ErrorSeverity, 1)
SQL Server 2012 and Latest Version
That’s it! We are done!
Reference: Pinal Dave (http://blog.SQLAuthority.com)