Thursday, May 25, 2017

How to generate script for multiple objects using SQL Server Management Studio

In this post, I will explain how to script multiple objects using SQL Server Management Studio. You may be aware of generating script by right clicking on specific object (Table / Stored Procedure / Function / View etc), or also you may generate scripts by right clicking on the database node, select Tasks, and then click on Generate Scripts... option. In this second option you can generate script for all objects or for some selected objects with advanced scripting options.

But in this post I will explain a short trick to generate script for multiple objects without going through the Generate Scripts... wizard. Obviously it will not provide other advanced options that we can have while using the Generate Script wizard, but it will let you create script for Create / Drop scenarios.

Following are the steps required to generate script for multiple objects, here I am considering generate Create script for selected tables.

  • Within SQL Server Management Studio, select your database.
  • Since I am going to generate script for tables, so I selected the Tables node, you may want to select Stored Procedures, Functions or Views etc.
  • On the right hand side you may see Object Explorer Details view, if not, you can make it visible from menu View > Object Explorer Details, or simply you can hit F7 shortcut key.
  • In details view, select the required objects.
  • Right click on the selected objects > Script Table as > Create To > New Query Editor Window (or other option)