简体   繁体   English

将 SQL Server 2016 表移动到另一个数据库并使用原始名称使用同义词访问它

[英]Move a SQL Server 2016 table to another database and access it with a synonym using the original name

For integration purposes I want to move a table to a different database B ( on the same server ) and then create a synonym of the table on the original database A so that all the objects referring to the table can still work as before the move.出于集成目的,我想将表移动到不同的数据库 B(在同一台服务器上),然后在原始数据库 A 上创建该表的同义词,以便引用该表的所有对象仍然可以像移动前一样工作。 The problem I'm having is that to create the synonym I first have to delete the local table which It's not easy as it has several objects depending on it.我遇到的问题是要创建同义词,我首先必须删除本地表,这并不容易,因为它有几个依赖于它的对象。 Is there an easier way other than recreating ALL the depending objects to achieve this?除了重新创建所有依赖对象来实现这一点之外,还有更简单的方法吗? In other words is there a way to "turn off the dependency check", drop the table, create the synonym and ... reactivate the dependencies which now find the synonym?换句话说,有没有办法“关闭依赖项检查”,删除表,创建同义词并...重新激活现在找到同义词的依赖项?

Hope it makes sense, thanks!希望有道理,谢谢!

I'm afraid I have to drop this idea.恐怕我不得不放弃这个想法。 Probably "converting" the table into a synonym could be done by first dropping all the foreign keys, deleting the table and then creating the synonym;可能可以通过首先删除所有外键,删除表然后创建同义词来将表“转换”为同义词; after this views referring to the table would work fine reading data from the other database through the synonym BUT I could not recreate the foreign keys as I now learn that you can not create foreign keys on a synonym ( similarly Cross database foreign keys are not supported ).在此引用表的视图可以通过同义词从其他数据库中读取数据后,我无法重新创建外键,因为我现在了解到您不能在同义词上创建外键(同样不支持跨数据库外键)。 due to this my whole idea is not gonna work and I will have to look into merging the two databases.因此,我的整个想法行不通,我将不得不考虑合并两个数据库。

For sake of details: the table contains user accounts which need to be shared between two systems using two different databases;详细说明:该表包含需要在使用两个不同数据库的两个系统之间共享的用户帐户; both systems must be able to add, update and delete user accounts and both systems have several tables which are referring to user records and which need a way to check referral integrity, foreign keys are required.两个系统都必须能够添加、更新和删除用户帐户,并且两个系统都有几个表,这些表引用用户记录并且需要一种方法来检查引用完整性,需要外键。

thanks for the attention.感谢关注。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM