How to make sql quit job step fail

I have a sql job step

like this

Declare 
@Result varchar(255)

exec myprocedure
@Result = @Result output

What I want to do:
if @Result = 'Error', then mark the work as failed, how can I achieve this?

+3
source share
2 answers

Add this to the end of your script:

if @Result = 'Error'
    raiserror('The stored procedure returned an error',16,1)

And make sure that the “Advanced” tab of the step properties has the “on failure” parameter set to “End Job Report Failure”

+7
source

You can use try catch

Begin Try
   exec myprocedure
   @Result = @Result output
End Try

Begin Catch
   /*Do whatever you want here*/
End Catch
0
source

Source: https://habr.com/ru/post/1731527/


All Articles