join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 



…try SQL Backup pro for faster, smaller, more robust backups.

Auto generate change scripts in SQL Server Management Studio (SSMS) for tables

Written By: Atif Shehzad -- 4/6/2009 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
As a part of my best practices, I always save the T-SQL scripts used for creation and modification of objects in SQL Server. When creating and modifying tables using SQL Server Management Studio designer it is easy to right click in the designer and select "Generate Change Script...", but is there a way to automatically script the creation and/or modification of tables made through of SQL Server Management Studio (SSMS) designer?  This tip shows you an option that exists within SSMS to automatically generate these scripts for all table changes when using the table designer.

Solution
Within SQL Server Management Studio (SSMS) there is an option to
automatically generate scripts for your changes done through the GUI.  The solution for this problem is provided in SSMS and works with both SQL Server 2005 and SQL Server 2008, but by default this option that we will discuss is disabled.

To enable the option

  • From the SSMS menus click on "Tools" 
  • Click on "Options..."
  • Click on "Designers"
  • Check the checkbox "Auto generate change scripts" as highlighted below

After checking the specified checkbox, SSMS is now able to generate scripts for the creation and/or modification of any table when you use the designer.

Testing It Out

Let's confirm the script generation for the creation of a new table.

  • In SSMS select a database and expand the tree
  • Right click on the tables folder inside and click on "New Table".
  • A designer will appear for the creation of new table.
  • Create a single column for this table, I have created a column named TestCol1 as nchar(10)
  • Save the new table and give the table a name and click OK
  • Another window will popup that includes the actual script for creating this new table as shown below

  • Click on "Yes" to save the script and you will be prompted to specify the location.  The file name by default will be the name of the table such as "tableName.sql", but this can be changed to anything you like.
  • If you do not want to save the script then click "No", clicking "No" will not roll back the implemented changes, but the script will not be saved

Notes:

  • It is important to mention here that unchecking the box in this frame as highlighted above "Automatically generate change script on every save" will cause this change to be undone in the options that we first configured.
  • Also scripts are not automatically generated for deletion of a table
  • While working on views through designers no script is generated, however the code for views operation is provided inside the designer and may be copied and saved.

Next Steps

  • By implementing script auto generation for your SSMS designer you will be able to save changes made through designers easily, which will allow you to have change scripts for changes made through SSMS designer.
Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Realistic test data in just one click with SQL Data Generator.

SQL Server Consultants - What you don't know could be your biggest asset - Guaranteed Results

Join the MSSQLTips LinkedIn Group

Learn SQL Server 2008, Performance Tuning, Development, Administration, DR, Replication and more from these web casts


Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.



Idera - SQL safe backup

Need more space for your backups? Idera has announced the latest edition of SQL safe, their award-winning backup and recovery solution. SQL safe v6.0 includes brand new compression algorithms developed with some of the leading compression experts in the world. Plus, SQL safe 6.0 continuously optimizes compression to give you the fastest possible backup with the highest possible compression, every time you do a backup.

Download now!

More SQL Server Tools
SQL Compare

SQL compliance manager

SQL Backup

SQL Refactor

SQL safe backup




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com