Job failure based on value returned from script

  • I have a job that I need to execute a test before running.

    The test is basically to check the data in one DB against the data in another DB and evaluate the Mirror/Replication status. If my Count of a table in the source DB equals the count of the same table in the destination DB the step should succeed and the job should continue. If the counts are not the same the job should fail and the job should not continue.

    So basically the script boils down to

    @Total = @CountDB1 - @CountDB2

    if @Total is 0 then the next step should proceed, if it is not 0 the job should fail.

    I tried

    SET @@ERROR = @Total

    and I get "incorrect syntax near @@ERROR"

    How do I throw an error based on the results of the script, not just whether the script ran or not?

    Thanks.

  • RAISERROR( '*ERROR* put your comment here and it will show in the failure message', 16, -1, @@servername ) --WITH LOG

    if u uncomment the with log it will write ur message to the lag as well.

  • Thanks. That gave me exactly the results I was looking for.

  • Now I have 2 additional issues.

    The first is directly related to the above issue and answer. I built my job step to RAISERROR upon certain conditions, and to repeat that step every 30 minutes for 3 hours until it succeeded, or finally failed on the last attempt. On the initial pass the step failed and the error was raised and the step stopped and waited. Thirty minutes later the step tried again, the error was raised, but this time the job continued onto the next step. Why didn't the job go into the wait mode again?

    On a related issue the second is related to a query time out on a job step. I have a job step which runs a query against a linked server. The step shows an error of a Query time out, but continued on to the next step.

    In both instances the job the "On Failure" action is set to quit.

    Thanks

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

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