Learn more about SQL Server tools



solving sql server problems for millions of dbas and developers since 2006 join MSSQLTips for free SQL Server tips


























































   Got a SQL tip?
            We want to know!

Generate Scripts for database objects with SMO for SQL Server

MSSQLTips author Arshad Ali By:   |   Read Comments (8)   |   Related Tips: More > SQL Server Management Objects SMO
Problem
In my last article, Getting started with SQL Server Management Objects (SMO), I discussed what SMO is, how you can start working with SMO, how to connect to SQL server, how to enumerate through the different SQL objects, create a database, create a table etc, all programmatically.

In this tip I would like to take you on an SMO ride to generate SQL object scripts programmatically. Though you can do this through SQL Server Management Studio (SSMS) there might be times (more details on usage scenarios given below) when you would need to create SQL scripts automatically.

Solution
As I discussed in my last tip, SQL Server objects are represented as object hierarchies inside SMO, for example a Server object is a collection of Database objects. A Database object is a collection of a Table (though there are couple of other collection inside the Database object as well such as Stored Procedure, Views, User-defined Functions etc).  A Table  is a collection of a Column and so on.

Every object in this hierarchy has a method called a script, which returns a string collection of scripts. Apart from that, SMO provides a utility class, Scripter, which generates the script in a more efficient way. For example, the Scripter class can discover the relationships between objects and can provide scripts for dependencies as well and it can respond to Progress and Error events.

Usage Scenario

As I said, SQL Server Management Studio (SSMS) provides a wizard type interface to script out all or selected objects, but there might be some scenarios, some of them are discussed below, where you would consider the use of SMO instead of SSMS.

  • You want to create a tool which will automatically set up (or sync) an environment for dev or test which resembles the production environment.
  • You have a data warehouse database, in this database data is not that important (or even the data size is so big, taking a backup of the entire database would not be feasible) as data can again be pulled from the source systems but you want to make sure the schema objects are scripted and backed up automatically so that you can re-create the database in case of a disaster.
  • Programmatically you want to control the backup and restore process of database administration (I will cover this in more detail in another tip "Backup and Restore Programmatically with SMO").
  • Programmatically you want to transfer a database schema and data to another instance of SQL Server. (I will cover this in more detail in another tip "Transferring schema objects and data programmatically with SMO").

Example

Before you start writing your code using SMO, you need to take reference of several assemblies which contain different namespaces to work with SMO. For more details on what these assemblies are and how to reference them in your code, refer to my tip Getting started with SQL Server Management Objects (SMO).

C# Code Block 1 - Here I am using the Scripter utility class to generate the script for two selected databases. Two Database objects are created first; one of them refers to the AdventureWorks database and another one refers to AdventureWorksDW.  The script method of the scripter object is called which takes database object URN (Unique Resource Name) as input and returns a string collection of scripts. URN is a new concept in SMO (this was not available in SQL-DMO) which provides similar notation like XPath to denote object hierarchy.

C# Code Block 1 - Generating Database Script

Scripter scripter = new Scripter(myServer);
Database myAdventureWorks = myServer.Databases["AdventureWorks"];
Database myAdventureWorksDW = myServer.Databases["AdventureWorksDW"];
Urn[] DatabaseURNs = new Urn[] { myAdventureWorks.Urn, myAdventureWorksDW.Urn };
StringCollection scriptCollection = scripter.Script(DatabaseURNs);
foreach (string script in scriptCollection)
    Console.WriteLine(script);

C# Code Block 2 - In this code block I am generating CREATE TABLE scripts for all the tables in the AdventureWorks database. As said before, a database is a collection of tables, so I am enumerating through the table collection of the database to generate a script for each table. Along with that I am also using the ScriptOptions class to specify the different scripting options, for example in this code I am scripting IF NOT EXISTS and DROP TABLE scripts as well.

C# Code Block 2 - Generating scripts for table collection

Scripter scripter = new Scripter(myServer);

Database myAdventureWorks = myServer.Databases["AdventureWorks"];
/* With ScriptingOptions you can specify different scripting
 
* options, for example to include IF NOT EXISTS, DROP
 
* statements, output location etc*/

scriptOptions = new ScriptingOptions();

scriptOptions.ScriptDrops = true;
scriptOptions.IncludeIfNotExists = true;  
foreach (Table myTable in myAdventureWorks.Tables)
{
    /* Generating IF EXISTS and DROP command for tables */
    StringCollection tableScripts = myTable.Script(scriptOptions);
    foreach (string script in tableScripts)
        Console.WriteLine(script);
 
    /* Generating CREATE TABLE command */
    tableScripts = myTable.Script();
    foreach (string script in tableScripts)
        Console.WriteLine(script);
}

