如何生成数据库的脚本?
我这样做的主要原因是跟踪我的应用程序的数据库架构更改。在 SQL Server Management Studio 中,我能够生成一个创建脚本来创建数据库,但它不包含任何测试数据。理想情况下,当脚本运行时,它应该删除现有数据库(假设它已经存在),然后使用包含架构更改和来自我的开发计算机的测试数据的新脚本重新创建它。
那么如何生成一个脚本来创建一个包含所有表、存储过程、触发器、视图、测试数据等的数据库呢?
我尝试过使用导入/导出功能,但这不好,因为它似乎无法复制存储过程。另外,如果有一个脚本就好了,这样我就可以使用 Mercurial 跟踪架构的更改。
我正在使用 SQL Server Express 2008 R2 和 SQL Server Management Studio。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您没有提到 SQL Server 的哪个版本,但在 SQL 2008 中这非常简单
SQL 2008
展开数据库
右键单击数据库
选择任务>生成脚本
生成并
发布对话框将打开
选择您的
对象(即表、过程等)
单击“下一步”设置脚本
选项选择高级选项
下
一般选择 SCRIPT DROP AND
创建 - 脚本拖放并创建
要编写脚本的数据类型 - 架构和
数据
关闭高级窗口
选择
保存到文件。
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.
我编写了一个名为 的开源命令行实用程序SchemaZen 就是这样做的。它比 Management Studio 的脚本要快得多,而且它的输出对版本控制更加友好。它支持对模式和数据进行脚本编写。
要生成脚本,请运行:
然后从脚本重新创建数据库,请运行:
I wrote an open source command line utility named SchemaZen that does this. It's much faster than scripting from management studio and it's output is more version control friendly. It supports scripting both schema and data.
To generate scripts run:
Then to recreate the database from scripts run:
尝试 Microsoft SQL Server 数据库发布向导。这是一个强大而灵活的工具,用于编写 SQL Server 架构/数据脚本。
Try Microsoft SQL Server Database Publishing Wizard. This is a powerful flexible tool for scripting schema / data rom SQL Server.
我个人使用 Microsoft Visual Studio 2010 数据库项目 和源代码控制存储库 ( SVN)来跟踪架构的更改。
Personally I use Microsoft Visual Studio 2010 Database Project with a Source Control Repository (SVN) to track changes to the schema.
注意数据库排序规则的差异。如果您在具有不区分大小写排序规则的数据库上进行开发,并尝试针对具有区分大小写排序规则的数据库运行 SSMS 生成的脚本,则大小写错误将破坏脚本。
Watch out for difference in database collation. If you develop on a database with a case insensitive collation and try and run the SSMS generated scripts against as database with a case sensitive collation then errors in case will break the scripts.
通常我会在开始新的开发之前对数据库进行备份。
最好的方法是在需要时恢复备份,我不知道如何通过脚本方式获取它!
Usually i make backups fom a database before start a new development on it.
the best way is restore the backup when needed, i don't know how to get it by the script way!