1
votes

When I am trying to alter the database collation from case insensitive to case sensitive, I am getting an error as follows:

Msg 5030, Level 16, State 2, Line 1 The database could not be exclusively locked to perform the operation. Msg 5072, Level 16, State 1, Line 1 ALTER DATABASE failed. The default collation of database 'e_mail' cannot be set to SQL_Latin1_General_CP1_CS_AS.

Please help me to fix this

2
Please be aware that changing the database collation will not change the collation of any existing column (only newly created ones) so even when the statement works, you may find it doesn't do what you're expecting. - Jeroen Mostert

2 Answers

1
votes

I'm guessing you still have a bunch of connections using that DB that is preventing it from being locked to alter the collation. Hence this part of your error:

The database could not be exclusively locked to perform the operation

So, you'll need to

  • Schedule some downtime
  • Inform your users
  • Set your DB to single-user mode (to prevent other connections and locks)
  • Then run your alter database statement

Something like...

ALTER DATABASE [e_mail] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; 
GO 

ALTER DATABASE [e_mail] COLLATE SQL_Latin1_General_CP1_CS_AS; 
GO 

ALTER DATABASE [e_mail] SET MULTI_USER; 
GO 
0
votes

If this is a production database, this is a major undertaking and a much more significant change than it appears on the face. It's potentially one of the most complicated changes you can make because it affects every existing string column in the database. If you're still in development, then, honestly, it may be easier to script out the schema and data values and start over in a new database.

Why is this so complicated? Mainly because changing the database collation is not retroactive. It will not change the collation for existing objects (tables, views, udfs, etc.). Instead, you need to modify each object to correct it if it refers to a character field. If there's an index, primary or foreign key, or constraint on a character field, you may need to drop the index, constraint, or key before you can change the collation, and then re-add the object afterwards, too. You should also be sure that applying a case insensitive collation isn't going to create duplicate key values when Ohio and OHIO suddenly become equivalent values.

Additionally, altering the database level setting will not alter the server level collation (that is to say, the collation on the master database). That means that your application DB might not have the same collation as the master and tempdb databases. Plan and test accordingly, as this can cause errors if you use tempdb. You can change the collation on tempdb, but changing the collation on the master database is... not impossible, but it's even less easy than it is changing a user database's collation.

There are several custom SQL scripts out there that contain varying levels of logic to handle this issue, but I wouldn't automatically trust my data to any of them without generating the scripts, reading the entire scripts, and then testing it thoroughly in a test environment.

The Microsoft doc is here. The commands you need to run to answer your question directly and change the default database collation are below. This operation will forcibly terminate all active connections to the database from all other users and rollback all pending transactions.

USE [master]
GO
ALTER DATABASE [e_mail] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [e_mail] SET SINGLE_USER 
GO
ALTER DATABASE [e_mail] COLLATE SQL_Latin1_General_CP1_CS_AS
GO
ALTER DATABASE [e_mail] SET MULTI_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [e_mail] SET MULTI_USER
GO

Why run the SET SINGLE_USER and SET MULTI_USER twice? Simply because in my experience the server will sometimes throw an error after the first time you change the user mode. This may be a bug from earlier versions, an issue with my hardware configuration, or some other reason, but I've kept my scripts the same for many years when I've needed to change the user mode and immediately run a command that requires single user mode, and the redundancy has caused no problem.