Solved

running sql agent job step based on value returned by a query

Posted on 2015-01-07
5
148 Views
Last Modified: 2015-01-21
I am creating a sql agent job in MS SQL server 2008 R2.
First step runs a query to get validation status from the database. I want to run the second step only if validation_status = 1, or quit the job returning failure if validation_status <>1
One way of accomplishing this is to create a new job with the second step and use an if statement in step1 to run the second job.
Is there a better way where I can I accomplish this in the same job, to run the second step based on the outcome of query on first step?

Thank you for your help.
0
Comment
Question by:patd1
[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
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40535919
You can set the value of  "Process Exit code of a successful command " for the fist step, by default it is zero.
You may need to alter the step to return a different value in case it is not successful using a return statement.
0
 

Author Comment

by:patd1
ID: 40537940
Aneesh, thanks for answering. Can you please give me an example for how to set Process Exit Code. Google didnt help much.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40538030
Goto job properties -> double click on step1 of the your job, just under the "Run As " selection box, you can see Process Exit Code text box
0
 

Accepted Solution

by:
patd1 earned 0 total points
ID: 40538173
I used @raise_error and raised error if validation_status <> 1

This works for me.

Thank you for your help.
0
 

Author Closing Comment

by:patd1
ID: 40561569
setting @raise error worked.
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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

627 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