Home
%3CLINGO-SUB%20id%3D%22lingo-sub-370384%22%20slang%3D%22en-US%22%3ESQL%20Server%202012%20Partially%20Contained%20Databases%20Part%202%20-%20Collation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-370384%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Feb%2026%2C%202013%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%0A%3CP%3EIn%20my%20last%20post%2C%20I%20went%20over%20partial%20database%20containment%20and%20contained%20users.%26nbsp%3B%20This%20post%20will%20focus%20on%20the%20other%20piece%20of%20functionality%20in%20partial%20containment%E2%80%A6%20collation.%26nbsp%3B%20The%20collation%20of%20data%20determines%20how%20data%20is%20sorted%20and%20compared.%26nbsp%3B%20When%20all%20databases%20are%20using%20the%20same%20collation%20as%20the%20instance%20collation%20(selected%20during%20install)%2C%20then%20there%20is%20really%20no%20need%20for%20containment%20of%20the%20collation.%26nbsp%3B%20The%20problem%20arises%20when%20you%20are%20hosting%20multiple%20application%20databases%20on%20your%20instance%20that%20require%20a%20separate%20collation.%26nbsp%3B%20The%20most%20typical%20example%20of%20this%20is%20when%20objects%20are%20created%20in%20tempdb.%26nbsp%3B%20They%20will%20have%20the%20instance%20default%20collation.%26nbsp%3B%20If%20the%20collations%20between%20the%20two%20are%20different%20then%20you%20may%20see%20an%20error%20similar%20to%20this%20one%20below%3A%3C%2FP%3E%0A%0A%3CPRE%3E%3CFONT%20color%3D%22red%22%3E%0AMsg%20468%2C%20Level%2016%2C%20State%209%2C%20Line%204%0A%0ACannot%20resolve%20the%20collation%20conflict%20between%20%22SQL_Latin1_General_CP1_CI_AS%22%20and%20%22Latin1_General_CI_AS_KS_WS%22%20in%20the%20equal%20to%20operation.%0A%3C%2FFONT%3E%3C%2FPRE%3E%0A%0A%0A%3CP%3EIn%20many%20cases%2C%20this%20can%20be%20resolved%20easily%20by%20using%20COLLATE%20to%20define%20the%20collation%20of%20the%20column%20to%20prevent%20this%20issue.%26nbsp%3B%20But%20what%20happens%20when%20you%20have%20a%20vendor%20application%20that%20will%20not%20allow%20you%20to%20change%20their%20code%3F%26nbsp%3B%20This%20is%20where%20having%20a%20contained%20collation%20will%20come%20into%20play.%3C%2FP%3E%0A%0A%0A%3CP%3EThis%20problem%20was%20resolved%20by%20creating%20the%20catalog%20collation.%26nbsp%3B%20This%20collation%20is%20for%20system%20metadata%20and%20transient%20objects.%26nbsp%3B%20This%20means%20that%20any%20temporary%20metadata%2C%20variables%2C%20etc.%20will%20use%20the%20catalog%20default%20collation%20and%20not%20the%20database%20collation.%20The%20catalog%20collation%20is%20%3CSTRONG%3E%20Latin1_General_100_CI_AS_WS_KS_SC%2C%20%3C%2FSTRONG%3E%20%3CSTRONG%3E%20and%20it%20is%20the%20same%20for%20all%20contained%20databases%20in%20the%20instance.%26nbsp%3B%20This%20does%20NOT%20mean%20that%20all%20contained%20databases%20on%20an%20instance%20must%20have%20the%20same%20collation%2Cthis%20is%20just%20the%20catalog%20collation%20that%20is%20predefined.%26nbsp%3BThe%20following%20chart%20will%20help%20illustrate%20the%20difference%3A%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%0A%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20423px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89392iC4831FF8ED94CE6E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%0A%0A%3CP%3EMany%20of%20us%20don%E2%80%99t%20deal%20with%20collation%20issues%20on%20a%20day%20to%20day%20basis%2C%20so%20I%20am%20including%20some%20scripts%20that%20will%20help%20you%20explore%20contained%20collation%20a%20little%20more.%26nbsp%3B%20I%20included%20the%20entire%20script%20in%20one%20piece%20at%20the%20bottom%20of%20the%20post%20for%20ease%20of%20copy%2Fpaste.%3C%2FP%3E%0A%0A%0A%3CP%3ERun%20this%20first%20section%20to%20enable%20containment%20and%20create%20the%20databases%20(note%20that%20if%20you%20still%20have%20the%203%20databases%20created%20from%20part%201%20then%20you%20can%20skip%20over%20that%20part%20here)%3A%3C%2FP%3E%0A%0A%3CPRE%3E%0A--enable%20contained%20dbs%0AEXEC%20sys.sp_configure%20N'contained%20database%20authentication'%0A%20%2C1%0AGO%0A%0ARECONFIGURE%0AGO%0A%0A--Create%20the%203%20dbs%2C%20all%20have%20different%20collation%20from%20the%20instance%20collation%0ACREATE%20DATABASE%20%5BMyDB%5D%20--not%20contained%0A%20COLLATE%20Latin1_General_CI_AS_KS_WS%0AGO%0A%0ACREATE%20DATABASE%20%5BMyContainedDB%5D%20--partially%20contained%0A%20CONTAINMENT%20%3D%20PARTIAL%20COLLATE%20Latin1_General_CI_AS_KS_WS%0AGO%0A%0ACREATE%20DATABASE%20%5BMyContainedDBToo%5D%20--partially%20contained%20to%20illustrate%20multiple%20collations%0A%20CONTAINMENT%20%3D%20PARTIAL%20COLLATE%20Latin1_General_CS_AS_KS%0AGO%0A%3C%2FPRE%3E%0A%0A%3CP%3EIn%20the%20first%20database%2C%20there%20is%20no%20containment.%3C%2FP%3E%0A%3CPRE%3E%0AUSE%20MyDB%0AGO%0A%0A--Create%20Static%20Table%0ACREATE%20TABLE%20MyTable%20(%0A%20mycolumn1%20NVARCHAR%0A%20%2Cmycolumn2%20NVARCHAR%0A%20)%3B%0AGO%0A%0A--Show%20column%20and%20server%20collation%20difference%0ASELECT%20name%0A%20%2Ccollation_name%0AFROM%20sys.columns%0AWHERE%20name%20LIKE%20'mycolumn%25'%3B%0AGO%0A%0ASELECT%20SERVERPROPERTY('collation')%0A%3C%2FPRE%3E%0A%0A%3CP%3EFrom%20the%202%20above%20statements%2C%20you%20should%20see%20the%20following%20result%20set%20to%20show%20you%20that%20the%20columns%20in%20the%20table%20have%20a%20different%20collation%20than%20the%20instance%3A%3C%2FP%3E%0A%0A%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20353px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89393i8648F0FD904ABC0C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%0A%0A%3CP%3EThen%20create%20a%20temp%20table%20and%20join%20to%20it%20from%20MyTable%2C%20you%20will%20get%20a%20collation%20conflict.%3C%2FP%3E%0A%0A%3CPRE%3E%0ACREATE%20TABLE%20%23MyTempTable%20(%0A%20mycolumn1%20NVARCHAR%0A%20%2Cmycolumn2%20NVARCHAR%0A%20)%3B%0AGO%0A%0ASELECT%20T1.mycolumn1%0A%20%2CT2.mycolumn1%0AFROM%20MyTable%20T1%0AJOIN%20%23MyTempTable%20T2%20ON%20T1.mycolumn1%20%3D%20T2.mycolumn1%0A%3C%2FPRE%3E%0A%0A%3CPRE%3E%3CFONT%20color%3D%22red%22%3E%0AMsg%20468%2C%20Level%2016%2C%20State%209%2C%20Line%2010%0A%0ACannot%20resolve%20the%20collation%20conflict%20between%20%22SQL_Latin1_General_CP1_CI_AS%22%20and%20%22Latin1_General_CI_AS_KS_WS%22%20in%20the%20equal%20to%20operation.%0A%3C%2FFONT%3E%3C%2FPRE%3E%0A%0A%3CP%3ENow%20drop%20the%20temp%20table%20and%20rerun%20the%20select%20with%20a%20collation%20conversion%20of%20the%20columns.%26nbsp%3B%20This%20works...%20until%20you%20find%20out%20that%20this%20is%20a%20vendor%20application%20and%20the%20mention%20the%20dreaded%20%E2%80%9Cunsupported%E2%80%9D%20word.%3C%2FP%3E%0A%0A%3CPRE%3E%0A--drop%20temp%20table%0ADROP%20TABLE%20%23MyTempTable%0A%0A--Now%20define%20the%20temp%20table%20with%20the%20db%20collation%0A--there%20will%20be%20no%20error%20when%20we%20select%0ACREATE%20TABLE%20%23MyTempTable%20(%0A%20mycolumn1%20NVARCHAR%20COLLATE%20Latin1_General_CI_AS_KS_WS%0A%20%2Cmycolumn2%20NVARCHAR%20COLLATE%20Latin1_General_CI_AS_KS_WS%0A%20)%3B%0A%0ASELECT%20T1.mycolumn1%0A%20%2CT2.mycolumn1%0AFROM%20MyTable%20T1%0AJOIN%20%23MyTempTable%20T2%20ON%20T1.mycolumn1%20%3D%20T2.mycolumn1%0A%3C%2FPRE%3E%0A%0A%3CP%3EDatabase%20containment%20to%20the%20rescue!%26nbsp%3BHere%20you%20will%20switch%20to%20a%20contained%20database.%3C%2FP%3E%0A%0A%3CPRE%3E%0A--switch%20dbs%0AUSE%20MyContainedDB%0AGO%0A%0A--Create%20static%20table%0ACREATE%20TABLE%20MyTable%20(%0A%20mycolumn1%20NVARCHAR%0A%20%2Cmycolumn2%20NVARCHAR%0A%20)%3B%0AGO%0A%0A--Show%20column%20and%20server%20collation%20difference%0ASELECT%20name%0A%20%2Ccollation_name%0AFROM%20sys.columns%0AWHERE%20name%20LIKE%20'mycolumn%25'%3B%0AGO%0A%0ASELECT%20SERVERPROPERTY('collation')%0A%3C%2FPRE%3E%0A%0A%0A%3CP%3ENote%20here%20that%20your%20result%20set%20looks%20identical%20to%20the%20non-contained%20db.%26nbsp%3B%20That%20is%20OK.%26nbsp%3B%20Remember%20that%20contained%20databases%20use%20the%20catalog%20collation%20for%20temporary%20metadata.%3C%2FP%3E%0A%0A%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20335px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89394iFCAFA1E73B97EB6A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%0A%0A%0A%0A%0A%3CP%3EHere%20is%20where%20we%20had%20an%20error%20before%2C%20but%20this%20time%20it%20will%20work!%3C%2FP%3E%0A%0A%3CPRE%3E%0A--drop%20temp%20table%0ADROP%20TABLE%20%23MyTempTable%0A%0A--Create%20the%20temp%20table%0A--Temp%20objects%20use%20the%20CATALOG%20COLLATION%20for%20tempdb%0A--since%20this%20db%20is%20partially%20contained%0ACREATE%20TABLE%20%23MyTempTable%20(%0A%20mycolumn1%20NVARCHAR%0A%20%2Cmycolumn2%20NVARCHAR%0A%20)%3B%0AGO%0A%0ASELECT%20T1.mycolumn1%0A%20%2CT2.mycolumn1%0AFROM%20MyTable%20T1%0AJOIN%20%23MyTempTable%20T2%20ON%20T1.mycolumn1%20%3D%20T2.mycolumn1%0A%3C%2FPRE%3E%0A%0A%0A%3CP%3EI%20also%20included%20a%20separate%20database%20with%20a%20third%20collation%20just%20to%20show%20that%20you%20can%20have%20a%20number%20of%20databases%20with%20a%20number%20of%20collations.%3C%2FP%3E%0A%0A%3CPRE%3E%0A--switch%20dbs%0AUSE%20MyContainedDBToo%0AGO%0A%0A--Create%20static%20table%0ACREATE%20TABLE%20MyTable%20(%0A%20mycolumn1%20NVARCHAR%0A%20%2Cmycolumn2%20NVARCHAR%0A%20)%3B%0AGO%0A%0A--Show%20column%20and%20server%20collation%20difference%0ASELECT%20name%0A%20%2Ccollation_name%0AFROM%20sys.columns%0AWHERE%20name%20LIKE%20'mycolumn%25'%3B%0AGO%0A%0ASELECT%20SERVERPROPERTY('collation')%0A%0A--drop%20temp%20table%0ADROP%20TABLE%20%23MyTempTable%0A%0A--Create%20the%20temp%20table%0A--Temp%20objects%20use%20the%20CATALOG%20COLLATION%20for%20tempdb%0A--since%20this%20db%20is%20partially%20contained%0ACREATE%20TABLE%20%23MyTempTable%20(%0A%20mycolumn1%20NVARCHAR%0A%20%2Cmycolumn2%20NVARCHAR%0A%20)%3B%0AGO%0A%0ASELECT%20T1.mycolumn1%0A%20%2CT2.mycolumn1%0AFROM%20MyTable%20T1%0AJOIN%20%23MyTempTable%20T2%20ON%20T1.mycolumn1%20%3D%20T2.mycolumn1%0A%3C%2FPRE%3E%0A%0A%0A%0A%3CP%3ENow%20you%20have%20an%20understanding%20of%20what%20contained%20databases%20can%20do.%26nbsp%3B%20Perhaps%20you%20have%20some%20candidates%20in%20your%20environment%20for%20database%20containment.%3C%2FP%3E%0A%0A%0A%0A%0A%0A%3CP%3E%20%3CSTRONG%3ELisa%20Gardner%20%3C%2FSTRONG%3E%20%E2%80%93%20SQL%20Premier%20Field%20Engineer%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-370384%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20MSDN%20on%20Feb%2026%2C%202013%20%26nbsp%3B%20In%20my%20last%20post%2C%20I%20went%20over%20partial%20database%20containment%20and%20contained%20users.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-370384%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Edatabase%20engine%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor

First published on MSDN on Feb 26, 2013

In my last post, I went over partial database containment and contained users.  This post will focus on the other piece of functionality in partial containment… collation.  The collation of data determines how data is sorted and compared.  When all databases are using the same collation as the instance collation (selected during install), then there is really no need for containment of the collation.  The problem arises when you are hosting multiple application databases on your instance that require a separate collation.  The most typical example of this is when objects are created in tempdb.  They will have the instance default collation.  If the collations between the two are different then you may see an error similar to this one below:


Msg 468, Level 16, State 9, Line 4

Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS_KS_WS" in the equal to operation.

In many cases, this can be resolved easily by using COLLATE to define the collation of the column to prevent this issue.  But what happens when you have a vendor application that will not allow you to change their code?  This is where having a contained collation will come into play.

This problem was resolved by creating the catalog collation.  This collation is for system metadata and transient objects.  This means that any temporary metadata, variables, etc. will use the catalog default collation and not the database collation. The catalog collation is Latin1_General_100_CI_AS_WS_KS_SC, and it is the same for all contained databases in the instance.  This does NOT mean that all contained databases on an instance must have the same collation,this is just the catalog collation that is predefined. The following chart will help illustrate the difference:

Many of us don’t deal with collation issues on a day to day basis, so I am including some scripts that will help you explore contained collation a little more.  I included the entire script in one piece at the bottom of the post for ease of copy/paste.

