简体   繁体   English

如何生成将使用数据重建MS SQL Server 2005数据库的脚本?

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

I have a SQL Server 2005 database, that I would like to be able to recreate at a moment's notice. 我有一个SQL Server 2005数据库,我希望能够立即重新创建。 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? 是否有任何开源或免费工具可以做到这一点?

The Database Publishing Wizard is a great little tool for this. 数据库发布向导是一个很棒的小工具。 Its OSS and free, which is hard to beat. 它的OSS和免费的,这是很难击败的。

What I always do is let MS SQL Management Studio create the script to rebuild database und empty tables. 我一直做的就是让MS SQL Management Studio创建脚本来重建数据库和空表。 Then I use another script to generate a ms-dos batch file to export/import the data via "bcp". 然后,我使用另一个脚本生成ms-dos批处理文件,以通过“ bcp”导出/导入数据。 See sql below. 请参阅下面的sql。

/* 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. 如果将表生成脚本保存在文件中,则也可以通过sqlcmd命令将此文件放入批处理文件中。

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 http://anastasiosyal.com/archive/2007/04/25/5.aspx

Edit: Seems Will has found an even better solution +1 to Will 编辑:似乎威尔已经找到了一个更好的解决方案+1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM