重新初始化订阅时如何避免表丢失(复制)

发布于 2024-12-20 03:40:45 字数 110 浏览 2 评论 0原文

在复制数据库中,我在每个表中添加额外的列。每次我重新初始化订阅时,所有表都会被删除,并且我会丢失添加的列,因此我必须再次添加它。是否可以避免删除表,以便复制过程使用数据库中的现有数据?

谢谢

In replicated database I add extra column into each table. Every time I reinitialize subscription all tables are dropped and I loose column that I added so I have to add it again. Is it possible to avoid dropping table so that replication process uses existing data in db?

Thanks

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

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

发布评论

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

评论(2

何以笙箫默 2024-12-27 03:40:45

您没有说明您正在使用什么类型的复制 - 是快照、事务、合并还是点对点?

无论哪种类型,对您问题的简短回答是否定的;在复制快照中,订阅者上的表架构必须与发布者匹配(或者,准确地说,快照中包含的发布者上的文章中筛选出的列列表)。

如果架构不匹配,则尝试应用快照将会失败。

有几种方法可以解决这个问题:

如果您需要保留链接到发布者行的订阅者信息,最简单的方法是将该信息存储在订阅者的另一个表中,并有一列将其链接到主订阅者复制表的键。请注意,您将无法使用外键强制执行此关系,因为这样做会阻止应用新快照时删除或清除订阅者上的表项目。

另一种(并且更复杂)的方法是修改发布以执行一个预快照脚本,该脚本重命名订阅者上的表,以及一个后快照脚本,该脚本创建新列,从重命名的附加列复制数据table 到新创建的表,然后删除重命名的表。有关详细信息,请参阅 sp_addpublication 的文档 ( @pre_snapshot_script@post_snapshot_script 参数)

You don't say what type of replication you're using - is it snapshot, transactional, merge or peer-to-peer?

Regardless of the type the short answer to your question is no; in a replication snapshot, the schema of the table on the subscriber must match the publisher (or, to be accurate, the filtered column list from the article on the publisher included in the snapshot)

If the schemas do not match, attempts to apply the snapshot will fail.

There are a couple of ways around this:

If you need to keep information on the subscriber which links to rows on the publisher, the simplest way would be to store that information in another table on the subscriber with a column which links it to the primary key of the replicated table. Note that you won't be able to enforce this relationship with a foreign key, because doing so will prevent the table article on the subscriber being dropped or cleared down when a new snapshot is applied.

An alternative (and more complex) method would be to amend the publication to execute a pre-snapshot script which renamed the table on the subscriber and a post-snapshot script which created the new column, copied the data from the additional column on the renamed table to the newly created table, then dropped the renamed table. See the documentation for sp_addpublication for details (@pre_snapshot_script and @post_snapshot_script parameters)

百变从容 2024-12-27 03:40:45

考虑到您想要添加一列,您需要在发布数据库上执行此操作,并将其传播到订阅者,并将发布属性 @replicate_ddl 设置为 true。不需要重新初始化。

您需要使用 ALTER TABLE 来完成此操作表>添加<发布者上的 Column > 语法。

请查看对发布数据库进行架构更改

Considering you're wanting to add a column, you'll want to do this on the publication database and have it propagate to the subscriber(s) with the publication property @replicate_ddl set to true. Reinitialization is not required.

You'll want to do this using ALTER TABLE < Table > ADD < Column > syntax on the Publisher.

Have a look at Making Schema Changes on Publication Databases.

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