SQL Server:避免在跨数据库视图中对数据库名称进行硬编码
因此,假设您在同一台服务器上有两个 SQL Server 数据库,它们在视图、函数和存储过程中相互引用表。
你知道,像这样的事情:(
use database_foo
create view spaghetti
as
select f.col1, c.col2
from fusilli f
inner join database_bar.dbo.conchigli c on f.id = c.id
我知道跨数据库视图不是很好的做法,但只能说你坚持下去)
是否有任何好的技术可以避免对数据库名称进行“硬编码”?
(因此,如果您需要偶尔重新指向不同的数据库 - 也许是为了测试 - 您不需要编辑大量视图、fns、sps)
我对 SQL 2005 或 SQL 2008 解决方案感兴趣。干杯。
So, lets say you have two SQL Server Databases on the same server that reference each others tables in their Views, Functions and Stored Procedures.
You know, things like this:
use database_foo
create view spaghetti
as
select f.col1, c.col2
from fusilli f
inner join database_bar.dbo.conchigli c on f.id = c.id
(I know that cross-database views are not very good practice, but lets just say you're stuck with it)
Are there any good techniques to avoid 'hard coding' the database names?
(So that should you need to occasionally re-point to a different database - for testing perhaps - you don't need to edit loads of views, fns, sps)
I'm interested in SQL 2005 or SQL 2008 solutions. Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用 同义词。这样您就可以更改同义词所指向的内容,而无需更改代码。
You can try using Synonyms. That way you can change what the synonym points to without having the change your code.