[英]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. 这就是我最终做的事情。
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.