Click here to Skip to main content
14,491,120 members

SQL Server 2000 Collation Changer

Rate this:
4.88 (99 votes)
Please Sign up or sign in to vote.
4.88 (99 votes)
3 Mar 2008CPOL
Change collation order for all text columns in a database

Introduction

Do you have an SQL application that you need to deploy in another country? Do you need to change the collation of your SQL database and all objects in it? If the answer is yes, then this could be a very slow process to do manually. If you run the alter database script as below:

ALTER DATABASE [My_Database] COLLATE My_Collation

You will find that the database default collation is changed for new columns, but all existing columns will retain the original collation order. Changing the collation on each column is a non-trivial task, as you need to drop all indexes, full text indexes and constraints associated with the column, along with any user-defined functions. Once the collation order has been changed, you can recreate the indexes, constraints and functions. This C# tool simplifies the process by creating an SQL script that does everything for you.

Using the Code

Simply run the program, select your SQL Server database and a new collation order. You have two options: you can either simply get the program to create an SQL script that you can run later (press the "Script Only" button) or you can actually make the changes (press the "Script and Execute" button). Things to note:

  • Always back up your database before running this tool. I cannot guarantee that you will not lose data. A number of the statements cannot be run in a transaction, so there is no way of detecting a failure and rolling back.
  • On SQL Server 2000, nText columns will be recreated so your column order will be slightly different.
  • To run the script, the program sets the database into single user mode to run the ALTER DATABASE [db_name] COLLATE [Collation Name] statement. You should therefore ensure that there are no open connections on the database before running the script (use the stored procedure SP_WHO to identify any open connections).
  • Since the script will drop and then rebuild all indexes and foreign keys in the database, you will find that it could take a long time to complete (possibly hours).
  • All columns will be changed to the new collation order, even if they have a non-default collation before running the script.
  • SQL 2005 support is gradually being added. If you encounter issues or missing functionality, please let me know.
  • If you change from a case-insensitive to a case-sensitive collation order, you may find errors occurring when recreating check constraints and functions. This is because your scripts will be parsed in a case-sensitive manner once the collation order has been changed. To work around this, I would recommend running the script in the program and reviewing the output once the script has run to completion. The error messages relating to each failure will be displayed in red under the code that failed.

Change History

18 January 2006

  • Original posting.

7 March 2006

  • Fixed bug where scripting failed on databases with case-sensitive collation orders. Script no longer drops foreign key constraints unless necessary.

30 August 2006

  • Fixed bug when scripting objects owned by different owners than the owner executing the script are used. May be seen as an error when scripting to #spindtab_____.
  • Fixed bug where scripting did not recreate permissions on table functions after they were recreated.

20 March 2007

  • Triggers now disabled while changes are made.
  • Altered sequence of execution to prevent errors following user feedback.
  • Added some SQL 2005 support. Let me know if you encounter any issues.
  • Resolved issue when recreating a table function where the body of a table function is greater than 4000 characters.
  • Reinstated the functionality to only delete the required indexes and primary keys.

10 October 2007

  • Added support for changing collation where full text indexes exist, including changing language used in the full text search.
  • Fixed issue when insteadof triggers exist on views.
  • Fixed issue where nText columns end up allowing nulls when they were originally declared NOT NULL.
  • SQL 2005 allows collation order of nText columns to be modified. The script has been modified to reflect this new functionality.
  • Fixed issues encountered when user-defined data types are used.
  • Added additional SQL 2005 support, including refactoring scripting logic to allow various parts of the script to be customised for different versions of SQL server. If you need to debug or customise the scripts, it should be a little easier to understand.
  • Converted from VB.NET to C#, as I use C# at work all the time and it is more familiar to me.
  • Moved execution task to a worker thread to give a more responsive UI.
  • Fixed issues when system databases have case-sensitive sort order.

1 March 2008

  • Created new code for handling indexes, statistics and relationships in SQL 2005. This new code uses the 2005 schema views and adds scripting support for new SQL 2005 functionality, such as included columns in non-clustered indexes.
  • Fixed issues where ANSI_NULLS setting is not correct after recreation of table functions.
  • Various minor UI bugs fixed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Alex Baker
Software Developer RXP Services
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralCheers !! Pin
Paul Bibby24-Apr-08 16:56
MemberPaul Bibby24-Apr-08 16:56 
GeneralNice One Pin
Nikita_0123-Jan-08 18:26
MemberNikita_0123-Jan-08 18:26 
QuestionGreat Job!!!! Pin
imsathy15-Jan-08 16:46
Memberimsathy15-Jan-08 16:46 
GeneralRe: Great Job!!!! Pin
Kishore.P15-Jan-08 17:59
MemberKishore.P15-Jan-08 17:59 
GeneralCalculated fields fail to be altered Pin
icemokka19-Dec-07 20:48
Membericemokka19-Dec-07 20:48 
GeneralString or binary data would be truncated Pin
Mikevou7-Dec-07 3:01
MemberMikevou7-Dec-07 3:01 
GeneralRe: String or binary data would be truncated Pin
Alex Baker27-Jan-08 22:10
MemberAlex Baker27-Jan-08 22:10 
QuestionLove your program! Pin
Camilla!26-Oct-07 6:38
MemberCamilla!26-Oct-07 6:38 
Hi,

I'm going to change collation on 30 databases, and your program have saved me from hours of work... Thank you for sharing this fantastic programSmile | :)

I'm having some problems while generating the srcript on curtain databases, and hope you can help me find out why!

--script drop of indexes-we will also populate a temp table that helps recreate the indexes later
create table #spindtab
(....)

--generate sql to do indexes
declare @ix_indid smallint, --the index id of an index
@ix_groupid smallint,.....

Cannot insert the value NULL into column 'groupname', table 'tempdb.dbo.#spindtab_______________________________________________________000000001A19'; column does not allow nulls....
The statement has been terminated.

Do you which to continue running the script anyway?


Do you know why this error occur?


Camilla Smile | :)

AnswerRe: Love your program! Pin
Alex Baker28-Oct-07 22:27
MemberAlex Baker28-Oct-07 22:27 
GeneralRe: Love your program! Pin
Camilla!30-Oct-07 3:49
MemberCamilla!30-Oct-07 3:49 
GeneralOne more question... Pin
Camilla!1-Nov-07 1:56
MemberCamilla!1-Nov-07 1:56 
GeneralAwesome work Pin
inharry16-Oct-07 12:56
Memberinharry16-Oct-07 12:56 
Generalsql errors Pin
Marc Gravell16-Oct-07 2:53
MemberMarc Gravell16-Oct-07 2:53 
GeneralRe: sql errors Pin
Marc Gravell16-Oct-07 2:54
MemberMarc Gravell16-Oct-07 2:54 
GeneralRe: sql errors Pin
Alex Baker16-Oct-07 3:20
MemberAlex Baker16-Oct-07 3:20 
GeneralRe: sql errors Pin
Marc Gravell16-Oct-07 3:43
MemberMarc Gravell16-Oct-07 3:43 
GeneralRe: sql errors Pin
Marc Gravell16-Oct-07 3:45
MemberMarc Gravell16-Oct-07 3:45 
GeneralGarbage portions in generated script Pin
pjotr_b16-Oct-07 2:41
Memberpjotr_b16-Oct-07 2:41 
GeneralRe: Garbage portions in generated script Pin
Alex Baker16-Oct-07 2:48
MemberAlex Baker16-Oct-07 2:48 
GeneralNULL error while generating script Pin
pjotr_b16-Oct-07 2:28
Memberpjotr_b16-Oct-07 2:28 
GeneralRe: NULL error while generating script Pin
Alex Baker16-Oct-07 2:44
MemberAlex Baker16-Oct-07 2:44 
GeneralRe: NULL error while generating script Pin
pjotr_b16-Oct-07 3:04
Memberpjotr_b16-Oct-07 3:04 
GeneralRe: NULL error while generating script Pin
pjotr_b16-Oct-07 3:56
Memberpjotr_b16-Oct-07 3:56 
GeneralCOLLATE Clause cannot be used on user-defined data types Pin
SoundBastard15-Oct-07 22:01
MemberSoundBastard15-Oct-07 22:01 
GeneralRe: COLLATE Clause cannot be used on user-defined data types Pin
Alex Baker15-Oct-07 23:05
MemberAlex Baker15-Oct-07 23:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 17 Jan 2006

Stats

566.8K views
9.6K downloads
101 bookmarked