I have inherited code that looks like this:
Stored Procedure UpdateSomeStuff
Update Table1 Set Col1=Para@1
IF @@Error > 0 Goto ERR
Update Table2 Set Col1=Para@2
IF @@Error > 0 Goto ERR
RETURN 0
ERR:
return -1;
This sp is called by ADO.NET in c# like this
try
{
myCommand.ExecuteNonQuery();
}
catch (System.Exception ex)
{
_log.Error(ex);
throw();
}
finally
{
if(myConnection!=null)
{
myConnection.Close();
}
}
I am just asking myself: Is the part IF @@Error > 0 Goto ERR good for anything? If an error happens the sp would return anyway and an exception would be caught in the calling method.
The calling code does not handle the return value 0 and -1. My plan would be to remove all Errorhandling in the Stored Procedure an have the same result. Am I right or is there something I missed?
My ‘2 cents’ you will kill the performance by removing if@error statements.
so either you implement try catch as mitch says or do not remove them.
Just think if there is error at first statement then it will not attempt to execute 2nd statement and so on when you have if@error statements.
and if you remove them , sql server is going to try next statement until it reaches the limit when it decides it can not go ahead with further processing.