8

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?

TT.
  • 15,774
  • 6
  • 47
  • 88
Jack
  • 281
  • 1
  • 3
  • 17

3 Answers3

6

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
Matt
  • 13,833
  • 2
  • 16
  • 28
1

You can use also "Select 1/0" this will parse but it will giving error while execution

Sunil Patil
  • 829
  • 8
  • 14
0

I feel it is bad practice to fail a job-step to implement application logic. Better to use IF (some condition) THEN (some job-step action) ELSE (print an explanation why there was no job-step action). And reserve failures for operational issues.

RichardBSmith
  • 11
  • 1
  • 3