C# Code Block 3 - This code block further extends the use of the Scripter and ScriptOptions classes to generate a script for a table (HumanResources.EmployeeAddress) along with all the other objects on which this table depends on. The ScriptOptions class also provides several properties for DRI (Declarative Referential Integrity) objects. You can either select individual DRI objects or all to script out along with the main object script.

C# Code Block 3
Generating table script with all dependencies and DRI objects

Scripter scripter = new Scripter(myServer);
Database myAdventureWorks = myServer.Databases["AdventureWorks"];
Table myTable = myAdventureWorks.Tables["EmployeeAddress", "HumanResources"];
 
/* Generate Scripts of table along with for all
 * objects on which this table depends on */
ScriptingOptions scriptOptionsForDependendencies = new ScriptingOptions();
scriptOptionsForDependendencies.WithDependencies = true;
/* DriAll will include all DRI objects in the generated script. */
scriptOptionsForDependendencies.DriAll = true;
/* You can optionally choose each DRI object separately as given below */
//scriptOptionsForDependendencies.DriAllConstraints = true;
//scriptOptionsForDependendencies.DriAllKeys = true;
//scriptOptionsForDependendencies.DriChecks = true;
//scriptOptionsForDependendencies.DriClustered = true;
//scriptOptionsForDependendencies.DriDefaults = true;
//scriptOptionsForDependendencies.DriForeignKeys = true;
//scriptOptionsForDependendencies.DriIndexes = true;
//scriptOptionsForDependendencies.DriNonClustered = true;
//scriptOptionsForDependendencies.DriPrimaryKey = true;
//scriptOptionsForDependendencies.DriUniqueKeys = true;
 
/* If you can use FileName to output generated script in a file
 * Note : You need to have access on the specified location*/
scriptOptionsForDependendencies.FileName = @"D:\TableScriptWithDependencies.sql";
StringCollection tableScripts = myTable.Script(scriptOptionsForDependendencies);
foreach (string script in tableScripts)
    Console.WriteLine(script);

C# Code Block 4 - This code block provides CREATE TABLE scripts for all the AdventureWorks' tables along with all the indexes of each table. Here I am using IndexCollection class to enumerate through index collection of the table to generate CREATE INDEX scripts for all the indexes on the given table.

C# Code Block 4
Generating scripts for table collection along with all the indexes on the table

Scripter scripter = new Scripter(myServer);
Database myAdventureWorks = myServer.Databases["AdventureWorks"];
/* With ScriptingOptions you can specify different scripting
 * options, for example to include IF NOT EXISTS, DROP
 * statements, output location etc*/
ScriptingOptions scriptOptions = new ScriptingOptions();
scriptOptions.ScriptDrops = true;
scriptOptions.IncludeIfNotExists = true;
 
foreach (Table myTable in myAdventureWorks.Tables)
{
    /* Generating IF EXISTS and DROP command for tables */
    StringCollection tableScripts = myTable.Script(scriptOptions);
    foreach (string script in tableScripts)
        Console.WriteLine(script);
 
    /* Generating CREATE TABLE command */
    tableScripts = myTable.Script();
    foreach (string script in tableScripts)
        Console.WriteLine(script);
 
    IndexCollection indexCol = myTable.Indexes;
    foreach (Index myIndex in myTable.Indexes)
    {
        /* Generating IF EXISTS and DROP command for table indexes */
        StringCollection indexScripts = myIndex.Script(scriptOptions);
        foreach (string script in indexScripts)
            Console.WriteLine(script);
 
        /* Generating CREATE INDEX command for table indexes */
        indexScripts = myIndex.Script();
        foreach (string script in indexScripts)
            Console.WriteLine(script);
    }
}

C# Code Block 5 - In my last tip on SMO, I showed you how you can use SMO to create a database and a table on the server programmatically. In this code block, I am using the same code, but this time before creating the objects on the server I am generating database and table scripts. This means it is not required to create objects on the server in order to generate scripts, if you have objects in memory you can generate scripts for these objects as well even without creating on the server.

 

C# Code Block 5
Generating Script for objects in memory even before creating on the server

/* Create database called, "MyNewDatabase" */
Database myDatabase = new Database(myServer, "MyNewDatabase");
 
/* Output the database script on the console */
StringCollection DBScripts = myDatabase.Script();
foreach (string script in DBScripts)
    Console.WriteLine(script);
 
