Editorials

SSMS Database Scripting

One of the great features included in SQL Server Management Studio (SSMS) is the ability to script out a database so that it can be re-created using TSQL commands. This makes the database highly transportable in that the script can be generated and then modified if needed to address implementations for different versions of SQL Server.

For example, the Sequence object was not available in SQL Server 2005. However, you could script out a SQL Server database that does use a sequence object, and convert the script for a different implementation. The ability to convert a database to a script object provides the developer with a mechanism to make things work according to a target implementation.

There are two key things I really like about the scripting. First, you can selected to script out one ore more objects in the database. In that selection you can export into a number of targets. My favorite is to export to files. You can export to a single file. But, exporting to individual files makes it easier to find specific objects you may want to tweak or review.

The second thing I like is related to data. You can export the definitions of the database for tables, etc. as described previously. But, for files you can also determine if you want to script the file contents. The scripting will create insert TSQL statements to re-populate the tables after they have been created.

If you script everything to a single file, in some versions of SQL Server it will generate the file such that the objects are created, data is imported, and then referential integrity is applied, allowing the script to be defined without regard to a necessary order based on referential integrity.

Scripting a database with data, and then restoring to a new instance is not an efficient method for copying a database. Detaching a database and copying data files, or performing a Backup and then a Restore is much more efficient. However, scripting does allow you to transport a database from a newer version of SQL Server to an older version.

As a matter of interest, the scripting capabilities are part of the Database Management Objects library. So, if you want to use the scripting capabilities they are actualy part of this Dot Net library. You can use it to perform your own customized implementations.

One final tip. In SSMS, if you select Options from the Tools menu, you can scroll down to SQL Server Object Explorer optoins. Expand that group and select Scripting. In the dialog you can now define what you want the default settings to be the next time you open the database scripting wizard. If you have a standard way you like to script a database, such as what parts of a table do you want to include in the script, you can set that configuration here as the default.

Cheers,

Ben