Solved

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

Posted on 2011-09-29
3
388 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks, works perfectly!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now