Solved

How to "force" an error in a job step in SQL Server

Posted on 2011-09-29
3
400 Views
Last Modified: 2012-05-12
Hello,

I have a SQL Server job with several steps.  On the first step I need the logic to:

  1--check for a table to have zero rows
  2--if not zero rows, force an error so that the step will fail and I can set up the step to try again in 15 minutes.

What I have not been able to do is "force" the error correctly.  To see what I mean, I put the following code in the first step of my job, but when I run it, regardless of whether the "test" table has zero rows or not, the job shows as having run successfully.  Any suggestions?

Thanks

if 0<>(select COUNT(*) from test)
	BEGIN
		RAISERROR (N'There are rows in table' , --message text
					10, --severity
					1, --state
					N'number',	--first argument
					5); --second argument
				-- The message text returned is:  This is message number 5.
	END

Open in new window

0
Comment
Question by:hpsuser
  • 2
3 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 36816733
if 0<>(select COUNT(*) from test)
        BEGIN
                 select 1 from NonExistantTableName where 1=0
        END
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 36816750
Or, change the error level in the raiserror statement:

RAISERROR (N'There are rows in table' , --message text
                                        11, --severity
                                        1, --state
                                        N'number',      --first argument
                                        5); --second argument
0
 

Author Closing Comment

by:hpsuser
ID: 36816969
thanks, works perfectly!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TOOLS - convert T-SQL TO PL/SQL 3 54
Linked Server Issue with SQL2012 3 28
Add a step to a system backup job 6 19
SQL Recursion schedule 13 16
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question