views:

24

answers:

2

Suppose I have two SQL Server 2008 databases, A and B. They initially were created with the intention to be separate, but over time have grown to both have constant references (in sprocs, views, etc) to each other. It's gotten to the point that they're effectively just two halves of the same database.

So, we're considering merging them. Does anyone know how we could best perform this merger? We have quite a lot of internal applications that reference one or the other, including many that are customer facing so minimizing downtime would be very important. In order to not have to find and update all of the things hitting these databases, we're especially interested in some sort of database 'aliasing', where an application could be calling a sproc in database 'A', and 'A' redirects that to the new merged database 'C', somehow. Does anyone have any experience with or insight into this type of situation?

A: 

Since you're using SQL 2008 you could look at using SYNONYMs, which I believe can go across databases (and even servers if needed). I don't have much experience with them, so I'm afraid I can't give any advice or "gotchas" to consider.

Your ultimate goal should be to eventually change the accessing code to point to the single database and then getting rid of the other two "dummy" DBs. Having this kind of redirection for the long term eventually causes problems in my experience.

Tom H.
A: 

Here's an outline of how I'd approach this. This is a totally non-trivial modification, so your mileage will vary depending upon your actual setup.

(1) Construct the new composite database. I'd take one of the existing two, and add the other's code into it, rather than create a new (third) database.

(1a) You'd (presumably) need a routine to build the new database from scratch.

(1b) You'd (definitely) need a routine to upgrade an existing pair of databases into the single-db form.

(2) Construct a revised "second" database. Everything in this database is a placeholder, referencing the correlated objects in the revised "first" database. As Tom H. recommends, sysnonyms should work well for this (they're available in SQL 2005 as well). Views would work as well. Stored procedures should just be wrappers that call their analogs in the (new) first database.

(3) Test, test test.

(4) Go back and do step 3 again.

(5) Apply your changes all at once to existing systems (this is why we have step 4), one environment at a time. Done properly, and depending a lot on your system, you might not even have to adjust how your databases are accessed by users or applications.

(6) As time and resources allow, revised all outside users and applications to reference the new/single database, ultimately deprecating all usage of or reference to the second database.

Philip Kelley
Does this sound right?1) Start with A and B.2) Create a C database, as a copy of A.3) Create and apply (to C) an 'A->C' patch, adding all of B's code to C, so that C contains A+B.4) Create a D database that contains all the objects from B.5) Create and apply (to D) a 'B->D' patch, which swaps all its code out for thin synonyms/wrappers for the objects in C.5) Test test test, test test test. Repeat.
Chadd Nervig
6) Update A to 'C' using the 'A->C' patch; this is the new single database. Update B to 'D' using the 'B->D' patch; this is just a thin redirect to C, for all the apps that are still making calls to B.7) Eventually, update all the apps making calls to be, to make them directly to A instead. Take B offline.
Chadd Nervig
How would that compare to something like...1) Create a database, C. Fill it with thin synonyms/wrappers to all the objects in A and B.2) Update all the apps to point to C.3) Eventually, really move each object from A to C, and B to C, as time/resources permits, leaving synonyms in their place pointing to C. (Or would this have to be done all at once?)Thanks for all the help so far!
Chadd Nervig
Sorry I didn't see your comments before. Either plan would work, but I'd recommend the first (update existing) over the second (create entirely new, migrate, deprecate old) as the second would overall take longer to do. The sooner you're done with data migrations, the better!
Philip Kelley