{"id":47,"date":"2012-08-19T09:41:00","date_gmt":"2012-08-19T09:41:00","guid":{"rendered":"\/lisa\/post\/2012\/08\/19\/Or-we-could-talk-some-more.aspx"},"modified":"2012-08-19T09:41:00","modified_gmt":"2012-08-19T09:41:00","slug":"or-we-could-talk-some-more","status":"publish","type":"post","link":"https:\/\/spacefold.com\/lisa\/2012\/08\/19\/or-we-could-talk-some-more\/","title":{"rendered":"Or&#8230; we could talk some more"},"content":{"rendered":"<p>This is a followup note to my <a title=\"original blog post on partial containment in sql server 2012\" href=\"\/lisa\/2012\/06\/22\/Collation-gets-better-in-SQL-2012-but-dont-get-confused\/\">cautionary note on<br \/>thinking that SQL Server 2012 partial containment is going to give you a free lunch<\/a>.<\/p>\n<p>As a quick summary: people are falling over themselves telling you how great it is that you can now move a database to another server with a different default collation easily, by using partial containment.&nbsp;<\/p>\n<p>I indicated that, while there were very good reasons to make your new server use different collations than the old default, it would be better to bite the bullet when moving a database to convert it properly to the new collation, rather than taking the easy way out with partial containment.<\/p>\n<p>I further indicated at least one good use case for using partial containment: if you are regularly copying a database from one server to another, using an automated process, it may not be possible to have your automated process &#8220;bite the bullet&#8221; and transparently do all that conversion work on a regular basis.<\/p>\n<p>Unless you can convert <strong>both<\/strong> servers to the new collation, partial containment looks like a beautiful thing&#8230; for those particular database copies.&nbsp; (For all other databases you&#8217;re moving to the new server, you still shouldn&#8217;t take the easy way out.)<\/p>\n<h2>OK so far?&nbsp; Not so fast&#8230;<\/h2>\n<p>I tried to follow this strategy and ran into a brick wall.<\/p>\n<p>Partial containment is just that, partial.&nbsp; While you&#8217;ve resolved a potential collation issue if you&#8217;re joining with temp tables by using it, if your database code does one of several other things, you&#8217;re still screwed.&nbsp;<\/p>\n<p><strong>SQL Server won&#8217;t let you set the database containment level to partial if it gets the idea that doing so will de-stabilize your code.&nbsp;<\/strong><\/p>\n<p>I&#8217;m pretty sure, although not 100% sure, that having dynamic SQL in your code may cause SQL Server to get this idea.&nbsp;<\/p>\n<p>But I&#8217;m 100% sure that joining with system metadata &#8212; which&nbsp; you can think of as similar to joining with temp tables, but isn&#8217;t covered by &#8220;partial&#8221; containment &#8212; gives SQL Server the idea that it wouldn&#8217;t be safe to&nbsp;apply partial containment on a database.<\/p>\n<h2>Here&#8217;s a very quick example<\/h2>\n<ol>\n<li>Create a test database\n<p><span style=\"font-size: small;\"><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">create database<\/span><\/span> [db1];<\/span><\/p>\n<\/li>\n<li>Create a table with at least one string column\n<p><span style=\"font-size: small;\"><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">create table <\/span><\/span>[db1].[dbo].[test]<span style=\"color: #808080;\"><span style=\"color: #808080;\">(<\/span><\/span>onefield <span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">char<\/span><\/span><span style=\"color: #808080;\"><span style=\"color: #808080;\">(<\/span><\/span>10<span style=\"color: #808080;\"><span style=\"color: #808080;\">));<\/span><\/span><\/span><\/p>\n<\/li>\n<li>\n<p>Create some code &#8212; a view or stored procedure &#8212; in this database that joins the table with some system metadata.&nbsp; Don&#8217;t use the new CATALOG_DEFAULT collation clause on the join; remember, this is for code that was written before SQL 2012, and you wouldn&#8217;t have had a chance to use it when the code was written.<\/p>\n<p><span style=\"font-size: small;\"><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">create <\/span><\/span><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">view<\/span><\/span> [dbo]<span style=\"color: #808080;\"><span style=\"color: #808080;\">.<\/span><\/span>[testView] <span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">as<br \/>&nbsp;&nbsp; select <\/span><\/span><span style=\"color: #808080;\"><span style=\"color: #808080;\">* <\/span><\/span><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">from <\/span><\/span><span style=\"color: #008000;\"><span style=\"color: #008000;\">sys<\/span><\/span><span style=\"color: #808080;\"><span style=\"color: #808080;\">.<\/span><\/span><span style=\"color: #008000;\"><span style=\"color: #008000;\">columns<\/span><\/span> cols ;<br \/>&nbsp;&nbsp; <span style=\"color: #808080;\"><span style=\"color: #808080;\">left <\/span><\/span><span style=\"color: #808080;\"><span style=\"color: #808080;\">join<\/span><\/span> test <span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">on<\/span><\/span> cols<span style=\"color: #808080;\"><span style=\"color: #808080;\">.<\/span><\/span>name <span style=\"color: #808080;\"><span style=\"color: #808080;\">=<\/span><\/span> test<span style=\"color: #808080;\"><span style=\"color: #808080;\">.<\/span><\/span>onefield<\/span><\/p>\n<\/li>\n<li>\n<p>Now try to set containment on this database:<br \/><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\"><br \/><span style=\"font-size: small;\">alter <\/span><\/span><\/span><span style=\"font-size: small;\"><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">database<\/span><\/span> [db1] <span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">set<\/span><\/span> containment <span style=\"color: #808080;\"><span style=\"color: #808080;\">= <\/span><\/span><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">partial<\/span><\/span><span style=\"color: #808080;\"><span style=\"color: #808080;\">;<\/span><\/span><\/span><\/p>\n<\/li>\n<li>\n<p>&#8230; See that?&nbsp; Your error will be something like this:<\/p>\n<p><span style=\"font-family: courier new,courier; color: #ff0000; font-size: x-small;\">Msg 468, Level 16, State 9, Procedure testView, Line 2<\/span><br \/><span style=\"font-family: courier new,courier; color: #ff0000; font-size: x-small;\">Cannot resolve the collation conflict between &#8220;Latin1_General_CI_AS&#8221; and &#8220;Latin1_General_100_CI_AS_KS_WS_SC&#8221; in the equal to operation.<\/span><br \/><span style=\"font-family: courier new,courier; font-size: x-small;\">[&#8230; more here&#8230;]<\/span><br \/><span style=\"font-family: courier new,courier; color: #ff0000; font-size: x-small;\">ALTER DATABASE statement failed. The containment option of the database &#8216;db1&#8217; could not be altered because compilation errors were encountered during validation of SQL modules. See previous errors.<\/span><br \/><span style=\"font-family: courier new,courier; font-size: x-small;\">[&#8230; more here&#8230;]<\/span><\/p>\n<\/li>\n<\/ol>\n<h2>Back to the drawing board<\/h2>\n<p>While all my trivial tests with partial containment went pretty well, it&#8217;s now clear to me that&nbsp;many, if not most,&nbsp;non-trivial databases&nbsp;could have a harder time taking advantage of the feature.<\/p>\n<p>I know, I know.&nbsp;&nbsp;Some of you are&nbsp;going to say the above example is contrived.&nbsp; Believe me, it&#8217;s not.&nbsp; There are plenty of good reasons to try to extend system metadata using tables of&nbsp; your own design in a database.<\/p>\n<p>You&#8217;re also going to say &#8220;I don&#8217;t have metadata joins like that and I really need the user piece of partial containment&#8221;. And, certainly, yes, the ability to ensure that SQL identities stay intact when copying a database from one server to another is one of the advantages of partial containment, right up there with having your temp tables follow the collation of the database instead of the collation of the server.<\/p>\n<p>But my own automated &#8220;copy&#8221; procedures already take care of the sql identity problem &#8212; and I&#8217;m sure yours could do that too.&nbsp;<\/p>\n<p class=\"NB\" style=\"padding-left: 30px;\">If anybody is interested, I&#8217;ll write a post on how I do take care of sql identities when moving databases between servers, although I don&#8217;t think it&#8217;s terribly slick or difficult.&nbsp; It certainly doesn&#8217;t require SQL 2012 to work.<\/p>\n<p>Meanwhile, I&#8217;m going to steer clear of partial containment for the next little while.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a followup note to my cautionary note onthinking that SQL Server 2012 partial containment is going to give you a free lunch. As a quick summary: people are falling over themselves telling you how great it is that you can now move a database to another server with a different default collation easily,<a class=\"more-link\" href=\"https:\/\/spacefold.com\/lisa\/2012\/08\/19\/or-we-could-talk-some-more\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}