Share via


How to: Generate a Script (SQL Server Management Studio)

You can create Transact-SQL scripts for multiple objects by using the Generate and Publish Scripts Wizard.. You can also generate a script for individual objects or multiple objects by using the Script as menu in Object Explorer.

Generate and Publish Scripts Wizard

Use the Generate and Publish Scripts Wizard to create a Transact-SQL script for many objects. The wizard generates a script of all the objects in a database, or a subset of the objects that you select. The wizard has many options for your scripts, such as whether to include permissions, collation, constraints, and so on. For more information, see Generate and Publish Scripts Wizard F1 Help.

To open the Generate and Publish Scripts Wizard

  1. In Object Explorer, expand Databases, right-click a database, point to Tasks, and then click Generate Scripts. Follow the steps in the wizard to script the database objects.

  2. On the Choose Objects page, select the objects to be included in the script. For more information, see Generate and Publish Scripts Wizard (Choose Objects Page).

  3. On the Set Scripting Options page, select Save scripts to a specific location.

    1. To specify advanced scripting options, select the Advanced button in the Save scripts to a specific location section.

    2. Select the location for the generated script: to a file, a Database Engine Query Editor window, or the Clipboard.

    For more information, see Generate and Publish Scripts Wizard (Set Scripting Options Page).

  4. On the Summary page, review your selections. Click Previous to change your selections. Click Next to generate a script of the objects you selected. For more information, see Generate and Publish Scripts Wizard (Summary Page).

  5. On the Save or Publish Scripts page, monitor the progress of the script generation. For more information, see Generate and Publish Scripts Wizard (Save or Publish Scripts Page)

Object Explorer Script as Menu

You can use the Object Explorer Script as menu to script a single object, script multiple objects, or script multiple statements for a single objects. You can choose one of several types of scripts; for example to create, alter, or drop the object. You can save the script in a Query Editor window, to a file, or to the Clipboard. The script is created in Unicode format.

To generate a script of a single object

  1. In Object Explorer, connect to an instance of the SQL Server Database Engine and then expand that instance.

  2. Expand Databases, and then expand the category of the object. For example, expand the Tables or Views node.

  3. Right-click the object, point to Script <object type> as, For example, point to Script Table as.

  4. Point to the script type, such as Create to or Alter to.

  5. Select the location to save the script, such as New Query Editor Window or Clipboard.

Sometimes you may want a script with multiple options, such as drop a procedure and then create a procedure, or create a table and then alter a table. The following process also works if you need to create a script that references different types of objects, such as tables, views, and stored procedures.

To generate a script of two objects using Object Explorer

  1. In Object Explorer, navigate to the first object you want to script.

  2. Right-click the object, point to Script <object type> as, and in the Save as selections chooses New Query Editor Window as the output destination.

  3. Navigate to the second object you want to script.

  4. Right-click the object, point to Script <object type> as, and in the Save as selections chooses Clipboard as the output destination.

  5. In the Query Editor window opened for the first object, paste the script for the second object from the clipboard.

You can use the Object Explorer Details pane to generate a script for mutliple objects of the same category.

To generate a script of two objects using Object Explorer Details.

  1. In Object Explorer, navigate to the category node of the types of object you want to script, such as the Tables node.

  2. Open the Object Explorer Details pane by either selecting F7, or opening the View menu and selecting Object Explorer Details.

  3. Left-click one of the objects you want to script.

  4. Crtl + left-click the second object you want to script.

  5. Right-click one of the selected objects, and select Script <object type> as.