|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 7:38 AM
Points: 12,
Visits: 315
|
|
In a POC project I was looking into the SQL Server 2012 feature of partially contained databases.
To my surprise I encountered an error where I didn't expect one.
The code hereafter is a simplified test-case.
Server Collation = Latin1_General_CI_AS Collation on database test is the same.
Running the code when database test has CONTAINMENT = PARTIAL gives an error (see below) Running the code when database test has CONTAINMENT = NONE doesn't give any error.
So it seems that there is some change in collation behavior depending on the CONTAINMENT type ...
USE [master] GO
ALTER DATABASE [test] SET CONTAINMENT = PARTIAL WITH NO_WAIT -- WILL RESULT IN ERROR -- ALTER DATABASE [test] SET CONTAINMENT = NONE WITH NO_WAIT -- WILL WORK GO
USE [test] GO
DROP TABLE TEST_CASE GO
CREATE TABLE TEST_CASE ( name varchar(10) NOT NULL , CONSTRAINT PK_LOG_FILTER_PK PRIMARY KEY CLUSTERED (name) ) GO
; MERGE INTO TEST_CASE Target USING ( SELECT 'case' AS name ) Source ON ( Target.name = Source.name ) WHEN NOT MATCHED BY TARGET THEN INSERT ( name ) VALUES ( Source.name ) WHEN NOt MATCHED BY SOURCE THEN DELETE OUTPUT $action , CASE WHEN $action = 'INSERT' THEN 'ADDED' WHEN $action = 'DELETE' THEN 'REMOVED' END ACTION , inserted.name , deleted.name ;
Msg 468, Level 16, State 9, Line 12 Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "Latin1_General_100_CI_AS_KS_WS_SC" in the equal to operation.
Can anyone give me a satisfactory explanation ...
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 1:26 AM
Points: 109,
Visits: 281
|
|
Hi, The only thing I can think of is replacing:
ON ( Target.name = Source.name ) with
ON ( Target.name COLLATE Latin1_General_CI_AS = Source.name COLLATE Latin1_General_CI_AS ) Hope this helps.
For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions. http://www.sqlservercentral.com/articles/Best+Practices/61537/
"Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 7:38 AM
Points: 12,
Visits: 315
|
|
D.Post (11/13/2012)
Hi, The only thing I can think of is replacing: ON ( Target.name = Source.name ) with ON ( Target.name COLLATE Latin1_General_CI_AS = Source.name COLLATE Latin1_General_CI_AS ) Hope this helps.
Yes, and no 
First: I'm aware of the COLLATE clause to overcome differences in collating stuff.
Second: the error isn't on the JOIN condition, but on the CASE-part where I check the $action field. So the COLLATE clause should go there.
CASE WHEN $action = 'INSERT' THEN 'ADDED' WHEN $action = 'DELETE' THEN 'REMOVED' END ACTION
BUT: the question was rather why there is a difference in behavior between a PARTIALLY CONTAINED database and a NON-PARTIALLY CONTAINED database. As far as I'm concerned: this is a bug 
I've got SQL Server Days coming up, and I'm going to relate this issue to some of the speakers.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 1:26 AM
Points: 109,
Visits: 281
|
|
Your code works for me on both 2008 (10.50.1600) Enterprise 2012 (10.0.2100) Evaluation
Both results are the same.
$action ACTION name name INSERT ADDED case NULL I can reproduce your error by forcing an incorrect collation:
CASE WHEN $action COLLATE Latin1_General_CI_AS = 'INSERT' COLLATE SQL_Latin1_General_Cp437_BIN THEN 'ADDED' WHEN $action = 'DELETE' THEN 'REMOVED' END ACTION
Msg 468, Level 16, State 9, Line 12 Cannot resolve the collation conflict between "SQL_Latin1_General_CP437_BIN" and "Latin1_General_CI_AS" in the equal to operation.
For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions. http://www.sqlservercentral.com/articles/Best+Practices/61537/
"Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 7:38 AM
Points: 12,
Visits: 315
|
|
Really appreciate you looking in/at this issue.
The fact that you can reproduce the error by adding the COLLATE-clause is "as designed" in SQL Server.
Forget the SQL 2008 track for now. The code is working in a SQL 2008 environment.
The code also works in a SQL 2012 environment only when the database wherein you work has CONTAINMENT TYPE NONE
Did you have a try with the code against a database with CONTAINMENT TYPE PARTIAL ?
I also installed Service Pack I for SQL 2012, but the error still occurs
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:41 PM
Points: 6,696,
Visits: 11,714
|
|
marc.snoeys (11/13/2012)
D.Post (11/13/2012)
[quote] CASE WHEN $action = 'INSERT' THEN 'ADDED' WHEN $action = 'DELETE' THEN 'REMOVED' END ACTION
BUT: the question was rather why there is a difference in behavior between a PARTIALLY CONTAINED database and a NON-PARTIALLY CONTAINED database. Here is one possible explanation as to what is going on:
In the contained database scenario $action is actually collated using the catalog default, i.e. Latin1_General_100_CI_AS_WS_KS_SC, per the table under the section Contained Databases in this article. Try adding COLLATE DATABASE_DEFAULT to collate $action in the CASE expression.
__________________________________________________________________________________________________ There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 1:26 AM
Points: 109,
Visits: 281
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 7:38 AM
Points: 12,
Visits: 315
|
|
opc.three (11/13/2012)
Here is one possible explanation as to what is going on: In the contained database scenario $action is actually collated using the catalog default, i.e. Latin1_General_100_CI_AS_WS_KS_SC, per the table under the section Contained Databases in this article. Try adding COLLATE DATABASE_DEFAULT to collate $action in the CASE expression.
It strikes me as very odd that changing the containment type of a database results in different behavior in code: in this case the $action field. But this is just a personal opinion (however a few colleagues do agree)
I did a kind of an extension to this test-case:
- Database [test] in CONTAINMENT TYPE=NONE - Create a stored procedure with the MERGE-statement above. - Try to change the CONTAINMENT TYPE=PARTIAL
That resulted in following error, which is basically the same as in the first test-case.
Msg 468, Level 16, State 9, Procedure sp_TEST_CASE, Line 19 Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "Latin1_General_100_CI_AS_KS_WS_SC" in the equal to operation. Msg 12813, Level 16, State 2, Line 2 Errors were encountered in the procedure 'dbo.sp_TEST_CASE' during compilation of the object. Either the containment option of the database 'test' was changed, or this object was present in model db and the user tried to create a new contained database. Msg 12836, Level 16, State 1, Line 2 ALTER DATABASE statement failed. The containment option of the database 'test' could not be altered because compilation errors were encountered during validation of SQL modules. See previous errors. Msg 5069, Level 16, State 1, Line 2 ALTER DATABASE statement failed.
Apparently there is a view where one can query for possible problems when changing to a CONTAINED database: SYS.DM_DB_UNCONTAINED_ENTITIES
But a query on this view does not show this particular stored procedure.
All in all: I guess I'm forced to use the COLLATE-clause ...
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:41 PM
Points: 6,696,
Visits: 11,714
|
|
marc.snoeys (11/14/2012) Apparently there is a view where one can query for possible problems when changing to a CONTAINED database: SYS.DM_DB_UNCONTAINED_ENTITIES
But a query on this view does not show this particular stored procedure. I am not surprised. I would not expect the proc to be returned.
All in all: I guess I'm forced to use the COLLATE-clause ... I think that is the correct move. Here is code that will be portable between both contained as well as non-contained databases regardless of which collation you are using in the database:
WHEN $action COLLATE DATABASE_DEFAULT = 'DELETE' THEN 'REMOVED'
__________________________________________________________________________________________________ There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 7:38 AM
Points: 12,
Visits: 315
|
|
A further update ...
I had a talk with Bob Beachemin about this issue ( and a few others stuff as well ).
He had a look at it and his conclusions were:
In fact, using any collation at random with the collate clause works, as long as you HAVE the collate clause WHEN $action collate Albanian_BIN = 'INSERT' THEN 'ADDED' WHEN $action collate Albanian_BIN = 'DELETE' THEN 'REMOVED'
So you can, as far as I’m concerned, report it as a contained database problem (the fact that you MUST do the collation sounds like a bug) AND/OR a documentation problem (there is no classification of $action anywhere in BOL). The behavior likely has to do with the rules for comparison predicates when collate clause is not specified, and the fact the $action is probably special cased somewhere in the (SQL Server) code.
So I decided to report it on CONNECT
|
|
|
|