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

发布于 2024-09-30 00:41:34 字数 184 浏览 0 评论 0 原文

在 Visual Studio 中,假设您有一个 SQL 2008 数据库项目,并向表中添加一个不可为空的列。当您现在部署数据库时,如果该表中有数据,那么它应该会失败。 (对吗?)您将如何编写自定义逻辑,以便在更新期间可以将此列设置为“x”或使用更高级的查询或游标来更新整个表并填充新列?这是您只想发生一次的事情 - 同时该列被添加到数据库中。有支持吗?

In Visual Studio, say you've got a SQL 2008 Database Project and you add a non-nullable column to a table. 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? 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等你爱我 2024-10-07 00:41:34

我将添加一个可为空的列,运行 UPDATE 命令来填充它,并将该列更改为 NOT NULL。虽然我确实使用 Visual Studio 2010,但我不使用它进行部署。相反,我使用 SQL Compare 进行部署 - 它生成一个 SQL 脚本,很容易调整。

I would add a nullable column, run an UPDATE command to populate it, and alter the column to NOT NULL. Although I do use Visual Studio 2010, I do not use it to deploy. Instead, I deploy with SQL Compare - it generates a SQL script, which is easy to tweak.

小草泠泠 2024-10-07 00:41:34

我是这样做的。

在架构查看器中打开表创建脚本并添加新列,如下所示:

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

因为它们具有*默认值*,所以它们可以成功部署。

然后,在我的安装后脚本中,我检查表中的“ ”值,并运行更新以填充它们(如果有)。或者代码可以修复它。

Here is how I do it.

In the Schema Viewer open the table creation script and add the new columns like so:

[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.

書生途 2024-10-07 00:41:34
update mytable
set newcolumn = 'x'
where newcolumn is null;
update mytable
set newcolumn = 'x'
where newcolumn is null;
§对你不离不弃 2024-10-07 00:41:34

这就是我最终所做的。

  1. 将版本信息添加到数据库(例如,在 SystemSettings 表中)。
  2. 为下一个版本创建脚本(例如1.1.0.0.sql)。
  3. 在 Scripts\Post-Deployment\Script.PostDeployment.sql 文件中,添加以下内容:

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

选择前 1 位
   @versionMajor = VersionMajor,
    @versionMinor = VersionMinor,
    @versionBuild = VersionBuild,
    @versionRevision = VersionRevision
来自系统设置;

IF (@versionMajor <= 1 AND @versionMinor <1)
开始
:r "..\升级\1.1.0.0.sql"
END

如果数据库版本较低,这将运行 1.1.0.0 脚本。 1.1.0.0 脚本更新数据库版本号作为其最后一步。未来的更新只需要您添加另一个 IF 块。

Here's what I ended up doing.

  1. Add version info to the database (e.g. in a SystemSettings table).
  2. Create a script for the next version (e.g. 1.1.0.0.sql).
  3. In the Scripts\Post-Deployment\Script.PostDeployment.sql file, add the following:

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

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

IF (@versionMajor <= 1 AND @versionMinor < 1)
BEGIN
:r "..\Upgrade\1.1.0.0.sql"
END

This will run the 1.1.0.0 script if the database version is lower. The 1.1.0.0 script updates the database version numbers as its final step. Future updates simply require you to add another IF block.

小糖芽 2024-10-07 00:41:34

我对 进行了大量研究这类主题,遗憾的是这似乎是 VSDB 项目所缺乏的领域。开始查找的好地方是 Visual Studio ALM Rangers VSDB 项目指南;他们有一个实践实验室,详细介绍了他们建议的最佳实践,以准确执行您所要求的操作(它涉及使用部署前和部署后脚本将数据复制到瞬态表或从瞬态表复制数据)。此外,Barclay Hill 的博客有一个 与您的问题相关的文章(他是数据工具的高级项目经理),与指南相同。

在我看来,如果您必须能够更新目标数据库的多个不同版本,那么这种推荐的方法似乎具有非常高的维护成本,但如果您只有一个数据库(或模式的一个版本)需要更新,那么它就相当不错了可行的。

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. A good place to start looking is the Visual Studio ALM Rangers guide to VSDB projects; 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文