The database could not be exclusively locked to perform the operation

When trying to change the collation of a database, you may get the following error message:

The database could not be exclusively locked to perform the operation.

To workaround this problem, we first need to set the database for single user.  And change it back for multi user after changing the collation.

ALTER DATABASE <database_name> SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE <database_name> COLLATE <target_collation_name e.g. SQL_Latin1_General_CP1_CI_AS>
ALTER DATABASE <database_name> SET MULTI_USER