Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a SQL Server agent job step failure on purpose

I have a process and the first step is to check status of other process. If the other process is done I would want to run the rest steps and if not I would want to quit the job.

I have a table that looks into the status and if all done it will mark as 'done'. So what should I put in the first step so that when the status is not 'done', it makes step 1 fail?

like image 721
Jack Avatar asked Nov 11 '16 15:11

Jack


1 Answers

There are lots of ways you can force sql-server to throw (or raise) an error but I would recommend using THROW for SQL-Server 2012 + or RAISERROR below that as that is your actual intention. So you could do something like:

IF EXISTS(SELECT * FROM StatusTable WHERE status <> 'done')
BEGIN
    ;THROW 51000, 'Process Is Not Finished', 1
END
like image 198
Matt Avatar answered Sep 19 '22 00:09

Matt