/* Create a table instance */
Table myEmpTable = new Table(myDatabase, "MyEmpTable");
/* Add [EmpID] column to created table instance */
Column empID = new Column(myEmpTable, "EmpID", DataType.Int);
empID.Identity = true;
myEmpTable.Columns.Add(empID);
/* Add another column [EmpName] to created table instance */
Column empName = new Column(myEmpTable, "EmpName", DataType.VarChar(200));
empName.Nullable = true;
myEmpTable.Columns.Add(empName);
/* Add third column [DOJ] to created table instance with default constraint */
Column DOJ = new Column(myEmpTable, "DOJ", DataType.DateTime);
DOJ.AddDefaultConstraint(); // you can specify constraint name here as well
DOJ.DefaultConstraint.Text = "GETDATE()";
myEmpTable.Columns.Add(DOJ);
/* Add primary key index to the table */
Index primaryKeyIndex = new Index(myEmpTable, "PK_MyEmpTable");
primaryKeyIndex.IndexKeyType = IndexKeyType.DriPrimaryKey;
primaryKeyIndex.IndexedColumns.Add(new IndexedColumn(primaryKeyIndex, "EmpID"));
myEmpTable.Indexes.Add(primaryKeyIndex);
 
/* Output the table script on the console */
StringCollection TableScripts = myEmpTable.Script();
foreach (string script in TableScripts)
    Console.WriteLine(script);
 
/* If you want to create objects on the server you need call
 * create method or else objects will not be created on the server */
myDatabase.Create();
myEmpTable.Create();

Output:

