Jonathan Durda
Jonathan Durda

Reputation: 41

What determines success or failure of SQL Server Job?

I have a query that I want to run in a SQL Server job. I would like to have the query report success if the query returns 1 or more rows, and to return failure if the query does not reutrn and rows. How would I go about doing this?

Upvotes: 1

Views: 2246

Answers (2)

Dave.Gugg
Dave.Gugg

Reputation: 6771

I think you will have to do this within the query:

IF @@ROWCOUNT < 1
    BEGIN
        RAISERROR('No records returned',16,1)
        RETURN
    END

If no rows are returned, an error will occur and the job will have a failure status.

Upvotes: 4

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

Have a step that checks the count and calls RAISERROR or THROWs if there are no rows.

Upvotes: 0

Related Questions