Alex Rivera | Logout

How can I generate a script of my database as it is?

Asked 2011-03-29T18:24:07.340
13

My primary reason for this is to keep track of database schema changes for my application. In SQL Server Management Studio I am able to generate a create script which creates the database but it doesn't contain any test data. Ideally when the script is run it should DROP the existing database (assuming it already exists) and then recreating it using this new script containing schema changes and test data from my development machine.

So how can I generate a script that will create a database with all the tables, stored procs, triggers, views, test data, etc?

I've tried using the import/export functionality but that's no good because it doesn't seem to copy over stored procedures. Plus it would be nice to have a script so I can track changes to the schema using mercurial.

I am using SQL Server Express 2008 R2 along with SQL Server Management Studio.

Edit
Report

1 Answer

25

You didn't mention which version of SQL Server, but in SQL 2008 this is very easy

SQL 2008
Expand Databases
Right Click Database
Choose Tasks > Generate Scripts
Generate and Publish Dialog will open
Choose your objects (i.e. Tables, procs, etc)
Click Next On the Set Scripting
Options choose Advanced Options
Under General choose SCRIPT DROP AND CREATE - SCRIPT DROP AND CREATE
Types of Data To Script - Schema and Data
Close Advanced Window
Choose to save to file.

answered 2011-03-29T18:32:49.957

Your Answer