views:

51

answers:

1

I have a lengthy stored procedure in which I would like to do something like the following:

IF @SubPageDirectory IS NULL
BEGIN
    RAISERROR('@SubPageDirectory cannot be NULL', 10, 1)
    EXIT STORED PROCEDURE
END

Basically I wish to check whether my variable is NULL, and if it is, return an error message to my .NET Web Application, and stop running the query at that stage. How can I achieve this?

+4  A: 
IF @SubPageDirectory IS NULL 
BEGIN 
    RAISERROR('@SubPageDirectory cannot be NULL', 16, 1) --change to > 10
    RETURN --exit now
END 
gbn
do you need the return, I thought raiserror would quit.
DaveShaw
@DaveShaw: no, it carries on executing
gbn
RETURN seems to return without any errors, as in, it won't show the user the errors, it will just continue.RAISERROR on its own continues reading through the stored procedure as it will then bring up another error.
Curt
+1, however, I'd make it `RETURN n` where `n` is an integer. I make negative return values warning messages (invalid user input, etc) and positive return values fatal errors (insert failure, etc). The calling application can determine how to handle the message (hard stop and/or just display the message) based on the return value being positive/negative.
KM
@KM: http://stackoverflow.com/questions/761898/
gbn
I've increased the severity level to 16, and now my asp.net web app shows a server error page, which is what I was hoping for. Return has also prevented the query from continuing.I had previously overlooked the severity level increase comment.
Curt
Sorry,meant to add severity level 16 means "user defined". 10 is a warning which is usually ignored
gbn
Thanks @gbn, much appreciated!
Curt