How do I generate scripts that will rebuild my MS SQL Server 2005 database with data?

StackOverflow https://stackoverflow.com/questions/18199

  •  09-06-2019
  •  | 
  •  

Question

I have a SQL Server 2005 database, that I would like to be able to recreate at a moment's notice. I want to be able to point to my database and generate a complete set of scripts that will not only create all the tables / views / sprocs / functions that are in the database, but will also populate all the tables with data.

Are there any tools that do this? Are there any open-source or free tools that do this?

Was it helpful?

Solution

The Database Publishing Wizard is a great little tool for this. Its OSS and free, which is hard to beat.

OTHER TIPS

What I always do is let MS SQL Management Studio create the script to rebuild database und empty tables. Then I use another script to generate a ms-dos batch file to export/import the data via "bcp". See sql below.

/* this is used to export */
use databaseXXX
select ('bcp databaseXXX..' + name + ' OUT ' + name + ' /eErrors.txt /b100 /n /Usa /Ppwd /Sserver') as bcp 
from 
  sysobjects 
where 
  type = 'U' 
order by 
  [name]


/* this is used to import */
use databaseXXX
select ('bcp databaseXXX..' + name + ' IN ' + name + ' /E /eErrors.txt /b100 /n /Usa /Ppwd /Sserver') as bcp 
from 
  sysobjects 
where 
  type = 'U' 
order by 
  [name]

Works for me everytime and is quick. If you save the table generation script in a file you can put this also into a batch file via sqlcmd command.

Check the following for a procedure that will create a script that will generate a table and all its data. You could wrap this up in another stored proc that iterated all tables and generate a single large script that will regenerate everything from scratch.

http://anastasiosyal.com/archive/2007/04/25/5.aspx

Edit: Seems Will has found an even better solution +1 to Will

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top