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

2.6k views Asked by At

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.

1

There are 1 answers

1
Randy Minder On BEST ANSWER

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