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

Posted by codeulike on Stack Overflow See other posts from Stack Overflow or by codeulike
Published on 2011-02-15T15:22:56Z Indexed on 2011/02/15 15:25 UTC
Read the original article Hit count: 416

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.

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about cross-database