sql server 2005 - IF, RAISERROR & RETURN in Stored Procedure -
i have stored procedure, proc
, receives parameters. if 1 of them, @id
, not null, given stored procedure, proc_a
, must executed. otherwise, proc_b
must executed. problem both of them may issue raiserror
, want propagate through call stack displayed @ client application. however, raiserror
won't stop rest of proc
stored procedure should, and, since using if
clause, checking if ( @@error <> 0 ) return
isn't option either. choice seems using try...catch
block wrap if
clause , rethrow raiserror
within catch
block, awkwards because have cache error_message()
, error_severity()
, error_state()
, use raiserror
once again.
isn't there more elegant way?
just use try - catch
block , echo original error, isn't hard do:
begin try --used in catch block echo error declare @errormessage nvarchar(400), @errornumber int, @errorseverity int, @errorstate int, @errorline int --your stuff here end try begin catch --your error code/logging here --will echo complete original error message select @errormessage = n'error %d, line %d, message: '+error_message(),@errornumber = error_number(),@errorseverity = error_severity(),@errorstate = error_state(),@errorline = error_line() raiserror (@errormessage, @errorseverity, @errorstate, @errornumber,@errorline) end catch
also, best practice have entire procedure in try - catch
, , not external procedure calls.
Comments
Post a Comment