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

Jack picture Jack · Nov 11, 2016 · Viewed 12.9k times · Source

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?

Answer

Matt picture Matt · Nov 11, 2016

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