Home
%3CLINGO-SUB%20id%3D%22lingo-sub-369119%22%20slang%3D%22en-US%22%3EChange%20collation%20of%20existing%20Azure%20SQL%20database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-369119%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Nov%2009%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20When%20you%20are%20creating%20a%20new%20database%20you%20can%20change%20the%20collation%20in%20the%20portal%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89074i6B492E0FC132DDEF%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Or%20else%20using%20TSQL%20(%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fcollations%2Fset-or-change-the-database-collation%3Fview%3Dazuresqldb-current%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fcollations%2Fset-or-change-the-database-collation%3Fview%3Dazuresqldb-current%20%3C%2FA%3E%20)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20However%20there%20is%20no%20easy%20way%20at%20this%20moment%20to%20change%20an%20existing%20database%20collation.%20The%20workaround%20is%20to%20use%20the%20BACPAC%20that%20can%20be%20used%20like%20the%20sample%20below%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20UPDATE%202019-02-06%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Just%20got%20an%20information%20from%20the%20product%20group%20that%20there%20is%20an%20easier%20way%20to%20do%20this%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20We%20added%20a%20%2FModelFilePath%20command%20line%20parameter%20to%20sqlpackage%20to%20override%20the%20model.xml.%20This%20should%20simplify%20the%20process%20steps%20to%20avoid%20the%20re-compute%20the%20hash%20in%20the%20origin.xml%2C%20and%20re-compressing%20the%20.bacpac%20which%20results%20in%20corruption%20for%20large%20files.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20So%20the%20steps%20are%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201.%20Make%20you%20have%20a%20modern%20version%20of%20sqlpackage%20that%20support%20the%20%2FModelFilePath%20parameter%20(we%20added%20it%20about%20a%20year%20ago).%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3ELatest%20sqlpackage%20version%20is%20%3CA%20href%3D%22https%3A%2F%2Femea01.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fwww.microsoft.com%252Fen-us%252Fdownload%252Fdetails.aspx%253Fid%253D57784%26amp%3Bdata%3D02%257C01%257CSergio.Fonseca%2540microsoft.com%257Ccb9c0eb0f41e46ef9bfb08d68ba250b4%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636849926598519464%26amp%3Bsdata%3D09FQgOAjL3YjhwruqlC2i629HLJWfw9RLG4NpMFTYJ8%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3ESqlpackage%20installs%20to%20the%20C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5C150%5CDAC%5Cbin%20directory.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%202.%20Open%20the%20.bacpac%20file%20using%20winzip%20or%207-zip%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203.%20Copy%20the%20model.xml%20to%20a%20local%20folder%20%E2%80%9CC%3A%5CTemp%5Cmodel.xml%E2%80%9D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204.%20Edit%20the%20%E2%80%9CC%3A%5CTemp%5Cmodel.xml%E2%80%9D%20with%20the%20desired%20collation.%20For%20example%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20From%3A%20%3CPROPERTY%20name%3D%22%26quot%3BCollation%26quot%3B%22%20value%3D%22%26quot%3BSQL_Latin1_General_CP1_CI_AS%26quot%3B%22%3E%3C%2FPROPERTY%3E%20%3CBR%20%2F%3E%20To%3A%20%3CPROPERTY%20name%3D%22%26quot%3BCollation%26quot%3B%22%20value%3D%22%26quot%3BLatin1_General_BIN%26quot%3B%22%3E%3C%2FPROPERTY%3E%20%3CBR%20%2F%3E%205.%20Run%20the%20import%20using%20sqlpackage.exe%2C%20but%20use%20the%20%2FModelFilePath%3AC%3A%5CTemp%5Cmodel.xml%20parameter%20to%20%3CSTRONG%3E%20override%20the%20model.xml%20%3C%2FSTRONG%3E%20in%20the%20.bacpac.%20For%20example%3A%20%3CBR%20%2F%3E%20sqlpackage.exe%20%2FAction%3AImport%20%2Ftsn%3A%5Bserver%5D.database.windows.net%20%2Ftdn%3A%5Bdatabase%5D%20%2Ftu%3A%5Buser%5D%20%2Ftp%3A%5Bpassword%5D%20%2Fsf%3A%22C%3A%5CTemp%5Cdatabase.bacpac%22%20%3CSTRONG%3E%20%2FModelFilePath%3AC%3A%5CTemp%5Cmodel.xml%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Below%20you%20can%20find%20the%20old%20way%2C%20from%20the%20original%20post%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201%20-%20Export%20the%20database%20to%20.bacpac%20file%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-export%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-export%20%3C%2FA%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%202%20-%20Rename%20it%20to%20.ZIP%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203%20-%20Unzip%20in%20a%20folder%2C%20you%20are%20going%20to%20see%20files%20like%20below%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89075iE919838E74A90D01%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204%20-%20Alter%20the%20file%20%3CSTRONG%3E%20model.XML%20%3C%2FSTRONG%3E%20%2C%20with%20the%20requested%20default%20collation%20for%20the%20database%20%3CBR%20%2F%3E%20From%3A%20%3CPROPERTY%20name%3D%22%26quot%3BCollation%26quot%3B%22%20value%3D%22%26quot%3B%22%3E%3CSTRONG%3E%20SQL_Latin1_General_CP1_CI_AS%20%3C%2FSTRONG%3E%20%22%20%2F%26gt%3B%20%3CBR%20%2F%3E%20To%3A%20%3CPROPERTY%20name%3D%22%26quot%3BCollation%26quot%3B%22%20value%3D%22%26quot%3B%22%3E%3CSTRONG%3E%20Latin1_General_BIN%20%3C%2FSTRONG%3E%20%22%20%2F%26gt%3B%20%3CBR%20%2F%3E%205%20-%20You%20can%20also%20check%20also%20fields%20that%20have%20non%20default%20collations%20%3CBR%20%2F%3E%20%3CELEMENT%20type%3D%22%26quot%3BSqlSimpleColumn%26quot%3B%22%20name%3D%22%26quot%3B%5Bdbo%5D.%5BTable_1%5D.%5Banothercollation%5D%26quot%3B%22%3E%20%3CBR%20%2F%3E%20%3CPROPERTY%20name%3D%22%26quot%3BCollation%26quot%3B%22%20value%3D%22%26quot%3B%22%3E%3CSTRONG%3E%20SQL_Latin1_General_CP1_CS_AS%20%3C%2FSTRONG%3E%20%22%20%2F%26gt%3B%20%3CBR%20%2F%3E%206%20-%20Alter%20the%20file%20%3CSTRONG%3E%20origin.xml%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EYou%20are%20going%20to%20find%20a%20checksum%20like%20the%20below%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CCHECKSUMS%3E%20%3CBR%20%2F%3E%20%3CCHECKSUM%20uri%3D%22%26quot%3B%2Fmodel.xml%26quot%3B%22%3E35EF75437D2087E0FB029E5D726B229B973CFB0EFE75D014E74214C468976D51%3C%2FCHECKSUM%3E%20%3CBR%20%2F%3E%20%3C%2FCHECKSUMS%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EYou%20need%20to%20recalculate%20it%20using%20a%20Powershell%20script.%20%3CSTRONG%3E%20Make%20sure%20to%20change%20the%20file%20path%20%3C%2FSTRONG%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%24modelXmlPath%20%3D%20%22%20%3CSTRONG%3E%20C%3A%5CFILEPATH%5Cmodel.xml%20%3C%2FSTRONG%3E%20%22%20%3CBR%20%2F%3E%20%24hasher%20%3D%20%5BSystem.Security.Cryptography.HashAlgorithm%5D%3A%3ACreate(%22System.Security.Cryptography.SHA256CryptoServiceProvider%22)%20%3CBR%20%2F%3E%20%24fileStream%20%3D%20new-object%20System.IO.FileStream%20-ArgumentList%20%40(%24modelXmlPath%2C%20%5BSystem.IO.FileMode%5D%3A%3AOpen)%20%3CBR%20%2F%3E%20%24hash%20%3D%20%24hasher.ComputeHash(%24fileStream)%20%3CBR%20%2F%3E%20%24hashString%20%3D%20%22%22%20%3CBR%20%2F%3E%20Foreach%20(%24b%20in%20%24hash)%20%7B%20%24hashString%20%2B%3D%20%24b.ToString(%22X2%22)%20%7D%20%3CBR%20%2F%3E%20%24fileStream.Close()%20%3CBR%20%2F%3E%20%24hashString%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EChange%20the%26nbsp%3Bfile%20%3CSTRONG%3E%20origin.xml%20%3C%2FSTRONG%3E%20with%20the%20%3CSTRONG%3E%20new%20hash%20%3C%2FSTRONG%3E%20and%20save%20it%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%207%20-%20Zip%20the%20files%20again%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EMake%20sure%20not%20to%20zip%20the%20folder%20where%20you%20unzipped%20the%20bacpac%2C%20only%20the%20files%20inside%20it%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89076i90716A524954A9BC%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%208%20-%20Rename%20the%20%3CSTRONG%3E%20zip%20%3C%2FSTRONG%3E%20to%20DBNAME.%20%3CSTRONG%3Ebacpac%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%209%20-%20Do%20the%20import%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%2010%20-%20You%20can%20check%20the%20collation%20in%20the%20fields%20with%20this%20query%20%3CBR%20%2F%3E%20SELECT%20T.name%2C%20C.name%2C%20C.collation_name%20%3CBR%20%2F%3E%20FROM%20sys.tables%20T%20%3CBR%20%2F%3E%20INNER%20JOIN%20sys.columns%20C%20%3CBR%20%2F%3E%20ON%20T.object_id%20%3D%20C.object_id%20%3CBR%20%2F%3E%20WHERE%20C.collation_name%20IS%20NOT%20NULL%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I%20used%20as%20a%20reference%20the%20information%20from%20this%20other%20article%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fazuresqldbsupport%2F2017%2F08%2F16%2Fediting-a-bacpac-file%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fblogs.msdn.microsoft.com%2Fazuresqldbsupport%2F2017%2F08%2F16%2Fediting-a-bacpac-file%2F%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FPROPERTY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-369119%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Nov%2009%2C%202018%20When%20you%20are%20creating%20a%20new%20database%20you%20can%20change%20the%20collation%20in%20the%20portalOr%20else%20using%20TSQL%20(https%3A%2F%2Fdocs.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-369119%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ebacpac%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ecollation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Edatabase%20collation%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FELEMENT%3E%3C%2FPROPERTY%3E%3C%2FPROPERTY%3E%3C%2FLINGO-BODY%3E
Not applicable
First published on MSDN on Nov 09, 2018
When you are creating a new database you can change the collation in the portal



Or else using TSQL ( https://docs.microsoft.com/en-us/sql/relational-databases/collations/set-or-change-the-database-... )



However there is no easy way at this moment to change an existing database collation. The workaround is to use the BACPAC that can be used like the sample below



UPDATE 2019-02-06



Just got an information from the product group that there is an easier way to do this

We added a /ModelFilePath command line parameter to sqlpackage to override the model.xml. This should simplify the process steps to avoid the re-compute the hash in the origin.xml, and re-compressing the .bacpac which results in corruption for large files.

So the steps are:

1. Make you have a modern version of sqlpackage that support the /ModelFilePath parameter (we added it about a year ago).

  • Latest sqlpackage version is here .

  • Sqlpackage installs to the C:\Program Files\Microsoft SQL Server\150\DAC\bin directory.


2. Open the .bacpac file using winzip or 7-zip



3. Copy the model.xml to a local folder “C:\Temp\model.xml”



4. Edit the “C:\Temp\model.xml” with the desired collation. For example:

From: <Property Name="Collation" Value="SQL_Latin1_General_CP1_CI_AS" />
To: <Property Name="Collation" Value="Latin1_General_BIN" />
5. Run the import using sqlpackage.exe, but use the /ModelFilePath:C:\Temp\model.xml parameter to override the model.xml in the .bacpac. For example:
sqlpackage.exe /Action:Import /tsn:[server].database.windows.net /tdn:[database] /tu:[user] /tp:[password] /sf:"C:\Temp\database.bacpac" /ModelFilePath:C:\Temp\model.xml




Below you can find the old way, from the original post



1 - Export the database to .bacpac file

2 - Rename it to .ZIP

3 - Unzip in a folder, you are going to see files like below



4 - Alter the file model.XML , with the requested default collation for the database
From: <Property Name="Collation" Value=" SQL_Latin1_General_CP1_CI_AS " />
To: <Property Name="Collation" Value=" Latin1_General_BIN " />
5 - You can also check also fields that have non default collations
<Element Type="SqlSimpleColumn" Name="[dbo].[Table_1].[anothercollation]">
<Property Name="Collation" Value=" SQL_Latin1_General_CP1_CS_AS " />
6 - Alter the file origin.xml

  • You are going to find a checksum like the below


<Checksums>
<Checksum Uri="/model.xml">35EF75437D2087E0FB029E5D726B229B973CFB0EFE75D014E74214C468976D51</Checksum>
</Checksums>

  • You need to recalculate it using a Powershell script. Make sure to change the file path


$modelXmlPath = " C:\FILEPATH\model.xml "
$hasher = [System.Security.Cryptography.HashAlgorithm]::Create("System.Security.Cryptography.SHA256CryptoServiceProvider")
$fileStream = new-object System.IO.FileStream -ArgumentList @($modelXmlPath, [System.IO.FileMode]::Open)
$hash = $hasher.ComputeHash($fileStream)
$hashString = ""
Foreach ($b in $hash) { $hashString += $b.ToString("X2") }
$fileStream.Close()
$hashString

  • Change the file origin.xml with the new hash and save it


7 - Zip the files again

  • Make sure not to zip the folder where you unzipped the bacpac, only the files inside it




8 - Rename the zip to DBNAME. bacpac

9 - Do the import

10 - You can check the collation in the fields with this query
SELECT T.name, C.name, C.collation_name
FROM sys.tables T
INNER JOIN sys.columns C
ON T.object_id = C.object_id
WHERE C.collation_name IS NOT NULL




I used as a reference the information from this other article
https://blogs.msdn.microsoft.com/azuresqldbsupport/2017/08/16/editing-a-bacpac-file/