开发者

SQL Server: avoiding hard coding of database name in cross-database views

开发者 https://www.devze.com 2023-02-11 03:23 出处:网络
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.

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 o开发者_如何学Pythonccasionally 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.


You can try using Synonyms. That way you can change what the synonym points to without having the change your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消