Run this first section to enable containment and create the databases (note that if you still have the 3 databases created from part 1 then you can skip over that part here):

--enable contained dbs
EXEC sys.sp_configure N'contained database authentication'
	,1
GO

RECONFIGURE
GO

--Create the 3 dbs, all have different collation from the instance collation
CREATE DATABASE [MyDB] --not contained
	COLLATE Latin1_General_CI_AS_KS_WS
GO

CREATE DATABASE [MyContainedDB] --partially contained
	CONTAINMENT = PARTIAL COLLATE Latin1_General_CI_AS_KS_WS
GO

CREATE DATABASE [MyContainedDBToo] --partially contained to illustrate multiple collations
	CONTAINMENT = PARTIAL COLLATE Latin1_General_CS_AS_KS
GO

In the first database, there is no containment.

USE MyDB
GO

--Create Static Table
CREATE TABLE MyTable (
	mycolumn1 NVARCHAR
	,mycolumn2 NVARCHAR
	);
GO

--Show column and server collation difference
SELECT name
	,collation_name
FROM sys.columns
WHERE name LIKE 'mycolumn%';
GO

SELECT SERVERPROPERTY('collation')

From the 2 above statements, you should see the following result set to show you that the columns in the table have a different collation than the instance:

Then create a temp table and join to it from MyTable, you will get a collation conflict.

CREATE TABLE #MyTempTable (
	mycolumn1 NVARCHAR
	,mycolumn2 NVARCHAR
	);
GO

SELECT T1.mycolumn1
	,T2.mycolumn1
FROM MyTable T1
JOIN #MyTempTable T2 ON T1.mycolumn1 = T2.mycolumn1

Msg 468, Level 16, State 9, Line 10

Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS_KS_WS" in the equal to operation.

Now drop the temp table and rerun the select with a collation conversion of the columns.  This works... until you find out that this is a vendor application and the mention the dreaded “unsupported” word.

--drop temp table
DROP TABLE #MyTempTable

--Now define the temp table with the db collation
--there will be no error when we select
CREATE TABLE #MyTempTable (
	mycolumn1 NVARCHAR COLLATE Latin1_General_CI_AS_KS_WS
	,mycolumn2 NVARCHAR COLLATE Latin1_General_CI_AS_KS_WS
	);

SELECT T1.mycolumn1
	,T2.mycolumn1
FROM MyTable T1
JOIN #MyTempTable T2 ON T1.mycolumn1 = T2.mycolumn1

Database containment to the rescue! Here you will switch to a contained database.

--switch dbs
USE MyContainedDB
GO

--Create static table
CREATE TABLE MyTable (
	mycolumn1 NVARCHAR
	,mycolumn2 NVARCHAR
	);
GO

--Show column and server collation difference
SELECT name
	,collation_name
FROM sys.columns
WHERE name LIKE 'mycolumn%';
GO

SELECT SERVERPROPERTY('collation')

Note here that your result set looks identical to the non-contained db.  That is OK.  Remember that contained databases use the catalog collation for temporary metadata.

Here is where we had an error before, but this time it will work!

--drop temp table
DROP TABLE #MyTempTable

--Create the temp table
--Temp objects use the CATALOG COLLATION for tempdb
--since this db is partially contained
CREATE TABLE #MyTempTable (
	mycolumn1 NVARCHAR
	,mycolumn2 NVARCHAR
	);
GO

SELECT T1.mycolumn1
	,T2.mycolumn1
FROM MyTable T1
JOIN #MyTempTable T2 ON T1.mycolumn1 = T2.mycolumn1

I also included a separate database with a third collation just to show that you can have a number of databases with a number of collations.

--switch dbs
USE MyContainedDBToo
GO

--Create static table
CREATE TABLE MyTable (
	mycolumn1 NVARCHAR
	,mycolumn2 NVARCHAR
	);
GO

--Show column and server collation difference
SELECT name
	,collation_name
FROM sys.columns
WHERE name LIKE 'mycolumn%';
GO

SELECT SERVERPROPERTY('collation')

--drop temp table
DROP TABLE #MyTempTable

--Create the temp table
--Temp objects use the CATALOG COLLATION for tempdb
--since this db is partially contained
CREATE TABLE #MyTempTable (
	mycolumn1 NVARCHAR
	,mycolumn2 NVARCHAR
	);
GO

SELECT T1.mycolumn1
	,T2.mycolumn1
FROM MyTable T1
JOIN #MyTempTable T2 ON T1.mycolumn1 = T2.mycolumn1

Now you have an understanding of what contained databases can do.  Perhaps you have some candidates in your environment for database containment.

Lisa Gardner – SQL Premier Field Engineer