TSQL with @@ error and is it possible to replace them?

I inherited code that looks like this:

Saved 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 just ask myself: is there a part of IF @@ Error> 0 Going to ERR is good for anything? If an error occurs, sp will be returned anyway, and an exception will be detected in the calling method.

The calling code does not handle the return value of 0 and -1. My plan would be to remove all Error Handlers in the Stored Procedure and have the same result. Am I right or is there something I missed?

+5
source share
2 answers

"2 " , , @. try catch, mitch, .  , , 2- .., , @.

, sql , , , .

+1

(SQL Server 2005) TRY..CATCH:

BEGIN TRY
    -- Perform operations here
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_MESSAGE() AS ErrorMessage;
END CATCH

: SQL 2005

+7

All Articles