Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come posso automatizzare l'attività di generazione degli script in SQL Server Management Studio 2008?

SqlPubwiz ha opzioni molto limitate rispetto alla generazione di script in SSMS. Al contrario, le opzioni disponibili con SMO corrispondono quasi esattamente a quelle in SSMS, suggerendo che probabilmente è anche lo stesso codice. (Spero che MS non l'abbia scritto due volte!) Ci sono diversi esempi su MSDN come questo che mostrano tabelle di scripting come singoli oggetti. Tuttavia, se si desidera che tutto venga eseguito correttamente con uno schema "completo" che includa oggetti "DRI" (Integrità referenziale dichiarativa) come chiavi esterne, lo scripting delle tabelle singolarmente non risolve correttamente le dipendenze. Ho scoperto che è necessario raccogliere tutti gli URN e consegnarli allo scripter come un array. Questo codice, modificato dall'esempio, funziona per me (anche se direi che potresti riordinarlo e commentarlo un po' di più):

    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Sdk.Sfc;
    // etc...

    // Connect to the local, default instance of SQL Server. 
    Server srv = new Server();

    // Reference the database.  
    Database db = srv.Databases["YOURDBHERE"];

    Scripter scrp = new Scripter(srv);
    scrp.Options.ScriptDrops = false;
    scrp.Options.WithDependencies = true;
    scrp.Options.Indexes = true;   // To include indexes
    scrp.Options.DriAllConstraints = true;   // to include referential constraints in the script
    scrp.Options.Triggers = true;
    scrp.Options.FullTextIndexes = true;
    scrp.Options.NoCollation = false;
    scrp.Options.Bindings = true;
    scrp.Options.IncludeIfNotExists = false;
    scrp.Options.ScriptBatchTerminator = true;
    scrp.Options.ExtendedProperties = true;

    scrp.PrefetchObjects = true; // some sources suggest this may speed things up

    var urns = new List<Urn>();

    // Iterate through the tables in database and script each one   
    foreach (Table tb in db.Tables)
    {
        // check if the table is not a system table
        if (tb.IsSystemObject == false)
        {
            urns.Add(tb.Urn);
        }
    }

    // Iterate through the views in database and script each one. Display the script.   
    foreach (View view in db.Views)
    {
        // check if the view is not a system object
        if (view.IsSystemObject == false)
        {
            urns.Add(view.Urn);
        }
    }

    // Iterate through the stored procedures in database and script each one. Display the script.   
    foreach (StoredProcedure sp in db.StoredProcedures)
    {
        // check if the procedure is not a system object
        if (sp.IsSystemObject == false)
        {
            urns.Add(sp.Urn);
        }
    }

    StringBuilder builder = new StringBuilder();
    System.Collections.Specialized.StringCollection sc = scrp.Script(urns.ToArray());
    foreach (string st in sc)
    {
        // It seems each string is a sensible batch, and putting GO after it makes it work in tools like SSMS.
        // Wrapping each string in an 'exec' statement would work better if using SqlCommand to run the script.
        builder.AppendLine(st);
        builder.AppendLine("GO");
    }

    return builder.ToString();