after begin ... raise error'

  • We have a stored procedure. In the code I subtract part of it as below:

    IF @ProcessNm = 'BatchAssignment' AND @ProcessType = 'Post'

    BEGIN

    SELECT @existingProcessID = ProcessID

    FROM dbo.appProcess

    WHERE ProcessNm = @ProcessNm

    AND ProcessType = @ProcessType

    AND SchoolYear = @SchoolYear

    AND ProcessStatus = 'Complete'

    IF @existingProcessID IS NOT NULL

    BEGIN

    RAISERROR('Complete %s %s process (ProcessID = %d) exists in appProcess table for the %d SchoolYear.',11,0,@ProcessNm,@ProcessType,@existingProcessID,@SchoolYear)

    END

    END

    INSERT INTO [dbo].[appProcess] (

    [SchoolYear],

    [ProcessType],

    [ProcessNm],

    [ProcessStartDt],

    [ProcessEndDt],

    [ProcessStatus],

    ...

    My question is : you can see there is a begin ..raise error ...end, if we have the error happened, will the sproc exit, and does the insert part still taken place or not?

    Thanks

  • RAISERROR does not abort the batch. The process will continue after a RAISERROR unless the RAISERROR is withing a TRY...CATCH block. In a TRY...CATCH block RAISERROR sends it to the CATCH block. You need to explicitly ROLLBACK and RETURN in order to stop processing after a RAISERROR outside a TRY...CATCH.

    You could also use a severity level high enough to terminate the batch/connection.

  • Thanks, it helps a lot

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply