views:

28

answers:

3

Hi,

In T-SQL (Microsoft SQL 2008), how can I make a new database which will have the same schemas, tables, table columns, indexes, constraints, and foreign keys, but will not contain any data from the original database?

Note: making a full copy, then removing all data is not a solution in my case, since the database is quite big, and such full copy will spend too much time.

A: 

In SQL Management Studio, right click on the database and select "Script database as" http://msdn.microsoft.com/en-us/library/ms178078.aspx

You can then use the script to create an empty one. Edit : OP did say 2008

nonnb
2008 doesn't have Enterprise Manager
SQLMenace
Yup, showing my age :)
nonnb
+1  A: 

See here for instructions: How To Script Out The Whole Database In SQL Server 2005 and SQL Server 2008

SQLMenace
Sorry, my question was not clear enough. I need this to run as a scheduled task or a maintenance plan, not just one time. Since database structure change frequently, I can't just script the whole database like this.
MainMa
In that case use SMO and build your own solution in .NET and this then you can schedule. Also take a look into visual studio database projects(Visual Studio Team System for database architects in 2008 but I believe in 2010 Pro also has this) which could be used for this also
SQLMenace
It seems you can do this programmatically as wellhttp://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.database.script.aspxYou might also look at using something like a .dbproj in VS to manage and keep version control over your schema changes.
nonnb
A: 

I use liquibase for this purpose. Just point liquibase to a different server and it will use your changelog to bring the second database up to date, schema wise. It has the added benefit that the changelog file gets stored in source control and so I can have tagged versions of it, allowing me to restore a database to what a specific version of my app is expecting.

Matt Greer