1

I have MS SQL server with several jobs. Sometimes I and my colleagues change task. I want to export jobs, like at enter image description here automaticaly so that put in mercurial and store version of jobs. I would also like to have a similar opportunity for stored procedures in the database

2 Answers 2

2

Once you realize that SSMS is using SMO (Server Management Objects) to do the scripting, and that you can use SMO yourself directly through something like powershell, this sort of thing gets easy. For instance, here's scripting out all the jobs on your server (written in powershell):

pushd; import-module sqlps -disablenamechecking; popd; $s = new-object microsoft.sqlserver.management.smo.server 'yourServer'; $db = $s.Databases['yourDatabase']; foreach ($proc in $db.StoredProcedures | where {$_.IsSystemObject -eq $false}) { $proc.script(); } $a = $s.JobServer; foreach ($job in $a.Jobs) { $job.script(); } 

This is the least fancy version in that it dumps it straight to your screen. Want fancier? Create a ScriptingOptions object and pass that to the script() method. That should get you started.

4
  • Thank you so much for the tip. But your example on line $s = new-object sql.microsoft.sqlserver.managment.smo.server 'yourServer'; I gave error: New-Object : Cannot find type [sql.microsoft.sqlserver.managment.smo.server]: make sure the assembly containing this type is loaded. I changed the code a bit and got a working example. Commented Apr 24, 2015 at 9:25
  • 1
    Ack... Typo. Fixed. Commented Apr 24, 2015 at 11:40
  • It is the tragedy of a single character :-) I spent an hour on the alternative solution for this problem. Commented Apr 25, 2015 at 20:38
  • I found alternative solution for export stored procedure - sp_helptext 'stored_procedure_name' Commented Apr 27, 2015 at 14:02
0

In my case this example worked without error, thanks Ben Thul:

import-module sqlps -disablenamechecking; $jobs = Get-ChildItem sql\ServerName\default\jobserver\jobs foreach ($job in $jobs) { Write-host $job.Name #script(); $job.script()|out-file c:\out\jobs\$job.sql UTF8 } import-module sqlps -disablenamechecking; $StoredProcedures = Get-ChildItem sql\ServerName\default\Databases\DatabaseName\StoredProcedures foreach ($sp in $StoredProcedures) { $spname=$sp.Schema+"."+$sp.Name Write-host $sp.Name #script(); $sp.script()|out-file c:\out\sp\$spname.sql UTF8 } 
3
  • 1
    Homework for you: get it to write each job/procedure to its own file. Hint: the ScriptingOptions object has a FileName property on it. Since you're ultimately putting this in version control, I think you'll find one file per object a bit more manageable. But excellent work! Commented Apr 24, 2015 at 18:25
  • I did so - $job and $spname is variable. But I understood you about more simple use with script() and FileName. Commented Apr 25, 2015 at 20:34
  • Ah... so you did! I missed the dollar sign in front of the output file name. It seems that we're trading meaningful one-character differences. :) Commented Apr 26, 2015 at 17:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.