1

I am thinking of something like the CASE statement for RAISERROR, but I want the remaining code to be checked too, and not skip anything after encountering the first RAISERROR. Right now I have something like this.

enter image description here

Now, I want a way where I can RAISERROR at the very end and do it conditionally. So if there are two errors from three checks, both should be displayed to the user. Thank you.

3
  • Is this happening within a TRY / CATCH? Commented Apr 2, 2016 at 18:57
  • No there is no try/catch block. Maybe I am doing this under the wrong assumption that the code will stop executing anything after the first RAISERROR encounter, if that is at all wrong.
    – Ravi
    Commented Apr 3, 2016 at 4:52
  • Okay, so it seems from the answer by Martin Smith that I am entirely wrong. Good to know that user generated errors don't abort the whole batch.
    – Ravi
    Commented Apr 3, 2016 at 4:57

2 Answers 2

2

The code you have posted already does what you want as written.

Errors you raise yourself with RAISERROR do not abort the batch, not even in trigger context.

You will see that in the screenshot below SSMS manages to display all errors fine. Are you missing some context in your question?

enter image description here

0
2

One method is to build an error message string and issue a single RAISERROR conditionally after all validations are done.

DECLARE @ErrorMessage varchar(2047);
IF @ForeCastYearCheck = 'false'
    SET @ErrorMessage = @ForeCastYearCheckError;
IF @ForeCastQuarterCheck = 'false'
    SET @ErrorMessage = COALESCE(@ErrorMessage + ',' + @ForeCastQuarterCheckError, @ForeCastQuarterCheckError);
IF @ForeCastComboCheck = 'false'
    SET @ErrorMessage = COALESCE(@ErrorMessage + ',' + @ForeCastComboCheckError, @ForeCastComboCheckError);
IF @ErrorMessage IS NOT NULL
    RAISERROR(@ErrorMessage, 16, 1);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.