Seamless SQLServer Database Name Change

All we need is an easy explanation of the problem, so here it is.

I need to change the name for a few databases, however the databases are used by a lot of applications managed by several different teams. It would take some time for all the applications to finish changing connections and database name references. Is there a way to allow the change to be done seamlessly? Was looking at synonyms to temporarily allow references to the renamed database but unfortunately it is only for database level.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Create an "alias" database with the name you want to use.
Create synonyms for the objects by querying the system tables to create a simple script, here for example for user tables.

select 
  'Create Synonym '
   +quotename(object_schema_name(id, db_id()))+'.'
   +quotename(o.name)+
   ' For [SourceDB].'
   +quotename(object_schema_name(id, db_id()))+'.'
   +quotename(o.name)+';' 
from sysobjects o where type = 'U'

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply