SQL Server 链接数据库别名 [英] SQL Server Linked Database Aliases

查看:22
本文介绍了SQL Server 链接数据库别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能不仅有 LinkedServer,还有链接的数据库和服务器?我的情况是有一个环境设置为:

Is it possible to have not only a LinkedServer, but linked database and server? My situation is that of having one environment with a setup of:

ServerX:DatabaseOne,数据库二

ServerX: DatabaseOne, DatabaseTwo

和另一个环境:

ServerY:MyDatabaseOne,我的数据库二

ServerY: MyDatabaseOne, MyDatabaseTwo

现在,DatabaseOne 和 MyDatabaseOne 完全相同,DatabaseTwo 和 MyDatabaseTwo 也是如此.我有一个存储过程,它在给定的服务器上从一个数据库到另一个数据库进行一些基本的表更新,我希望它能够在两种环境中工作.我已经设置了一个链接服务器,所以我的参考可以是:

Now, DatabaseOne and MyDatabaseOne are exactly the same, as are DatabaseTwo and MyDatabaseTwo. I have a stored procedure that does some basic table updates from one db to the other on a given server, and I want it to be able to work in both evironments. I have set up a linked server so my reference can be:

MyLinkedServer.DatabaseOne.dbo.MyTable 或 MyLinkedServer.MyDatabaseOne.dbo.MyTable

MyLinkedServer.DatabaseOne.dbo.MyTable or MyLinkedServer.MyDatabaseOne.dbo.MyTable

即使这样,当我切换环境以更改数据库名称时,我仍需要更改 SP,有没有办法设置数据库别名,以便我可以编写:

Even with this though, I will need to change the SP when I switch environments to change the db names, is there any way of setting up a Database Alias, so that I can write:

SELECT * FROM MyLinkedServer.MyLinkedDatabase.dbo.MyTable

SELECT * FROM MyLinkedServer.MyLinkedDatabase.dbo.MyTable

这样只要在每个环境中都设置了 LinkedServer 和(如果可能的话)LinkedDatabase,那么在切换环境时 SP 就可以正常工作?

So that as long as LinkedServer and (if possible) LinkedDatabase are set up in each environment, the SP will work without changing when switching environments?

推荐答案

我认为简短的回答是否定的.我认为尚不支持数据库别名.

I think the short answer is no. I don't believe database aliasing is supported yet.

动态 SQL 可以做到.您可以注入数据库名称等并执行您的查询.

Dynamic SQL could do it. You could inject the database name etc and execute your query.

这篇关于SQL Server 链接数据库别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