Search

Make it easy to change collation on a database by Erland Sommarskog

Active

68
0
Sign in
to vote
Type: Suggestion
ID: 358320
Opened: 7/28/2008 1:49:09 PM
Access Restriction: Public
0
Workaround(s)
It's great to be able to have different collations on the same server, and even in a
different columns. But in many cases, you are not interested in having a multi-collation
server, but you only want a single collation. I have submitted several other suggestions
to minimise the impact and the risk for it to happen. However, even if all these are implemented, there will still be users sitting with a datbase that for one reason or another have the wrong collation. Today they are very much left on their own. You can change the
database collation with ALTER DATABASE, but it only changes the collation for metadata, and the restrictions precludes it from being usable for a database of any complexity. (What?
CHECK constraints not permitted?)

One way would be put this a tool of some sort, for instance the Copy Database Wizard.
However, it's hardly any secret that I have don't any high regards for CDW. I think this
should be in the engine.
Details (expand)
Product Language
English

Category

SQL Engine

Proposed Solution

The first step would be to lift the current restrictions of ALTER DATABASE COLLATE. That is, CHECK constraints, computed columns and schemabound functions and view would be reparsed as part of the transaction, which would roll back in case of error. Table-valued functions is somewhat more problematic, but they should probably be recompiled too have their columns changed.

The next step would be to add a clause COLLATE WITH DATA. When this clause is in effect, ALTER DATABASE would update the collation of columns with the database collation(*)
in the metadata. All indexes involving these columns would be disabled, and all constraints involving these columns would be marked as not trusted. At this point ALTER DATABASE would commit its transaction and complete. Thus the database
would be left in a more or less unusable state.

The last step would be supply a stored procedure that rebuilds all disabled indexes, and checks all untrusted constraints. This procedure could embed ALTER DATABSE COLLATE WITH DATA_CHANGE. This procedure would continue even if there are errors and report all errors.

The reason for this two-step operation, is that if ALTER
DATBASE did all in one transaction, the log could grow enormously. The database could also be offline for a very
long time, and no one would like think of what would happen if the last index fails because of a duplicate in the new collation.

Since COLLATE WITH DATA_CHANGE would be quite a dangerous operation, it would need some safeguards, like requiring
single-user mode. Even strong serious safe-guards would be to put this in RESTORE or CREATE FOR ATTACH.



(*) Or you could track whether column was created with an explicit collation, and only change those that were not.

Benefits

Improved Administration

Other Benefits

 
File Attachments
0 attachments
Sign in to post a comment.
Posted by AussieJohn on 5/8/2012 at 5:48 PM
Any chance of changing "ALTER DATABASE ... COLLATE ..." to have three options?
1. NEW
the current behaviour - new columns have the new collation
2. REPORT
change database collation and report columns that now have different collation
3. ALL
change database collation and all column collations in that database, dealing with any pesky constraint issues.

Many users expect ALTER DATABASE COLLATE to change the collation database wide. It certainly would help many DBAs and organisations to be able to do this.
Posted by Josh Gallagher on 4/16/2012 at 7:18 AM
I've just tried out the ALTER DATABASE on SQL Server 2012 RTM and the issue still exists. Any computed columns in the database still prevent the database collation from being changed.
Posted by Geir Andre on 2/1/2012 at 2:24 AM
Hi, will this be a feature of the SQL Server 2012?
Posted by Microsoft on 8/8/2008 at 11:02 AM
Hello,

Thank you for sharing your ideas with us. We are aware of this limitation of SQL Server 2008 and we're considering fixing it in the next release.

Thanks,
Krzysztof Kozielczyk
Sign in to post a workaround.