spork001
spork001

Reputation: 15

Returning Errors without a stored procedure

I need to know the number of rows in a certain table. If it is under 250 rows I need to return an error to the sql job forcing it to quit. The problem is it's not a stored procedure. It's sql code is running straight from the job step as a Transact-SQL script. Is this possible to return anything, or is there a better way to do this?

This is what I have: select case when (select cnt = count([col]) from db.dbo.table) < 250 THEN 1 ELSE 0 END

Upvotes: 0

Views: 91

Answers (1)

bobs
bobs

Reputation: 22184

You can use the RAISERROR command.

IF (SELECT COUNT([col] FROM db.dbo.table) < 250
    RAISERROR('My error message', 15, 1)

The severity level 15 is a level that will indicate to the job that the command failed.

Look here for more about the RAISERROR command.

Upvotes: 2

Related Questions