简体   繁体   English

Visual Studio 2010 SQL Server 2008数据库项目 - 自定义更新

[英]Visual Studio 2010 SQL Server 2008 Database Project - Custom Updates

In Visual Studio, say you've got a SQL 2008 Database Project and you add a non-nullable column to a table. 在Visual Studio中,假设您有一个SQL 2008数据库项目,并且将非空列添加到表中。 When you go to deploy the database now, if that table has data in it it should fail. 现在开始部署数据库时,如果该表中包含数据,则该数据库将失败。 (Right?) How would you write custom logic so that during the update you can set this column to "x" or use a more advanced query or cursor to update the entire table and fill in the new column? (对吗?)您将如何编写自定义逻辑,以便在更新期间可以将此列设置为“ x”,或者使用更高级的查询或游标更新整个表并填写新列? This is something you would only want to happen once - at the same time the column was added to the database. 这是您只希望发生一次的事情-同时将列添加到数据库中。 Is there any support for this? 有什么支持吗?

Here is how I do it. 我就是这样做的。

In the Schema Viewer open the table creation script and add the new columns like so: 在Schema Viewer中打开表创建脚本并添加新列,如下所示:

[first_name]        varchar( 32 )   NOT NULL default ' ',
[last_name]         varchar( 32 )   NOT NULL default ' ',

Because they have a * default value * they can deploy successfully. 由于它们具有* 默认值*,因此可以成功部署。

Then in my post install script I check for ' ' values in the table and run an update to populate them if so. 然后在我的安装后脚本中,我检查表中的''值并运行更新以填充它们,如果是这样的话。 Alternately the code could fix it. 或者,代码可以修复它。

I would add a nullable column, run an UPDATE command to populate it, and alter the column to NOT NULL. 我将添加一个可为空的列,运行UPDATE命令以填充它,并将该列更改为NOT NULL。 Although I do use Visual Studio 2010, I do not use it to deploy. 虽然我确实使用Visual Studio 2010,但我不使用它来部署。 Instead, I deploy with SQL Compare - it generates a SQL script, which is easy to tweak. 相反,我使用SQL Compare进行部署-它生成一个SQL脚本,该脚本易于调整。

update mytable
set newcolumn = 'x'
where newcolumn is null;

I have done a lot of research on this type of topic , and sadly it seems to be an area where VSDB projects are lacking. 我对这类主题进行了大量研究,遗憾的是它似乎是一个缺乏VSDB项目的领域。 A good place to start looking is the Visual Studio ALM Rangers guide to VSDB projects; 一个很好的开始地方是VSDB项目的Visual Studio ALM Rangers指南 they have a hands-on-lab detailing their recommended best-practice for doing exactly what you ask about (it involves using pre and post-deployment scripts to copy data to and from transient tables). 他们有一个动手实验室,详细介绍了按照您的要求做事的推荐最佳实践(涉及使用部署前和部署后脚本在临时表之间复制数据)。 Additionally, Barclay Hill's blog has an article pertaining to your question (he's the Sr. Program manager for Data Tools), which is the same as the guide. 此外,Barclay Hill的博客上有一篇与您的问题相关的文章 (他是数据工具的高级项目经理),与指南相同。

This recommended method seems to me as having a very high maintenance cost if you have to be able to update multiple different versions of a target database, but if you only have one database (or one version of the schema) to update, it is pretty workable. 在我看来,如果必须能够更新目标数据库的多个不同版本,则此推荐方法的维护成本非常高,但是如果您仅要更新一个数据库(或架构的一个版本),那么这很漂亮可行的。

Here's what I ended up doing. 这就是我最终做的事情。

  1. Add version info to the database (eg in a SystemSettings table). 将版本信息添加到数据库(例如,在SystemSettings表中)。
  2. Create a script for the next version (eg 1.1.0.0.sql). 为下一个版本创建一个脚本(例如1.1.0.0.sql)。
  3. In the Scripts\\Post-Deployment\\Script.PostDeployment.sql file, add the following: 在Scripts \\ Post-Deployment \\ Script.PostDeployment.sql文件中,添加以下内容:

DECLARE @versionMajor INT; DECLARE @versionMajor INT;
DECLARE @versionMinor INT; DECLARE @versionMinor INT;
DECLARE @versionBuild INT; 声明@versionBuild INT;
DECLARE @versionRevision INT; DECLARE @versionRevision INT;

SELECT TOP 1 选择TOP 1
@versionMajor = VersionMajor, @versionMajor = VersionMajor,
@versionMinor = VersionMinor, @versionMinor = VersionMinor,
@versionBuild = VersionBuild, @versionBuild = VersionBuild,
@versionRevision = VersionRevision @versionRevision = VersionRevision
FROM SystemSettings; FROM SystemSettings;

IF (@versionMajor <= 1 AND @versionMinor < 1) IF(@versionMajor <= 1 AND @versionMinor <1)
BEGIN 开始
:r "..\\Upgrade\\1.1.0.0.sql" :r“.. \\ Upgrade \\ 1.1.0.0.sql”
END 结束

This will run the 1.1.0.0 script if the database version is lower. 如果数据库版本较低,这将运行1.1.0.0脚本。 The 1.1.0.0 script updates the database version numbers as its final step. 1.1.0.0脚本将数据库版本号更新为最后一步。 Future updates simply require you to add another IF block. 以后的更新仅要求您添加另一个IF块。

暂无
暂无

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

相关问题 Visual Studio 2010和SQL Server 2008脚本和数据库更新的源代码管理工具? - Source Control tools for Visual Studio 2010 and SQL Server 2008 scripts and database updates? 将数据库SQL Server 2008链接到Visual Studio 2010 - Linked database sql server 2008 to visual studio 2010 适用于开发人员的SQL Server 2008和Visual Studio 2010 - SQL Server 2008 and Visual studio 2010 for Developers 使用Visual Studio 2010 Express Edition在SQL Server 2008中将表从一个数据库克隆到另一个数据库 - Clonning of tables from one database to another database in sql server 2008 using Visual Studio 2010 Express Edition 将Microsoft Visual Studio 2010连接到SQL Server 2008 R2数据库 - Connect Microsoft Visual Studio 2010 to SQL Server 2008 R2 database SQL Server 2008和Visual Studio 2010中的命名管道错误 - Named Pipes error in SQL Server 2008 and Visual Studio 2010 在Visual Studio 2010上连接到远程SQL Server 2008 - Connect to remote SQL Server 2008 on Visual Studio 2010 无法将C#SQL CLR数据库项目从Visual Studio 2010部署到SQL Server 2014 - Not able to deploy C# SQL CLR database project from Visual Studio 2010 to SQL Server 2014 将数据库移至新服务器后,无法在Visual Studio 2010的实体框架设计器中看到SQL Server 2008表 - Unable to see SQL Server 2008 tables within entity framework designer of visual studio 2010 after moving database to new server 将数据库添加到我的Visual Studio 2008项目中说我需要sql server express吗? - Adding a database to my Visual Studio 2008 project says I need sql server express?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM