Updated on Kisan Patel
This tutorial will show you how to handle errors in store procedure in SQL Server?
To handle error in stored procedure of SQL Server, we use BEGING TRY
– END TRY
and BEGIN CATCH
and EDN CATCH
statements.
Normally, if we perform any invalid SQL operations, it throws error like in below case−
SELECT 'csharpcode'/0
Here, it is not possible divide any string by 0, so SQL Server is trying to convert it to integer however ‘csharpcode’ couldn’t get converted and it throws error.
Instead of raw error coming in, we want to ignore this error, we can wrap the above statements into
BEGIN and END TRY
like this−
BEGIN TRY SELECT 'csharpcode'/1 END TRY BEGIN CATCH END CATCH
Here, the SELECT
statement will throw error that is being caught in the BEGIN CATCH
block, but this block is not doing anything but suppressing the error. So the output is showing nothing without any error.