December 8, 2006 at 7:41 am
Ok, I've been using RAISERROR for a while (I've even gotten past the misspelling of it - thanks MS). However, I want to understand it a little more if someone is willing. I am a .NET Programmer and I am used to code IMMEDIATELY returning when I throw an Exception. However, RAISERROR does NOT immediately return from a Stored Procedure.
For example, I have a Proc that validates the data upfront and if it's invalid, I use RAISERROR and then exit. What I want to understand here is why the RETURN statement does NOT immediately return!
Here's a snippet of my Proc:
If (@IsValid = 1) And (@SomeValue Is Null)
RaisError('The @SomeValue Parameter is required.', 16, 1) WITH NOWAIT
If (@@ERROR <> 0) RETURN(@@ERROR)
The example above DOES NOT RETURN even though the Error No = 50000. I know this becuase it hits my Transaction that follows that statement and attempts to keep going. However, the following example does return immediately.
If (@IsValie = 1) And (@SomeValue Is Null)
BEGIN
RaisError('The @LegacyName Parameter is required because the Application is ELS Sensitive.', 16, 1) WITH NOWAIT
RETURN(@@ERROR)
END
So why does the RETURN statement sometimes work, and sometimes no? Is it because of the BEGIN / END? I was trying NOT to litter my Procs with hundreds of BEGIN / ENDs but it looks like it's still required...
December 8, 2006 at 8:39 am
As what you wrote, @@ERROR only returns the error number of the last transaction. If you would like to be sure you had better include a piece of code something like
SET @errCode = @@ERROR
in a right place.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply