Sometimes using SYNONYM is a good strategy:
CREATE SYNONYM [schema.]name FOR [[[linkedserver.]database.]schema.]name
Then refer to the object by its synonym in the stored procedure.
Changing where synonym points are is a matter of dynamic SQL, but then your main stored procedures can be fully dynamic SQL-free. Create a table to manage all the objects you need for the link and the stored procedure that switches all the necessary synonyms into the correct context.
This feature is only available in SQL Server 2005 and later.
This method is NOT suitable for frequent switching or for situations where different connections must use different databases. I use it for a database that occasionally moves between servers (it can run in the prod database or in the replication database, and they have different names). After restoring the database in her new home, I launch my switcheroo SP switch and everything works after about 8 seconds.
source share