How to Get the Scripts for SQL Server Objects

Introduction

I usually come across the need to script out the objects from a SQL Server database to text files. I like to create one script per object with different file extensions to identify the object type. There are many ways this can be done. One can connect to SQL server and script out the objects using Enterprise Manager. One can use Visual Studio and create a database project. This too allows you to script out the object. However, these methods are not effective if you want to automate this process. One should think about a continuous integration process which can allow one to have a clean database build process with the most recent changes. Another benefit is the ability to maintain these scripts files in a source control repository.

Purpose/Scope

The main goal of this article is to show an approach of how to script out the SQL server objects using the SQL-DMO COM object. There are other approaches, but the focus on this article is using this COM object with VB Script. Any other language which supports COM can be used to achieve the same. This script has been tested using SQL 2000/2005 ,MSDE/Express.

Background

This script uses the SQLDMO.SQLServer2 COM interface. DMO stands for Distributed Management Objects. It provides an interface to programmatically manage SQL Server. The reader is encouraged to read further on this topic.

Using the code

This script connects to a SQL Server database using Windows or SQL Server authentication. Once it is connected, it iterates through the collection of objects in the database and scripts each one of them

The code is divided into three main areas. There are the constant declarations which are needed to provide the different scripting options. For example, if one wants the script to first drop the object before creating it. The entry point Main handles the parameter validation and drives the calls to script the objects, and the ScriptObjects subroutine which iterates thru the elements of the collection.

The constant definitions are needed when a call to the Script method is made. There are other options, but these are the ones used in this article.

The Main subroutine reads the arguments. The arguments are referenced by the objArgs variable which holds an array of values. The first three parameters are required. The last two parameters are optional. A reference to the COM interface is created:

Set oSql = WScript.CreateObject("SQLDMO.SQLServer2")

If only three arguments are passed, the script sets the LoginSecure property to True which indicates that Windows Authentication is used to connect to the database. Otherwise, SQL Authentication is used instead. Once the connection is successful, calls to ScriptObjects are made using the following collections:

if err.Description <> "" then StdOut.Echo err.Description end if
end sub

The ScriptObjects subroutine iterates through the elements in the list. It checks to make sure the object is not a SystemObject. This is because one only needs to script out user objects. The options are a combination of constant values which are passed as an argument to the Script method. This is what allows us to create additional information in the script. This information can be constraint definitions, triggers, options to look if object exists and to drop it before creating it. There are also two additional arguments to this subroutine, ext and msg. The ext argument is used as the file extension. The msg argument is used to display the status of what is being generated. The format of the files generated is OWNER.OBJECTNAME.EXTENSION, so for the MyProc stored procedure which belongs to dbo, the script generated would be:

There are two helper functions to help us create a folder for each object type. The GetFolderName function maps the file extension to a predefined folder name (i.e TAB = Tables). A call to the CreateFolder subroutine is made to create the folder if it does not already exists. The purpose here is to group the object types in separate folders. This is similar to the way the objects are presented using SQL Server tools.

if not fso.FolderExists(path) then fso.CreateFolder(path) end if
set fso=nothing
end sub

This article only covers a small subset of the functionality available using SQL-DMO. If the goal is to have an automated build and source control process, this article can provide you with some direction. I hope some of you can find this useful.