?
Solved

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

Posted on 2011-09-29
3
Medium Priority
?
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

741 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