views:

610

answers:

5

In an update project i have to do the following:

Move 3 databases from SQL2000 to SQL2005 and merge them at the same time. There are already quite a few cross database queries used in SP's and Views. The current plan is to move each of the old databases into a separate schema in 1 database.

That means we will also have to change our current SP's and Views, we now have:

SELECT OrderId, OrderDate FROM Sales.dbo.Orders

and expect we will have to change that into

SELECT OrderId, OrderDate FROM Sales.Orders

The question is: how do we do that as automated as possible?

I know about SED and similar for changing the scripts. I would welcome tips about how to be 'smart' about this, like strategies for partitioning the scripts, performance (tons of INSERT INTO lines) etc.

Note: I did look at the Import/Export Wizard but apparently I would have to set the Schema manually on each output table and fix the SP's through ALTER scripts anyway.

+1  A: 

Could you have a dummy database called SALES that has a VIEW called [Orders]:

CREATE VIEW Sales.dbo.Orders
AS
SELECT OrderId, OrderDate, ...
FROM CombinedDatabase.Sales.Orders

and then

SELECT ... FROM Sales.dbo.Order

will still work.

You won't be able to INSERT / UPDATE that table without some further jiggery-pokery though.

If you could have such VIEWs log that they were used that would enable you to fix the code that called them!! but I can't think of a way to do that; however you could disable each in turn, run some tests, fix whatever is broken, then move on to next one ... and thus eradicate them by refactoring, but have a largely working application during the process.

I've used SED for this type of thing, but we have unique names for all our tables and all our columns, and we use variable names within our application that match the database column names - so I would have high confidence that changing xxx_yyy_ID to aaa_bbb_ID in our application would work well, and not have accidental side effects.

If you have actual column/table names like "Sales" and "Orders" I think that something like SED would be risky

Kristen
I'd rather clean house and change and fix all references. Especially since it's 'dbo' we want to get rid of. And no, our table/field names aren't unique enough to make SED fool-proof, we just want to minimize errors.
Henk Holterman
Kristen
I'm not sure this will help. Our new Schema-names (old database-names) are unique enough.
Henk Holterman
Kristen
A: 

I would like to know if it's same kind of data. Any way. I would create a new column with the name 'SourceSystem'. So when the boss comes running after:

" - what was the sales diff between databasesystem1 and db2 in 2004".

Then you can answer that. Then in a year or two, if that questions don't pop up. You can delete that column. Merging data removes the origin of the data.

Flinkman
Could you explain? Google finds 0 hits on "Merging data removes data"
Henk Holterman
ok, what I mean is. You don't know where the data come from. If you mix them. If you do, then there are some kind of questions we can't answer. But you know better. Why you need to smash three databases together.
Flinkman
I think HH's plan is to segregate the original data by having three separate Owners - so three Orders tables owned by A, B and C - representing the original Orders tables formerly in Database-A, Database-B and Database-C
Kristen
Actually, there is little overlap, so we have only one A.Orders table, one b.Customers etc. We are not merging data (tables), only databases with disjunct tables.
Henk Holterman
+2  A: 

You could give Redgate SQL Compare and Data Compare a shot. They have a schema mapping feature that should let you map the dbo schema to the sales schema in another and then move the tables and procs. It would make it so you don't have to mess with the SQL export wizard. You still would have to refactor your other objects though.

I love these two tools.


edit: I think you can get a fully functional demo too.


edit: Additionally, they offer SQL Refactor, which does a 'smart' rename. Score!

Sam
+1  A: 

Ok, so my basic understanding of your problem is something like this:

  1. You have three different databases (i.e. Sales, Manu, Inventory)
  2. They have distinct table & procedure names (no table/proc names in Sales exist in Manu or Inventory)
  3. You want all the tables/procs from all three databases in a single database (i.e. SaleManInv)
  4. Some stored procedures in each database explicitly refer to tables in the other databases (i.e. Sales.dbo.lookupItem() explicitly refers to Inventory.dbo.Items table)

Exporting and importing the tables doesn't seem like it will be a problem, what I would do for the procs:

  1. Export one proc from the SQL Server 2000 db to the SQL Server 2005 DB to determine if you need to get rid of the ".dbo." portion of the cross references.
  2. Export all the procs to text files (same folder for all procs)
  3. Use a text editor with a "Search and Replace in Files" (I use PSPAD) and replace all the "Sales.dbo." with "SaleManInv.dbo.", then all the "Iventory.dbo." with "SameManInv.dbo." etc. to convert all the references to the new db.
  4. Then run the exported and modified procs into your new db.

Is that making any sense? :-)

Ron

Ron Savage
+4  A: 

I did this a couple of years ago, and I ran into a few problems that you want to be aware of.