The complete code listing (created on SQL Server 2008 and Visual Studio 2008, though there is not much difference if you are using it on SQL Server 2005 and Visual Studio 2005) can be found in the below text box.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Sdk.Sfc;
namespace SQLScriptGenerationProgrammatically
{
    class Program
    {
        static void Main(string[] args)
        {
            Server myServer = new Server(@"ARSHADALI-LAP\ARSHADALI");
            try
            {
                //Using windows authentication
                myServer.ConnectionContext.LoginSecure = true;
                myServer.ConnectionContext.Connect();
                //GenerateDBScript(myServer);
                //GenerateTableScript(myServer);
                //GenerateTableScriptWithDependencies(myServer);
                //GenerateTableScriptWithIndexes(myServer);
                GenerateScriptWithoutCreatingObjectOnServer(myServer);
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (myServer.ConnectionContext.IsOpen)
                    myServer.ConnectionContext.Disconnect();
                Console.WriteLine("Press any key to terminate....");
                Console.ReadKey();
            }
        }
        private static void GenerateDBScript(Server myServer)
        {
            Scripter scripter = new Scripter(myServer);
            Database myAdventureWorks = myServer.Databases["AdventureWorks"];
            StringCollection scriptCollection = scripter.Script(new Urn[] { myAdventureWorks.Urn });
            foreach (string script in scriptCollection)
                Console.WriteLine(script);
        }
        private static void GenerateTableScript(Server myServer)
        {
            Scripter scripter = new Scripter(myServer);
            Database myAdventureWorks = myServer.Databases["AdventureWorks"];
            /* With ScriptingOptions you can specify different scripting
             * options, for example to include IF NOT EXISTS, DROP
             * statements, output location etc*/
            ScriptingOptions scriptOptions = new ScriptingOptions();
            scriptOptions.ScriptDrops = true;
            scriptOptions.IncludeIfNotExists = true;
            foreach (Table myTable in myAdventureWorks.Tables)
            {
                /* Generating IF EXISTS and DROP command for tables */
                StringCollection tableScripts = myTable.Script(scriptOptions);
                foreach (string script in tableScripts)
                    Console.WriteLine(script);
                /* Generating CREATE TABLE command */
                tableScripts = myTable.Script();
                foreach (string script in tableScripts)
                    Console.WriteLine(script);
            }
        }
        private static void GenerateTableScriptWithDependencies(Server myServer)
        {
            Scripter scripter = new Scripter(myServer);
            Database myAdventureWorks = myServer.Databases["AdventureWorks"];
            Table myTable = myAdventureWorks.Tables["EmployeeAddress", "HumanResources"];
            /* Generate Scripts of table along with for all
             * objects on which this table depends on */
            ScriptingOptions scriptOptionsForDependendencies = new ScriptingOptions();
            scriptOptionsForDependendencies.WithDependencies = true;
            /* DriAll will include all DRI objects in the generated script. */
            scriptOptionsForDependendencies.DriAll = true;
            /* You can optionally can choose each DRI object separately as given below */
            //scriptOptionsForDependendencies.DriAllConstraints = true;
            //scriptOptionsForDependendencies.DriAllKeys = true;
            //scriptOptionsForDependendencies.DriChecks = true;
            //scriptOptionsForDependendencies.DriClustered = true;
            //scriptOptionsForDependendencies.DriDefaults = true;
            //scriptOptionsForDependendencies.DriForeignKeys = true;
            //scriptOptionsForDependendencies.DriIndexes = true;
            //scriptOptionsForDependendencies.DriNonClustered = true;
            //scriptOptionsForDependendencies.DriPrimaryKey = true;
            //scriptOptionsForDependendencies.DriUniqueKeys = true;
            /* If you can use FileName to output generated script in a file 
             * Note : You need to have access on the specified location*/
            scriptOptionsForDependendencies.FileName = @"D:\TableScriptWithDependencies.sql";
            StringCollection tableScripts = myTable.Script(scriptOptionsForDependendencies);
            foreach (string script in tableScripts)
                Console.WriteLine(script);
        }
        private static void GenerateTableScriptWithIndexes(Server myServer)
        {
            Scripter scripter = new Scripter(myServer);
            Database myAdventureWorks = myServer.Databases["AdventureWorks"];
            /* With ScriptingOptions you can specify different scripting
             * options, for example to include IF NOT EXISTS, DROP
             * statements, output location etc*/
            ScriptingOptions scriptOptions = new ScriptingOptions();
            scriptOptions.ScriptDrops = true;
            scriptOptions.IncludeIfNotExists = true;
            foreach (Table myTable in myAdventureWorks.Tables)
            {
                /* Generating IF EXISTS and DROP command for tables */
                StringCollection tableScripts = myTable.Script(scriptOptions);
                foreach (string script in tableScripts)
                    Console.WriteLine(script);
                /* Generating CREATE TABLE command */
                tableScripts = myTable.Script();
                foreach (string script in tableScripts)
                    Console.WriteLine(script);
                IndexCollection indexCol = myTable.Indexes;
                foreach (Index myIndex in myTable.Indexes)
                {
                    /* Generating IF EXISTS and DROP command for table indexes */
                    StringCollection indexScripts = myIndex.Script(scriptOptions);
                    foreach (string script in indexScripts)
                        Console.WriteLine(script);
                    /* Generating CREATE INDEX command for table indexes */
                    indexScripts = myIndex.Script();
                    foreach (string script in indexScripts)
                        Console.WriteLine(script);
                }
            }
        }
        private static void GenerateScriptWithoutCreatingObjectOnServer(Server myServer)
        {
            /* Drop the database if it exists */
            if (myServer.Databases["MyNewDatabase"] != null)
                myServer.Databases["MyNewDatabase"].Drop();
            /* Create database called, "MyNewDatabase" */
            Database myDatabase = new Database(myServer, "MyNewDatabase");
            /* Output the database script on the console */
            StringCollection DBScripts = myDatabase.Script();
            foreach (string script in DBScripts)
                Console.WriteLine(script);
            /* Create a table instance */
            Table myEmpTable = new Table(myDatabase, "MyEmpTable");
            /* Add [EmpID] column to created table instance */
            Column empID = new Column(myEmpTable, "EmpID", DataType.Int);
            empID.Identity = true;
            myEmpTable.Columns.Add(empID);
            /* Add another column [EmpName] to created table instance */
            Column empName = new Column(myEmpTable, "EmpName", DataType.VarChar(200));
            empName.Nullable = true;
            myEmpTable.Columns.Add(empName);
            /* Add third column [DOJ] to created table instance with default constraint */
            Column DOJ = new Column(myEmpTable, "DOJ", DataType.DateTime);
            DOJ.AddDefaultConstraint(); // you can specify constraint name here as well
            DOJ.DefaultConstraint.Text = "GETDATE()";
            myEmpTable.Columns.Add(DOJ);
            /* Add primary key index to the table */
            Index primaryKeyIndex = new Index(myEmpTable, "PK_MyEmpTable");
            primaryKeyIndex.IndexKeyType = IndexKeyType.DriPrimaryKey;
            primaryKeyIndex.IndexedColumns.Add(new IndexedColumn(primaryKeyIndex, "EmpID"));
            myEmpTable.Indexes.Add(primaryKeyIndex);
            /* Output the table script on the console */
            StringCollection TableScripts = myEmpTable.Script();
            foreach (string script in TableScripts)
                Console.WriteLine(script);
            /* If you want to create objects on the server you need call 
             * create method or else objects will not be created on the server */
            myDatabase.Create();
            myEmpTable.Create();
        }
    }
}

