如何按原样生成数据库脚本? [英] How can I generate a script of my database as it is?
问题描述
我这样做的主要原因是为了跟踪应用程序的数据库架构更改。在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.
那我如何生成一个脚本来创建一个包含所有表,存储的proc,触发器,视图,测试数据等的数据库?
So how can I generate a script that will create a database with all the tables, stored procs, triggers, views, test data, etc?
使用导入/导出功能,但这不好,因为它似乎无法复制存储过程。另外,拥有一个脚本会很好,这样我就可以使用mercurial跟踪架构的更改。
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.
我正在使用SQL Server Express 2008 R2和SQL Server Management Studio。
I am using SQL Server Express 2008 R2 along with SQL Server Management Studio.
推荐答案
您没有提到哪个版本的SQL Server,但是在SQL 2008中,这非常容易
You didn't mention which version of SQL Server, but in SQL 2008 this is very easy
SQL 2008
扩展数据库
右键单击数据库
选择任务>生成脚本
生成和
发布对话框将打开
选择您的
对象(即表,过程等)
在设置脚本上单击下一步
选项选择高级选项
在
下,常规选择 SCRIPT DROP AND
CREATE-SCRIPT DROP AND CREATE
要编写脚本的数据类型-Schema和
数据
关闭高级窗口
选择
保存到文件。
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.
这篇关于如何按原样生成数据库脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!