Scripting database objects

Being able to script database objects is a powerful capability. There may be times when you'll need to have scripts handy, whether for versioning purposes or simply to provide them to your developers.

SMO largely drives this scripting capability. There is a class called Microsoft.SqlServer.Management.Smo.Scripter, which can be passed a collection of SMO objects to script:

$script.Script($smoObjects)

For example, if you need to script all stored procedures, you can add all the stored procedures into an array and pass this array to the Scripter object. You need to ensure that you are passing the actual stored procedure object and not just the names.

The Scripter object also accepts ScriptingOptions. There are a number of ...

Get PowerShell for SQL Server Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.