如何生成 SQL 脚本来更新现有数据库并保留其数据
我有一个 SQL Server 2008 数据库(称为 productionDB),其中包含数据并将在生产环境中使用。
我有另一个在临时环境中使用的 SQL Server 2008 数据库(称为 stagingDB)。
我正在开发的应用程序不断发展,因此我经常更改数据库架构(即添加/编辑表和添加/编辑存储过程)。
到目前为止,每次我对 stagingDB 进行更改时,我都会生成脚本(通过 SQL Server 脚本向导)来删除/创建所有架构。因此,当我在 ProductionDB 上运行脚本时,它将成功更新到更新后的架构,但 ProductionDB 拥有的所有数据都将丢失 - 到目前为止,这一切都很好。
展望未来,我想生成将现有数据保留在 productionDB 中并更新其架构的脚本。
我在 SQL Server 脚本向导中找不到可以执行我所描述的操作的选项。
SQL Server 脚本向导能够完成我想要的任务吗?如果是这样,怎么办?
I have a SQL Server 2008 database (call it productionDB) that contains data and will be used in a production environment.
I have another SQL Server 2008 database (call it stagingDB) that is used in a staging environment.
The application that I am working on continues to evolve such that I am often making changes to the database schema (namely adding/editing tables and adding/editing stored procedures).
Up to this point, every time I have made changes to stagingDB I have generated scripts (via the SQL Server Scripts Wizard) that will drop/create all the schema. So, when I ran the scripts on the productionDB, it would successfully get updated to the updated schema, but all data that productionDB had would be lost - which has been fine, up til now.
Moving forward, I would like to generate scripts that will keep the existing data in productionDB as well as update its schema.
I cannot find an option(s) in the SQL Server Scripts Wizard that will do what I am describing.
Is the SQL Server Scripts Wizard capable of doing what I am looking for? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
删除/重新创建是一把大刀;你必须更加手术。以下是一些伪代码示例:
添加新列:
如果它不能为空,请设置默认值,或更新该列以填充数据,然后更改表并将该列设置为 NOT NULL。
重命名列:
重命名表:
注意一个共同的主题;首先,您必须通过检查更新的架构元素是否存在来检查是否已执行此更新。然后,按照添加架构 -> 迁移数据 -> 删除架构的顺序仅添加或删除必要的内容。编码的工作量要多一些,但运行速度会更快并且可以保存数据。
Drop/recreate is a broadsword; you have to be more surgical. Here are some pseudocode examples:
Adding a new column:
If it cannot be nullable, set a default value, or update the column to populate data, then alter the table and set the column to be NOT NULL.
Renaming a column:
Renaming a table:
Notice a common theme; first, you must check to see if you've already performed this update, by checking for the existence of the updated schema elements. Then, add or drop only what's necessary, in the order add schema->migrate data->drop schema. It's a little more work to code, but it will run faster and save your data.
我真的不知道 SQL Server 有什么方法可以自动执行您想要的操作,但您可能会发现商业应用程序(例如 RedGate 等公司提供的应用程序)可以执行您想要的操作,但这是一个非常复杂的主题,并且总会有一些无法真正管理的场景。我总是找到一个更好的方案来编写对暂存的修改的脚本,以便它们可以在实时情况下重新运行。因此,您编写脚本来修改表结构 - 这样您就可以在脚本运行时维护暂存数据和实时数据。
I'm not really aware of any way in SQL Server to automatically do what you want, but you may find commercial applications (Such as those supplied by companies like RedGate) that do what you want, but it is a very complex subject matter and there will always be scenarios that can't really be managed. I've always found a better scenario to script your modifications to Staging in such a way that they can be re-run against live. So you write scripts to amend the table structure - that way you maintain both the staging and live data when the scripts are run.
您的主要问题是 SQL Server 脚本向导不知道您将运行脚本的数据库处于什么状态。
因此,它不知道要应用哪些差异,例如:何时使用
alter table
或何时使用create table
。是的,脚本可以添加一个
If Not Exist
子句,但它也不知道当表存在但需要额外的 2 列时要做什么。 SQL Server 脚本向导不会与另一个架构进行比较,它只是按原样编写现有架构的脚本。根据您的 SQL2008 版本,您可能会随 SQL 安装一个 BIDS 版本。在开始菜单中,您应该有“Microsoft SQL Server 2008”文件夹,其中有一个指向“SQL Server Business Intelligence Development Studio”(BIDS) 的链接。
同样,这取决于 SQLServer2008 的版本。 BIDS 是 Visual Studio 开发环境。
现在,如果您有,根据您拥有的 BIDS 版本(我认为架构比较工具的最低版本是 VS2008 SP1),您可以在其中创建项目来比较 2 个数据库架构。然后可以将差异编写到适当的脚本中。
我自己不知道如何在 VS2008 中执行此操作,因为我们使用的是 VS2010,其版本已完全实现。我查看了VS2008,但在明显的位置找不到它。据Google称,VS2008 SP1有一个模式比较工具。
如果所有其他方法都失败,您将不得不搜索免费或付费的架构比较工具,例如 SQL 比较 8.2。
想法是相同的,您必须将 2 个模式相互比较,看看有什么不同。然后决定您想要从登台到现场的内容,并相应地编写脚本。这些工具只是为您完成大部分手动工作。
Your main problem will be that the SQL Server Script Wizard has no clue what state your database is in you will run the script against.
Thus, it does not know what differences to apply, e.g.: When to use
alter table
or when to usecreate table
.Yes, the script can add a
If Not Exist
clause, but that also doesn't know what to do when your table exists but needs an extra 2 columns. The SQL Server Script Wizard does not do a compare to another schema, it simply scripts your existing schema as is.Depending on your version of SQL2008 you might have a version of BIDS installed along with SQL. In the start menu you should have your "Microsoft SQL Server 2008" folder, within that you have a link to "SQL Server Business Intelligence Development Studio" (BIDS).
Again, this will depend on the version of SQLServer2008. BIDS is the Visual Studio Development Environment.
Now, if you have that, depending on which version of BIDS you have (I think minnimum for schema compare tool is VS2008 SP1) you can create projects in there to compare 2 database schemas. The difference can then be scripted into a appropriate script.
I don't know myself how to do it in VS2008 as we are using VS2010 with an edition which has it fully implemented. I looked at VS2008 but could not find it in an obvious location. According to Google though VS2008 SP1 has a schema compare tool.
If all else fails, you going to have to search for a free or pay-for Schema Compare Tool, such as SQL Compare 8.2.
The idea is the same, you have to compare 2 schemas with each other to see what is different. Then decide what you want to push through from staging to Live and script it accordingly. The tools merely do most of the manual work for you.
您可以使用 Visual Studio 数据库项目管理数据库(使用数据库脚本很容易创建),然后当您想要更新现有数据库时,对要更新的数据库使用发布选项将生成一个脚本更新数据库,如果需要,可以针对数据库运行脚本:)
You can manage the database using a visual studio database project (quite easy to create using a script for your database), then when you want to update an existing database, using the publish option against the database you want to update will generate a script to update the database and if required run the script against the database for you :)