|
sql
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Can I script out SQL Server jobs programmatically?Does anyone know how to script out a complete job definition (job creation
script) from a T-SQL query? In SQL Server Managment Studio, you can right click a job and script out a complete definition. I just want to call this same functionality from code rather than the UI. I am using SQL Server 2005. I think it would be pretty complex, but you can always try to mimic what
Management Studio does, by watching it in profiler as you script a job... Show quoteHide quote On 6/22/09 3:36 PM, in article 97feae9955a3d@uwe, "josephs" <u52758@uwe> wrote: > Does anyone know how to script out a complete job definition (job creation > script) from a T-SQL query? In SQL Server Managment Studio, you can right > click a job and script out a complete definition. I just want to call this > same functionality from code rather than the UI. I am using SQL Server 2005. > I have done that, but and it tells me which tables to look at, but does not
give me a nice sproc to call or anything like that. I am hoping to avoid constructing the job definition by hand from several different tables. That could be very time consuming. > a nice sproc to call Sorry, but there is no such thing; I was just giving you a starting point.How different are your jobs that you couldn't build a script once and use it as a template with variables to fill in? You could do this quite easily with PowerShell or .NET... The bonus with this is that you could use it both to create jobs initially and then to "copy" them later... A T-SQL is not the way to do this. Use SMO instead. The Job class has a Script
method, and that's all you need to do. Linchi Show quoteHide quote "josephs" wrote: > Does anyone know how to script out a complete job definition (job creation > script) from a T-SQL query? In SQL Server Managment Studio, you can right > click a job and script out a complete definition. I just want to call this > same functionality from code rather than the UI. I am using SQL Server 2005. > > I have delved into the smo and the script method a little using a clr, and
the implementation looks a bit messy. I believe I would have to import my assembly and the necessary smo assemblies (which can change with each new version of Sql Server) as "unsafe". This is certainly doable, but less than ideal. I was hoping for something slightly less messy. Why do you need to do it in CLR or inside SQL Server at all? It's much
cleaner to do this in a client program. Linchi Show quoteHide quote "josephs" wrote: > I have delved into the smo and the script method a little using a clr, and > the implementation looks a bit messy. I believe I would have to import my > assembly and the necessary smo assemblies (which can change with each new > version of Sql Server) as "unsafe". This is certainly doable, but less than > ideal. I was hoping for something slightly less messy. > > Linchi, thank you for pulling me out of my daze!!! I'm not sure why I was so
set on doing this in SQL Server. After reading your tidbit of wisdom I used the SMO in my client program and voila; it works beautifully and simply (less than 5 lines of code). Thank you everyone who contributed to this post. I now have exactly what I was looking for. How about showing us your work. I've been looking for this too and I know
nothing about SMO. I'd appreciate seeing what you did. I could probably use it to meet my needs as well. Thanks! josephs wrote: Show quoteHide quote >Linchi, thank you for pulling me out of my daze!!! I'm not sure why I was so >set on doing this in SQL Server. After reading your tidbit of wisdom I used >the SMO in my client program and voila; it works beautifully and simply (less >than 5 lines of code). > >Thank you everyone who contributed to this post. I now have exactly what I >was looking for. Here's a simple C# code.
// Begin using System; using System.Collections; using System.Collections.Specialized; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Smo.Agent; using Microsoft.SqlServer.Management.Common; public class Junk { public static void Main(string[] args) { Server srv = new Server("MySQLInstance"); StringCollection strs = srv.JobServer.Jobs["Job 3"].Script(); foreach (string str in strs) Console.WriteLine("{0}", str); } } // class Junk // End Copy the code between //Begin and //End to a text file named junk.cs. And copy the following line to a file named csc_junk.bat (a single line with no text wrap): csc junk.cs /reference:"c:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\Microsoft.SqlServer.Smo.dll","c:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\Microsoft.SqlServer.SqlEnum.dll","c:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\Microsoft.SqlServer.SmoEnum.dll","c:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll" Put both files in the same directory (say, c:\junk). Assume you have .NET Framework SDK 2.0 installed. Open a command prompt with .NEt SDK Command prompt so that you have the environment variables correctly set, and type the following to compile the C# code: cmd>csc_junk.bat Run junk.exe to script out a job named "Job 3" on a SQL instance named MySQLInstance: cmd>junk.exe Linchi Show quoteHide quote "soccerhawg" wrote: > How about showing us your work. I've been looking for this too and I know > nothing about SMO. I'd appreciate seeing what you did. I could probably use > it to meet my needs as well. > > Thanks! > > josephs wrote: > >Linchi, thank you for pulling me out of my daze!!! I'm not sure why I was so > >set on doing this in SQL Server. After reading your tidbit of wisdom I used > >the SMO in my client program and voila; it works beautifully and simply (less > >than 5 lines of code). > > > >Thank you everyone who contributed to this post. I now have exactly what I > >was looking for. > > Thank you. Since I'm completely new to all of this, I have one more question.
Using your code as an example, is it easy enough to modify this code to script out all jobs on the SQL instance specified? Thanks. Linchi Shea wrote: Show quoteHide quote >Here's a simple C# code. > >// Begin >using System; >using System.Collections; >using System.Collections.Specialized; >using Microsoft.SqlServer.Management.Smo; >using Microsoft.SqlServer.Management.Smo.Agent; >using Microsoft.SqlServer.Management.Common; > >public class Junk >{ > public static void Main(string[] args) > { > Server srv = new Server("MySQLInstance"); > StringCollection strs = srv.JobServer.Jobs["Job 3"].Script(); > > foreach (string str in strs) > Console.WriteLine("{0}", str); > > } >} // class Junk > >// End > >Copy the code between //Begin and //End to a text file named junk.cs. And >copy the following line to a file named csc_junk.bat (a single line with no >text wrap): > >csc junk.cs /reference:"c:\Program Files\Microsoft SQL >Server\90\SDK\Assemblies\Microsoft.SqlServer.Smo.dll","c:\Program >Files\Microsoft SQL >Server\90\SDK\Assemblies\Microsoft.SqlServer.SqlEnum.dll","c:\Program >Files\Microsoft SQL >Server\90\SDK\Assemblies\Microsoft.SqlServer.SmoEnum.dll","c:\Program >Files\Microsoft SQL >Server\90\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll" > >Put both files in the same directory (say, c:\junk). Assume you have .NET >Framework SDK 2.0 installed. Open a command prompt with .NEt SDK Command >prompt so that you have the environment variables correctly set, and type the >following to compile the C# code: > >cmd>csc_junk.bat > >Run junk.exe to script out a job named "Job 3" on a SQL instance named >MySQLInstance: > >cmd>junk.exe > >Linchi > >> How about showing us your work. I've been looking for this too and I know >> nothing about SMO. I'd appreciate seeing what you did. I could probably use >[quoted text clipped - 9 lines] >> >Thank you everyone who contributed to this post. I now have exactly what I >> >was looking for. Sure and the change is simple. Just loop through the jobs. Here is the code:
using System; using System.Collections; using System.Collections.Specialized; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Smo.Agent; using Microsoft.SqlServer.Management.Common; public class Junk { public static void Main(string[] args) { Server srv = new Server("LINCHIS-LT1"); foreach (Job job in srv.JobServer.Jobs) { Console.WriteLine("-- Script for Job = {0}", job.Name); foreach (string str in job.Script()) Console.WriteLine("{0}", str); } } } // class Junk Linchi Show quoteHide quote "soccerhawg via SQLMonster.com" wrote: > Thank you. Since I'm completely new to all of this, I have one more question. > Using your code as an example, is it easy enough to modify this code to > script out all jobs on the SQL instance specified? > > Thanks. > > Linchi Shea wrote: > >Here's a simple C# code. > > > >// Begin > >using System; > >using System.Collections; > >using System.Collections.Specialized; > >using Microsoft.SqlServer.Management.Smo; > >using Microsoft.SqlServer.Management.Smo.Agent; > >using Microsoft.SqlServer.Management.Common; > > > >public class Junk > >{ > > public static void Main(string[] args) > > { > > Server srv = new Server("MySQLInstance"); > > StringCollection strs = srv.JobServer.Jobs["Job 3"].Script(); > > > > foreach (string str in strs) > > Console.WriteLine("{0}", str); > > > > } > >} // class Junk > > > >// End > > > >Copy the code between //Begin and //End to a text file named junk.cs. And > >copy the following line to a file named csc_junk.bat (a single line with no > >text wrap): > > > >csc junk.cs /reference:"c:\Program Files\Microsoft SQL > >Server\90\SDK\Assemblies\Microsoft.SqlServer.Smo.dll","c:\Program > >Files\Microsoft SQL > >Server\90\SDK\Assemblies\Microsoft.SqlServer.SqlEnum.dll","c:\Program > >Files\Microsoft SQL > >Server\90\SDK\Assemblies\Microsoft.SqlServer.SmoEnum.dll","c:\Program > >Files\Microsoft SQL > >Server\90\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll" > > > >Put both files in the same directory (say, c:\junk). Assume you have .NET > >Framework SDK 2.0 installed. Open a command prompt with .NEt SDK Command > >prompt so that you have the environment variables correctly set, and type the > >following to compile the C# code: > > > >cmd>csc_junk.bat > > > >Run junk.exe to script out a job named "Job 3" on a SQL instance named > >MySQLInstance: > > > >cmd>junk.exe > > > >Linchi > > > >> How about showing us your work. I've been looking for this too and I know > >> nothing about SMO. I'd appreciate seeing what you did. I could probably use > >[quoted text clipped - 9 lines] > >> >Thank you everyone who contributed to this post. I now have exactly what I > >> >was looking for. > > -- > Message posted via SQLMonster.com > http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200906/1 > > Once again, thank you. With your help I was able to get the job working and
figure out how to tailor it to my specific needs. It works like a charm! Linchi Shea wrote: Show quoteHide quote >Sure and the change is simple. Just loop through the jobs. Here is the code: > >using System; >using System.Collections; >using System.Collections.Specialized; >using Microsoft.SqlServer.Management.Smo; >using Microsoft.SqlServer.Management.Smo.Agent; >using Microsoft.SqlServer.Management.Common; > >public class Junk >{ > public static void Main(string[] args) > { > Server srv = new Server("LINCHIS-LT1"); > > foreach (Job job in srv.JobServer.Jobs) > { > Console.WriteLine("-- Script for Job = {0}", job.Name); > foreach (string str in job.Script()) > Console.WriteLine("{0}", str); > } > > } >} // class Junk > >Linchi > >> Thank you. Since I'm completely new to all of this, I have one more question. >> Using your code as an example, is it easy enough to modify this code to >[quoted text clipped - 59 lines] >> >> >Thank you everyone who contributed to this post. I now have exactly what I >> >> >was looking for.
http://www.sqlservercentral.com/Forums/Topic647768-146-1.aspx
Show quote Hide quote "josephs" <u52758@uwe> wrote in message news:97feae9955a3d@uwe...
> Does anyone know how to script out a complete job definition (job creation > script) from a T-SQL query? In SQL Server Managment Studio, you can right > click a job and script out a complete definition. I just want to call this > same functionality from code rather than the UI. I am using SQL Server > 2005. >
Other interesting topics
Sql transaction log size because of reindexing
User login date change date from Mon dd yyyy Backup and restore sql server 2005 Query is much slower in 2008 than in 2005 SQL Server 2005 database monitoring c2 files/sysxmitqueue Identifying Memory Pressures Jobs cannot running SQL Server 2005 Restore DB Name **INCOMPLETE** |
|||||||||||||||||||||||