Assumptions:

  • You've got a single SQL 2000 database server with 3 databases, A/B/C
  • You want all of the objects to end up in SQL 2005 in database A (we'll refer to that as the Target)
  • You want to get rid of databases B and C eventually (the old Sources)
  • You don't have a full-blown test environment where you can automatically restore your production databases every day, and script this again and again until it's right. (That's the best way, and I've taken that approach too, but it's labor-intensive.)

Here's my hard lessons learned:

Don't do the merge and the SQL 2005 change the same day. Either do the merge before you go to 2005, or after, but don't try to accomplish it all in a single outage. It'll be a finger-pointing mess. If it was me, I'd go to 2005 first just to get it out of the way. That way, I know anything that breaks isn't because of a schema change, and those types of breaks are easier to fix. You want at least a week of end user activity on the 2005 box before you declare victory and move on to the merge.

Build the new objects in Target ahead of time. Even if they're not being queried in your live production apps, go ahead and build 'em now. That way you can populate fake test data in there to test your applications ahead of time. Yes, this means mixing live and test data, but frankly, you're already out there working without a net. Be wary of identity fields, though, since you can end up with conflicting records with the same identity number but different data in the Target and Source databases.

Create views in Target ahead of time. You mentioned that you've got views that already do cross-database queries. Copy those from Source to Target now, and tell any other developers (report guys, power users) to start referring to the Target views instead. This isn't going to speed up your own work, but it speeds up THEIR work. If you can get to the point where you can verify that they're only hitting Target (even though the Target views still point to tables in Source) then it'll make troubleshooting easier on migration day. Then you can start denying permissions on the Source views ahead of time.

Sync tables ahead of time. Make a list of all of the tables that need to be moved out of the Sources, and for each one, analyze how it's being updated. If it's only being inserted into (not updated or deleted), like a log table, then write a T-SQL script to start keeping it in sync in Target. Run that script via a SQL Agent job during periods of low activity on your server, like nightly. This way, when it's go-live day, you won't have to push as many records around, meaning your go-live window will be smaller and your Target transaction logs can stay smaller. Tables that are being constantly updated or deleted aren't as easy, and it's up to you whether you decide to sync those as well. We did it for any tables over a million lines.

Check for record conflicts between the Source databases. It sounds like this one doesn't apply to you specifically, but I'm noting it here in case anybody else does a merge and they're reading it for tips. If you have more than one Source database, dump out the list of objects. If you've got two objects with the same name, check their schema. I've worked with instances where they had a State or Region table in each database, and they were supposed to be identical, but they had identity fields for their primary keys. Each child table (like Customers, which linked to a Region table) referred to the parent table (Region) by the primary key (identity field) - which didn't match from one database to the other. In that case, the smart thing to do is take an outage window ahead of time, before the migration day, to clean those records up with manual update scripts.

  1. Disable any constraints or foreign key relationships
  2. Change the identity fields (if they're lookup tables, you may be able to turn off the identity stuff and just run with manually specified pk numbers)
  3. Modify the Region table to add a NewID field, matching to what it's going to become, and an OldID field, showing what it used to be
  4. Update all of the child tables (Customers) to use the NewID number instead of the original
  5. Update the Region table so that the real ID field now has the NewID value, and the OldID field has what the Region used to be. (You're probably going to screw something up like miss a child table you didn't know about, and you're going to wonder what it used to be.)

Break the migration into pieces. List every stored proc in all of the databases. If any of them can be moved without moving data, do that first. For example, if you've got Source.dbo.usp_RunReport, and it only refers to tables in the Target database, then do that in a first phase. If you've got small system lookup tables that are only used internally in your app, not visible to customers or reports, then put that in the first phase too. It sounds like it's too small to bother with, but the idea is to reduce the amount of panic on migration day. The less you wonder about, the better you can troubleshoot. We moved every static lookup table (State, Region, Calendar, etc) over ahead of time. The amount of work required in Phase 1 - just moving those small, static tables - got management to understand how huge it was going to be to move the rest, and it bought us resources and time we wouldn't have gotten otherwise.

Pre-grow the data files for Target. If you're not using SQL 2005's new Instant File Initialization, data file growths take quite a while. Enable Instant File Initialization if you've got a choice, then grow the data files to make sure they're not fragmented. If they just grow naturally during your migration day, they can be fragmented. If you can't use Instant File Initialization, you still need to pre-grow the files, but you want to do that ahead of time during periods of low activity to speed up the maintenance window.

On migration day, run your inserts one table at a time, or smaller. You want to keep your insert transactions as tight as possible. The smaller your insert transactions, the less space you'll need in the transaction log. Remember that the transaction log will grow with insert statements even in simple mode. After every round of inserts, do a sanity check to make sure that they worked, and that you're not going to run out of drive space for data files or t-log files.

After the updates finish, change security on the Source databases. Put every non-SA login into the dbdenydatareader and dbdenydatawriter roles in the Source databases. That way they can still log in if they've hard-coded the database name in the connection string, but they won't be able to do anything. This makes your troubleshooting easier too: if an app or a query runs into problems, you could consider taking their login out of the deny roles and see if it works - if it does, it's borked. The risk with that is that they might run a transaction that uses the Source database data to update the Target database (get customers from Source, update them in Target) and it might cause issues.

Other options for the Source databases are:

  • Rename them, so you can still query 'em but the apps won't touch 'em
  • Detach them, but keep the files available in case you need to troubleshoot
  • Strip out all logins, and use new logins to access the existing databases just in case. Then if somebody's read-only report is totally borked, you can let it work temporarily by issuing them a new login and telling them it's referring to the wrong database.

After the updates finish, rebuild indexes & statistics on Target. If you're just doing continuous inserts, this isn't a big deal, but if you're merging multiple databases (like two Sales databases that had been broken up into regions of the country) then you'll want to clean things up.

IMHO, use one schema unless you can justify a gain from multiple schemas. This last one is just my two cents, but it sounds like you're going through an awful lot of work to go from 3 databases 1 schema each, to 1 database with 3 schemas. If you're not really sure about the 3 schema thing, you might consider using 1 schema - or else you'll be in another messy rework later on down the road. 3 schemas does make sense if you have specific security needs, but otherwise, just make sure you're getting the bang for the buck that you want. Now would be a great time to go to one schema.

Brent Ozar
Brent, thanks for the nce detailed anwer. We;re still planing and preparing and it looks like we can use at least some of your tips.
Henk Holterman
Great, glad I could help! Glad my time on that project can help other people, heh. Was worth it in the end, but boy, was it a long one.
Brent Ozar
Excellent post. Good point on the version change/merge separation. Be aware of the 2000 to 2005 upgrade adviser as well. It's a tool from MS to spot problems beforehand
Sam