Define Collation: Collation is a way of recognizing
different languages, their ordering the characters and accents. It determines
how the data is sorted and compared. There are following collation options
provided by SQL Server to support it:
- Case Sensitivity:
- Accent Sensitivity:
- Kana Sensitivity:
- Width Sensitivity:
Collation can be specified at following
levels in any SQL Server:
- Server Level
- Database Level
- Column Level of a database table
Changing Server Level Collation:
There are two approaches to change Server
Level Collation:
- Documented approach (supported by Microsoft):
- Undocumented approach (not supported by Microsoft):
In the undocumented approach of server
collation change, following are the steps:
- Detach all the databases of SQL Server
- Take backup of SQL Jobs, logins and users if required.
- Stop the service of SQL Server
- Execute following command to change the collation: sqlservr -m -T4022 -T3659 -s"SQL2017" –q "SQL_Latin1_General_CP1_CI_AI"
- After execution of above command, after successfully collation change you would see following message:
- “The default collation was successfully changed.”
- After getting above message, user can stop further processing by pressing Ctrl+C button.
- Start the Service of SQL Server, which was stopped in step 3.
- Check the server collation now, it should be changed to SQL_Latin1_General_CP1_CI_AI
- Attach the databases detached in step 1.
- Check SQL Jobs,
logins, users and other artifacts if they are there and behaving the same way
as it was before the collation change operation.
Automating undocumented approach of
collation change using powershell
Algorithm with following steps can be
implemented:
- Create a SQL Script to detach and attach all the databases of SQL Server. Execute the detach script using powershell.
- Stop the service of SQL Server using below command:
- STOP-SERVICE -NAME $SQLServiceName –Force
- Get the location of SqlServr.exe file and execute command to change the collation:
- $CollationChangeProcess=Start-Process -FilePath $SQLRootDirectory -ArgumentList "-c -m -T 4022 -T 3659 -s $JustServerInstanceName -q $NewCollationName" -NoNewWindow –passthru
- Using loop check the event viewer application logs for SqlServr.exe continuously, to check when it generates following log message: The default collation was successfully changed.
- $log=Get-WinEvent -FilterHashtable @{logname='application'; providername=$SQLServiceName; starttime = $StartDateTime} | Where-Object -Property Message -Match 'The default collation was successfully changed.'
- Once the log message is generated, kill the process using below command:
- Stop-Process -ID $CollationChangeProcess.ID
- Start the service of SQL Server using below command:
- START-SERVICE -NAME $SQLServiceName
- Execute the attach script using powershell.
I have posted additional information here:
https://stackoverflow.com/questions/58337969/change-instance-level-collation-of-sql-server-using-powershell?noredirect=1#comment103033939_58337969
https://stackoverflow.com/questions/58337969/change-instance-level-collation-of-sql-server-using-powershell?noredirect=1#comment103033939_58337969