The solution to this problem is the following:
I will build on the solution proposed by Thomas Lane ( link ), where Through a scripts task script, all the scripts of a server can be independently generated.
1) We need to have the following variables declared in the project:
2) We add a task script where we will pass the variables declared in the ReadOnlyVariables part:
3) We edit the script in C #, where we need to add the following references to the project:
To add the references, these have to be searched in C: \ Program Files \ Microsoft SQL Server \ 120 \ SDK \ Assemblies and add the corresponding ddls.
Once done, we can add the following import to the project:
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.Agent;
using Microsoft.SqlServer.Management.Common;
Then the Main () method has to have the following code:
public void Main()
{
string strFolder = @Dts.Variables["FilePath"].Value.ToString();
StringCollection sc = new StringCollection();
ScriptingOptions so = new ScriptingOptions();
so.IncludeDatabaseContext = true;
string filename = "";
//en caso de iniciar seison con cuenta de windows en sql, utilizamos esta parte...
/*
ServerConnection conn = new ServerConnection();
conn.LoginSecure = true;
conn.ServerInstance = Dts.Variables["ServerName"].Value.ToString();
*/
// si utiliamos cuenta de usuario en sql server utilizamos lo siguiente:
ServerConnection conn = new ServerConnection();
conn.LoginSecure = false;
conn.Login = Dts.Variables["UserName"].Value.ToString();
conn.Password = Dts.Variables["Password"].Value.ToString();
conn.ServerInstance = Dts.Variables["ServerName"].Value.ToString();
Server srv = new Server(conn);
if (!System.IO.Directory.Exists(strFolder))
{
System.IO.Directory.CreateDirectory(strFolder);
}
string script = "";
string strDrop = "";
string JobName;
//Loop over all the jobs
foreach (Job J in srv.JobServer.Jobs)
{
//Output name in the console
Console.WriteLine(J.Name.ToString());
JobName = J.Name.ToString();
sc = J.Script(so);
strDrop = "USE [MSDB] " + (char)13 + (char)10;
strDrop = strDrop + "IF EXISTS (SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = N'";
strDrop = strDrop + J.Name.ToString() + "')";
strDrop = strDrop + (char)13 + (char)10;
strDrop = strDrop + " EXEC msdb.dbo.sp_delete_job @job_name=N'" + J.Name.ToString();
strDrop = strDrop + "', @delete_unused_schedule=1";
strDrop = strDrop + (char)13 + (char)10 + "GO";
strDrop = strDrop + (char)13 + (char)10;
//Get all the text for the job
foreach (string s in sc)
{
script += s;
}
script = strDrop + script;
//Generate the file
filename = JobName + ".sql";
filename = Regex.Replace(filename, @"[\/:?'=|]", "");
if (strFolder.Substring(strFolder.Length - 1, 1) == @"\")
{
filename = strFolder + filename;
}
else
{
filename = strFolder + @"\" + filename;
}
TextWriter tw = new StreamWriter(filename);
tw.Write(script);
tw.Close();
//Make the string blank again
script = "";
strDrop = "";
}
// TODO: Add your code here
Dts.TaskResult = (int)ScriptResults.Success;
}
Then we save and execute the code:
We review the path, and we see the generated script: