Solved

T sql script to alter SQL Server agent job if exists

Posted on 2014-01-12
6
2,147 Views
Last Modified: 2014-01-27
Hi Experts,

  Could you please help me with T-Sql script to alter SQL Server agent job.  If the job exists, then I need to alter the job script ,to  add more steps and also edit the existing steps.  
I don't want to add it manually through wizard as we want to run the T-Sql scripts only (as we don't have access to production environments)

I need a sample Alter script . Your help is greatly appreciated in this regard.

Please Note : I don't want to drop and create the existing job in production as I would loose history of the job.

Regards,

Sreekanth.
0
Comment
Question by:n_srikanth4
6 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39775430
I would go through the steps with the wizard, just don't finish and instead save the SQL Scripts off so that you can optionally edit them and have them ultimately run in Production.

Let me know if you need help with that.
0
 

Author Comment

by:n_srikanth4
ID: 39775464
Hi Savant ,

     I could not go through the wizard , It will just add the step , but not generate any script .  Secondly , the  jobs are already created in Dev server , so i cannot undo the wizard.  So could you please walk through an example sample script please.

Regards,

Srk.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39775503
i would drop the step in development and start again using the wizard, but this time save the SQL scripts, before you finish.

P.S.  The name is Anthony (you will see it as it is just below where it says "Your Comment" and after "by:")
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 38

Accepted Solution

by:
Jim P. earned 500 total points
ID: 39775517
My suggestion is that you create your own "history" table that you write the results to as the steps run. Basically as the first part of that particular step you insert the step name, time, server, db and other details at the beginning into the history table. Then at the end of the step you have a similar insert statement.

That way you aren't dependent on the job history table.

Then adding steps or replacing the script you don't lose the history. And if you build out the script for more stuff, you can just hand it off to the production team and move on.
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39781034
Hi,

If you examine the script of the existing job, that script should have all the clues you need. That is, the job steps are added to the job one at a time. Of course you'll have to add logic that finds how many existing steps there are, and appends them. And don't forget to modify existing last step to go on to the next on success.

HTH
  David
0
 

Author Closing Comment

by:n_srikanth4
ID: 39813554
good
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now