Solved

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

Posted on 2011-09-29
3
402 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 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

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

Suggested Solutions

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article I will describe the Backup & Restore 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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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