Monday, November 23, 2009

SQL Server Management Studio Generating Change Scripts

For those of you who need to lookup SQL syntax for things like alter table, this will let SQL Server Management Studio (SSMS) do the work for you giving you a baseline script to modify or check right into source control.

Generating an Alter Table Statement:

  1. In SSMS use the object explorer to navigate to a table.
  2. Right click and select design.
  3. Make your changes.
  4. From the menu select Table Designer --> "Generate Change Script..."
  5. Copy and paste the script text or click the yes button to save it to a text file.

Adding a Foreign Key:

  1. In SSMS use the object explorer to navigate to a table.
  2. Expand the table node.
  3. Right click on Keys  and select "New Foreign Key..."  Notice this brings up the table designer if it isn't already open.
  4. Fill out the new FK and click Close
  5. You should now be in the table designer.  Follow the steps from above.

No comments: