%3CLINGO-SUB%20id%3D%22lingo-sub-1547319%22%20slang%3D%22en-US%22%3ECollation%20conflict%20when%20moving%20Azure%20SQL%20DB%20to%20SQL%20server%20on-premises%20or%20Azure%20VM%20using%20SQLPackage.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1547319%22%20slang%3D%22en-US%22%3E%3CP%3E%3CU%3EError%20message%20sample%3A%3C%2FU%3E%3C%2FP%3E%0A%3CP%3E***%20Error%20importing%20database%3A%20Could%20not%20import%20package.%20Error%20SQL72014%3A%20.Net%20SqlClient%20Data%20Provider%3A%20Msg%20468%2C%20Level%2016%2C%20State%209%2C%20Procedure%20GETTABLESWITHOPENCURSORS%2C%20Line%20142%20Cannot%20resolve%20the%20collation%20conflict%20between%20%22SQL_Latin1_General_CP1_CI_AS%22%20and%20%22Latin1_General_100_CI_AS_KS_WS_SC%22%20in%20the%20equal%20to%20operation.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorMousa_Janini_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20id%3D%22tinyMceEditorMousa_Janini_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22error1.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F208081i8528C6335564F689%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22error1.png%22%20alt%3D%22error1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CU%3EError%20cause%3A%3C%2FU%3E%3C%2FP%3E%0A%3CP%3EThe%20conflict%20is%20happening%20because%20the%20database%20containment%20is%20enabled%20on%20source%2C%20hence%20it%20is%20forcing%20the%20same%20on%20target%20that%20makes%20any%20system%20or%20temp%20objects%20to%20use%20the%20default%20windows%20collation%20%22Latin1_General_100_CI_AS_KS_WS_SC%22%20instead%20of%20the%20instance%2FDB%20collation%20%E2%80%9CSQL_Latin1_General_CP1_CI_AS%E2%80%9D.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CU%3EWhat%20can%20enable%20containment%20on%20Azure%20SQL%20DB%3F%3C%2FU%3E%3C%2FP%3E%0A%3CP%3EWhen%20exporting%20Azure%20SQL%20DB%20to%20a%20Bacpac%20the%20containment%20is%20%3CSTRONG%3ENone%3C%2FSTRONG%3E%20by%20default%2C%20unless%20database%20has%20a%20contained%20database%20users%20created%20under%20it%2C%20then%20it%20will%20force%20the%20containment%20to%20%3CSTRONG%3EPartial%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3EIn%20SQLPackage%2C%20the%20DAC%20framework%20will%20force%20enabling%20the%20containment%20during%20the%20import%20process%20if%20one%20of%20the%20below%20conditions%20is%20true%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIf%20source%20database%20containment%20%3D%20Partial%20in%20model.xml.%3C%2FLI%3E%0A%3CLI%3EIf%20containment%20was%20not%20being%20set%20in%20model.xml%20file%20and%20database%20has%20a%20contained%20database%20users.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIn%20our%20scenario%2C%20the%20second%20option%20is%20true%2C%20hence%20the%20containment%20was%20being%20forced%20during%20the%20import%20of%20the%20bacpac%2C%20you%20can%20see%20it%20in%20SQLPackage%20logs%20as%20mentioned%20below%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22logs1.png%22%20style%3D%22width%3A%20538px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F208082i2CB6A160CB160D97%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22logs1.png%22%20alt%3D%22logs1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CU%3EResolution%2FWorkaround%3A%3C%2FU%3E%3C%2FP%3E%0A%3CP%3EIn%20order%20to%20disable%20containment%20in%20Azure%20SQL%20DB%2C%20you%20need%20to%20drop%20the%20contained%20DB%20users%20created%20under%20the%20database%2C%20and%20it%20will%20set%20the%20containment%20%3D%20None%20when%20importing%20the%20database%20to%20target%20SQL%20server.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CU%3EHow%20to%20identify%20and%20drop%20contained%20DB%20users%20is%20Azure%20SQL%20DB%3F%3C%2FU%3E%3C%2FP%3E%0A%3CP%3E1.%20Detect%20the%20contained%20DB%20users%3A%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3BA.%20Run%20the%20below%20script%20to%20list%20all%20users%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22623%22%3E%3CP%3Eselect%20name%20as%20username%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20create_date%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20modify_date%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20type_desc%20as%20type%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20authentication_type_desc%20as%20authentication_type%3C%2FP%3E%0A%3CP%3Efrom%20sys.database_principals%3C%2FP%3E%0A%3CP%3Ewhere%20type%20not%20in%20('A'%2C%20'G'%2C%20'R'%2C%20'X')%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20and%20sid%20is%20not%20null%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20and%20name%20!%3D%20'guest'%3C%2FP%3E%0A%3CP%3Eorder%20by%20username%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3BB.%26nbsp%3B%20Users%20with%20authentication_type%20DATABASE%20is%20a%20contained%20DB%20user%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22ssms1.png%22%20style%3D%22width%3A%20556px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F208083i5E99D3B25F77A3A4%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22ssms1.png%22%20alt%3D%22ssms1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2.%20Remove%20the%20contained%20DB%20user%20on%20source%20database%20by%20following%20the%20below%20steps%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20A.%20Run%20the%20below%20script%20to%20confirm%20if%20the%20user%20is%20an%20owner%20to%20a%20schema%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20SELECT%20s.name%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20FROM%20sys.schemas%20s%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20WHERE%20s.principal_id%20%3D%20USER_ID('test2')%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20B.%20If%20it%E2%80%99s%20an%20owner%20to%20a%20schema%2C%20then%20change%20the%20owner%20for%20this%20schema%20to%20dbo%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20ALTER%20AUTHORIZATION%20ON%20SCHEMA%3A%3A%3CSCHEMANAME%3E%20TO%20dbo%3B%3C%2FSCHEMANAME%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20C.%20Drop%20the%20user%20now%20under%20the%20database.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20DROP%20USER%20%5Btest2%5D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1547319%22%20slang%3D%22en-US%22%3E%3CP%3ECannot%20resolve%20the%20collation%20conflict%20between%20%22SQL_Latin1_General_CP1_CI_AS%22%20and%20%22Latin1_General_100_CI_AS_KS_WS_SC%22%20when%20moving%20Azure%20SQL%20DB%20to%20SQL%20server%20on-premises%20or%20on%20Azure%20VM%20using%20SQLPackage.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

Error message sample:

*** Error importing database: Could not import package. Error SQL72014: .Net SqlClient Data Provider: Msg 468, Level 16, State 9, Procedure GETTABLESWITHOPENCURSORS, Line 142 Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_100_CI_AS_KS_WS_SC" in the equal to operation.

 
 

error1.png

 

Error cause:

The conflict is happening because the database containment is enabled on source, hence it is forcing the same on target that makes any system or temp objects to use the default windows collation "Latin1_General_100_CI_AS_KS_WS_SC" instead of the instance/DB collation “SQL_Latin1_General_CP1_CI_AS”.

 

What can enable containment on Azure SQL DB?

When exporting Azure SQL DB to a Bacpac the containment is None by default, unless database has a contained database users created under it, then it will force the containment to Partial.

In SQLPackage, the DAC framework will force enabling the containment during the import process if one of the below conditions is true:

  • If source database containment = Partial in model.xml.
  • If containment was not being set in model.xml file and database has a contained database users.

In our scenario, the second option is true, hence the containment was being forced during the import of the bacpac, you can see it in SQLPackage logs as mentioned below:

logs1.png

 

Resolution/Workaround:

In order to disable containment in Azure SQL DB, you need to drop the contained DB users created under the database, and it will set the containment = None when importing the database to target SQL server.

 

How to identify and drop contained DB users is Azure SQL DB?

1. Detect the contained DB users:   

   A. Run the below script to list all users:

 

select name as username,

       create_date,

       modify_date,

       type_desc as type,

       authentication_type_desc as authentication_type

from sys.database_principals

where type not in ('A', 'G', 'R', 'X')

      and sid is not null

      and name != 'guest'

order by username;

 

   B.  Users with authentication_type DATABASE is a contained DB user:

            ssms1.png

 

2. Remove the contained DB user on source database by following the below steps:

  A. Run the below script to confirm if the user is an owner to a schema:

      SELECT s.name

      FROM sys.schemas s

      WHERE s.principal_id = USER_ID('test2');

  B. If it’s an owner to a schema, then change the owner for this schema to dbo

      ALTER AUTHORIZATION ON SCHEMA::<SchemaName> TO dbo;

  C. Drop the user now under the database.

      DROP USER [test2]