7
votes

I hate case sensitivity in databases, but I'm developing for a client who uses it. How can I turn on this option on my SQL Server, so I can be sure I've gotten the case right in all my queries?

4

4 Answers

14
votes

You don't actually need to change the collation on the entire database, if you declare it on the table or columns that need to be case-sensitive. In fact, you can actually append it to individual operations as needed.

SELECT name WHERE 'greg' = name COLLATE Latin1_GENERAL_CS_AS

I know, you said that you want this to apply throughout the database. But I mention this because in certain hosted environments, you can't control this property, which is set when the database is created.

9
votes

How about:

ALTER DATABASE database_name COLLATE collation_name

See BOL for a list of collation options and pick the case-sensitive one that best fits your needs (i.e. the one your client is using).
Obviously, it's probably a good idea to make a full backup of your database before you try this. I've never personally tried to use a database with a different collation than the server's default collation, so I don't know of any "gotchas". But if you have good backups and test it in your environment before deploying it to your client, I can't imagine that there's much risk involved.

1
votes

If you have a DB that has a different collation to the instance default, you can run into problems when you try and join your tables with temporary ones. Temporary tables have to collation of the instance (because they're system objects) so you need to use the COLLATE database_default clause in your joins.

select temp.A, table.B
from #TEMPORARY_TABLE temp inner join table
on temp.X COLLATE database_default = table.Y

This forces the collation of temp.X (in this example) to the collation of the current DB.

0
votes

You'll have to change the database collation. You'll also need to alter the table and column level collation. I beleive you can find a script out there if you google it.