Note

  • If you have an application written in SQL-DMO and want to upgrade it to SMO, that is not possible, you will need to rewrite your applications using SMO classes.
  • SMO assemblies are installed automatically when you install Client Tools.
  • Location of assemblies in SQL Server 2005 is C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies folder.
  • Location of assemblies in SQL Server 2008 is C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies folder.
  • SMO provides support for SQL Server 2000 (if you are using SQL Server 2005 SMO it supports SQL Server 7.0 as well) but a few namespaces and classes will not be supported in prior versions.
  • Scripter and ScriptingOptions classes are available in Microsoft.SqlServer.Smo assembly (in microsoft.sqlserver.smo.dll) and Microsoft.SqlServer.Management.Smo namespace.
  • User needs to have permissions on the objects in order to generate scripts for it.

Next Steps



Last Update: 9/9/2009


About the author
MSSQLTips author Arshad Ali
Arshad Ali is a SQL and BI Developer focusing on Data Warehousing projects for Microsoft.

View all my tips


print tip Print  
Become a paid author





join MSSQLTips for free SQL Server tips     



Comments and Feedback:
Wednesday, September 09, 2009 - 4:58:50 AM - manish Read The Tip

Hi Arshad,

Thank you so much for this valuable information, I tried and its working well for me.

But I would like to know, how should I do to have the 'USE DATABASE' statement on the top of my script.

 


Wednesday, September 09, 2009 - 7:01:45 AM - arshad0384 Read The Tip

Thanks manish for your nice words and encouragements.

You can use IncludeDatabaseContext property like this for your requirement.

scriptOptions.IncludeDatabaseContext = true;

And result will be like this.

USE [AdventureWorks]
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [HumanResources].[EmployeeAddress](
        [EmployeeID] [int] NOT NULL,
        [AddressID] [int] NOT NULL,
        [rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
        [ModifiedDate] [datetime] NOT NULL
) ON [PRIMARY]

Hope it helps.


Friday, September 11, 2009 - 1:16:16 AM - ESL Read The Tip

Hi,

thank's for this article but, do you have the same examples using powershell script please ?

 

many thank's.

ESL


Friday, September 11, 2009 - 2:21:50 AM - arshad0384 Read The Tip

Hi,

I think these tips will be helpful for you.

Using PowerShell with SQL Server Management Objects (SMO)

http://www.mssqltips.com/tip.asp?tip=1745

Retrieve a List of SQL Server Databases and their Properties using PowerShell

http://www.mssqltips.com/tip.asp?tip=1759 

 


Wednesday, August 01, 2012 - 12:10:12 PM - Vassil Read The Tip

Hi Arshad,

I like your article very much and I find it very useful.

But :) I have got a problem when scripting views.

I can not get the proper order of views.

Some time refered view script is after refering one.

I lost for the moment.

Vassil


Thursday, August 30, 2012 - 7:17:08 AM - Avinash Singh Read The Tip

Hi,

 I have added Microsoft.SqlServer.Smo assembly  and Microsoft.SqlServer.Management.Smo both but still ScriptOptions class showing "The Name  ScriptOptions does not exist"



Plese give solution for that...


 


Monday, November 26, 2012 - 1:36:41 AM - narasimha Read The Tip

Hi,

 

How to generate Change script of table instead of Create table script, How to get only alter script of table using SMO.

 

Thanks in advance.


Wednesday, January 23, 2013 - 3:03:21 PM - OklahomaCoder Read The Tip

Arshad,

Thanks for this informative blog post. I've been working with the SMO objects for a while now, and trying to find references, (useful) documentation, or examples has been a severe challenge!

I wonder if you might be able to help with one area that has proven most troublesome for me - the declaration of a ScriptErrorEventHandler. I've written a simple scripting engine that's working as desired, except for the assignment of such a handler.

I have an error handler declared as:

static void myScriptErrorHandler(object e, ScriptErrorEventArgs s)

{

  //...code

}

And, early in my code, I assign the handler thusly:

script.ScriptError += new ScriptintErrorEventHandler(myScriptErrorHandler);
script.Options.ContinueScriptingOnError = true; // have tried false as well, doesn't matter

Within my application, when a scripting error fires, I expect the handler to fire, but it never does. The exception is merely thrown to whatever try-catch block (if any) is active at the time.

So, obviously, I'm not understanding the proper way to implement an error event handler. Thanks for any insight or suggestions you may have!

 



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email Notify for updates



Comments
Get free SQL tips:

*Enter Code refresh code


 
Sponsor Information







Copyright (c) 2006-2014 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | free t-shirt | user groups | community | events | first timer?
Some names and products listed are the registered trademarks of their respective owners.