Question : Change collation on many tables/columns - SQL Server 2005

I manage a database which was created on a different server and has been restored on my server.  The server collation on my server is Latin1_General_CI_AS, but the collation of the database in question is SQL_Latin1_General_CP1_CI_AS.  The collation is causing errors in my application so I need to change the collation of the database, all the tables, and all the columns.

I have done some searching and found some examples of changing the collation of a column:

ALTER TABLE MyTable ALTER COLUMN CharCol
            varchar(10)COLLATE Latin1_General_CI_AS NOT NULL
GO

But according to Microsoft (http://msdn.microsoft.com/en-us/library/ms190920(SQL.90).aspx) there are restrictions:

You cannot change the collation of a column that is currently referenced by any one of the following:

    * A computed column
    * An index
    * Distribution statistics, either generated automatically or by the CREATE STATISTICS statement
    * A CHECK constraint
    * A FOREIGN KEY constraint

In the database I need to change there are about 1200 tables, and many thousands of columns.  Many of the columns are either indexed or foreign keys.

I thought of writing a script which has two cursors - one to select the tables, and another to select the columns of the current table - which would go through each table and column and alter the collation.  But what happens to all the columns which are indexed or foreign keys?

Can anyone suggest a reliable way to change the collation on ALL the columns, tables, and the database itself?  

Answer : Change collation on many tables/columns - SQL Server 2005

>> How much down-time are we looking at?  Is it a risky exercise?

Not a risky one..
Totally 10 to 15 minutes. Is that fine for you.

>> Is there a way to reliably drop the indexes, constraints, etc, in such a way that they can be easily (and correctly) re-created?

Yes. We have reliable methods.

>> Are there commercial tools available which will automate the process?

Yes...There are a few tools like Database Publishing Wizard given below. This will generate the scripts of our Entire database along with data. Once you create the scripts, change the collation of the required columns and run your scripts to create the modified database.

http://www.microsoft.com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A&displaylang=en

Steps to do it.

1. First 5 minutes of time to backup your Production database.
2. Restore it in another machine and create the Scripts using your Database Publishing wizard.
3. Change the collation in your Required columns.
4. Run the scripts to create another database.
5. Compare this one with your production database using Comparison tools like

a. ApexSQL Diff (http://www.apexsql.com/sql_tools_diff.asp) - to compare DDL Changes.
b. ApexSQL Data Diff (http://www.apexsql.com/sql_tools_datadiff.asp) - to compare Data changes.

6. Apply any changes if you need.
7. Restore your Production database once this is completely done. ( another 5 minutes to restore)

Hope this helps.
Random Solutions  
 
programming4us programming4us