Wednesday, March 7, 2012

script all MSDB jobs in TSQL

I know of a way to script all SQL Agent jobs using EM. But i want to script
all my jobs on a daily basis and copy that script file to another server . I
know how i can do the daily part of it which would be scheduling it again
but whats the TSQL or command I need to run within the job step .. Thanks
You can use Distributed Management Objects (DMO), not T-SQL. Check the
article at
http://www.winnetmag.com/SQLServer/A...90/38790.html.
Dejan Sarka, SQL Server MVP
Associate Mentor
Solid Quality Learning
More than just Training
www.SolidQualityLearning.com
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:uEV1iN8iEHA.3988@.tk2msftngp13.phx.gbl...
> I know of a way to script all SQL Agent jobs using EM. But i want to
script
> all my jobs on a daily basis and copy that script file to another server .
I
> know how i can do the daily part of it which would be scheduling it again
> but whats the TSQL or command I need to run within the job step .. Thanks
>
|||You can set up a job that uses an ActiveX script type job step containing
the following
Set oSQL = CreateObject("SQLDMO.SQLServer")
oSQL.Name = "(local)"
oSQL.LoginSecure = True
oSQL.LoginTimeout = 10
oSQL.Connect
Set oJobs = oSQL.JobServer.Jobs
oJobs.Script 4,"c:\jobs.sql",2048
oSQL.DisConnect
Set oSQL = Nothing
This will script all your jobs to c:\jobs.sql. For a named instance, change
the oSQL.Name
HTH
Jasper Smith (SQL Server MVP)
http://www.sqldbatips.com
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:uEV1iN8iEHA.3988@.tk2msftngp13.phx.gbl...
>I know of a way to script all SQL Agent jobs using EM. But i want to script
> all my jobs on a daily basis and copy that script file to another server .
> I
> know how i can do the daily part of it which would be scheduling it again
> but whats the TSQL or command I need to run within the job step .. Thanks
>
|||one thing to watch out for is that the job owner (@.owner_login_name) will be
scripted out too. If login doesn't exist or in a different domain / env this
may cause you problems.
Could parse the Scripted text (can use the Script object just to return text
as opposed to file) and do a replace operation on the @.owner_login_name =
'xx' and replace with @.owner_login_name = null, so that sp_add_job will
create the job with owner of the user executing the script.
Then write the corrected text to File.
cheers,
Andy
"Hassan" wrote:

> I know of a way to script all SQL Agent jobs using EM. But i want to script
> all my jobs on a daily basis and copy that script file to another server . I
> know how i can do the daily part of it which would be scheduling it again
> but whats the TSQL or command I need to run within the job step .. Thanks
>
>
|||try attached
rename to .vbs
usage CSCRIPT ScriptAllJobs.vbs <Server> <FileName> <owner>
Server is Source Server where you want to script jobs from
FileName is output file for the script
owner is the new owner of the jobs. either do NULL (which will give make the
owner the user who runs the create script) or 'somelogin' ie the NT Login
that should be the owner ie 'Domainx\Jobowner' - with the quotes.
hope that makes sense
I had to do a bit of hacking cos a) the owner is hard coded and b) the
servername is hard coded (ie replace this with @.@.ServerName). Not too happy
about this, I think I might have missed a DMO Option somewhere
ai
Andy.
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:uEV1iN8iEHA.3988@.tk2msftngp13.phx.gbl...
> I know of a way to script all SQL Agent jobs using EM. But i want to
script
> all my jobs on a daily basis and copy that script file to another server .
I
> know how i can do the daily part of it which would be scheduling it again
> but whats the TSQL or command I need to run within the job step .. Thanks
>
begin 666 ScriptAllJobs.txt
M)R!38W)I<'1!;&Q*;V)S+G-Q;" -"B<@.4V-R:7!T<R!A;&P@.:F]B<R!O;B!A
M(&=I=F5N(%-13"!397)V97(@.86YD(')E<&QA8V5S('1H92!H87)D8V]D960@.
M<F5F97)E;F-E<R!T;R!A;&QO=R!T;R!B92 -"B<@.8V]P:65D('1O(&%N;W1H
M97(@.<V5R=F5R#0HG($%N9'D@.0F%L;" R-"\X+S(P,#0)=C$N, E"87-E(%9E
M<G-I;VX-"@.T*3W!T:6]N($5X<&QI8VET#0H)( T*)R!344PM1$U/(&-O;G-T
M86YT<PT*0V]N<W0@.4U%,1$U/4V-R:7!T7T1R;W!S(#T@.,0T*#0I#;VYS="!3
M44Q$34]38W)I<'1?4')I;6%R>4]B:F5C=" ](#0@.#0I#;VYS="!344Q$34]3
M8W)I<'1?5&]&:6QE3VYL>2 ](#8T#0I#;VYS="!344Q$34]38W)I<'1?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S92X-"B<@.4V\@.=V4@.8V%N(')E<&QA8V4@.:&%R9&-O9&5D
M(')E9F5R;F-E<RX@.#0I&=6YC=&EO;B!297!L86-E2&%R9$-O9&5D4F5F("A3
M44Q3=')I;F<L(%-E87)C:%-T<FEN9RP@.4F5P;&%C95-T<FEN9RD-"@.E$:6T@.
M3F5W4W1R:6YG#0H)1&EM(%-E87)C:%-T<FEN9U!O<PT*"41I;2!396-O;F11
M=6]T95!O<PT*#0H)1&EM($QO9VEN3F%M90T*"41I;2!344Q3=')I; F=,96X-
M"@.T*"5-13%-T<FEN9TQE;B ]($QE;BA344Q3=')I;F<I#0H-"@.DG(&=E="!T
M:&4@.;&]C871I;VX@.;V8@.=&AE(&]W;F5R7VQO9VEN7VYA;64@.8VAA<@.T*"5-E
M87)C:%-T<FEN9U!O<R ]($EN4W1R*#$L(%-13%-T<FEN9RP@.4V5A<F-H4W1R
M:6YG+" P*0T*#0H))R!G970@.82!3=')I;F<@.+"!S=&%R=&EN9R!W:71H( $XG
M;&]G:6X@.;F%M92<-"@.E,;V=I;DYA;64@./2!-:60H4U%,4W1R:6YG+"!396%R
M8VA3=')I;F=0;W,@.*R!,14XH4V5A<F-H4W1R:6YG*2 K(#,L(%-13%-T<FEN
M9TQE;B M(%-E87)C:%-T<FEN9U!O<RD-"@.D-"@.E396-O;F11=6]T95!O<R ]
M($EN4W1R*#,L($QO9VEN3F%M92P@.(B<B+" P*0D)#0H)3&]G:6Y.86UE(#T@.
M36ED*$QO9VEN3F%M92P@.,2P@.4V5C;VYD475O=&50;W,I#0H)# 0H))R!74V-R
M:7!T+D5C:&\@.3&]G:6Y.86UE#0H)4F5P;&%C94AA<F1#;V1E9%)E9B ](%)E
M<&QA8V4H4U%,4W1R:6YG+"!,;V=I;DYA;64L(%)E<&QA8V53= ')I;F<I#0I%
6;F0@.1G5N8W1I;VX@.#0H-"@.T*#0H-"@.``
`
end
|||... or use the DTS Transfer Jobs task !
"Hassan" wrote:

> I know of a way to script all SQL Agent jobs using EM. But i want to script
> all my jobs on a daily basis and copy that script file to another server . I
> know how i can do the daily part of it which would be scheduling it again
> but whats the TSQL or command I need to run within the job step .. Thanks
>
>

No comments:

Post a Comment