简体   繁体   English

SQL Server 2008上的数据库别名

[英]DB Alias on Sql Server 2008

I need to run a migration from my server1 to server2, in both servers have the same DB schema 我需要运行从server1到server2的迁移,因为这两个服务器具有相同的数据库架构

Server1: DB1.dbo... All tables DB2.dbo... All tables Server1:DB1.dbo ...所有表DB2.dbo ...所有表

Server2: DB1.dbo... All tables DB2.dbo... All tables Server2:DB1.dbo ...所有表DB2.dbo ...所有表

I need connect from server1 to server2, and also until I finished this script, I have to run on Local environment and the DB names are different (like DB1_Local instead of DB1) 我需要从server1连接到server2,并且在完成此脚本之前,还必须在本地环境上运行,并且数据库名称不同(例如DB1_Local而不是DB1)

I try to create a synonym for "Server1.DB1.dbo" so when I have to join a table of Server1 with other on Server2 I'll run 我尝试为“ Server1.DB1.dbo”创建一个同义词,因此当我必须将Server1的表与Server2上的其他表联接时,我将运行

MySynonymForServer1.MyTable JOIN MySynonymForServer2.MyTable ON .... MySynonymForServer1.MyTable加入...

But this doesn't work... 但这行不通...

Is there any way to accomplish this? 有什么办法可以做到这一点?

Synonyms are for objects. 同义词是对象的。 You cannot create a synonym for a schema or a database. 您不能为架构或数据库创建同义词。 However, you can create a synonym fo an object in a different database. 但是,您可以在其他数据库中为对象创建同义词。 so in your case, you have to create a synonym for every object in the database. 因此,对于您的情况,您必须为数据库中的每个对象创建一个同义词。 You could automate that by looping through the objects in sys.objects . 您可以通过遍历sys.objects的对象来实现自动化。 Just make sure to filter out unwanted object types like constraints. 只要确保过滤掉不需要的对象类型(如约束)即可。

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

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