{"id":636,"date":"2006-05-30T14:31:00","date_gmt":"2006-05-30T14:31:00","guid":{"rendered":"\/blogs\/kimberly\/post\/Changing-Database-Collation-and-dealing-with-TempDB-Objects.aspx"},"modified":"2013-01-11T23:12:18","modified_gmt":"2013-01-12T07:12:18","slug":"changing-database-collation-and-dealing-with-tempdb-objects","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/changing-database-collation-and-dealing-with-tempdb-objects\/","title":{"rendered":"Changing Database Collation and dealing with TempDB Objects"},"content":{"rendered":"<p>\nOK, so&#8230; I don&#39;t know how many of you use different collations but if you do then you know that there are two truths:\n<\/p>\n<blockquote dir=\"ltr\" style=\"margin-right: 0px\">\n<p>\n\t1) They&#39;re very flexible<br \/>\n\t2) They can cause you a bit of grief (changing collations and tempdb)\n\t<\/p>\n<\/blockquote>\n<p>\n<strong>Flexibility<\/strong>\n<\/p>\n<p>\nAs of SQL Server 2000 (or heck, maybe it was 7.0?), database collations could be changed at installation OR set\/changed later. You can set the collation when a database is created (if not set, the database will use the server&#39;s default). You can set the collation when a table is created (if not set, the table will use the database&#39;s default). You can set the collation when a query is executed (which doesn&#39;t really make sense unless it&#39;s in a WHERE clause or ORDER by clause). And &#8211; you can set the collation in a view or stored procedure to do things like case sensitive searching &#8211; on the fly. However, neither of these will perform well over large results sets (at least not without indexes) so, I&#39;d be careful of doing any adHoc changes to collations (even in views\/sps &#8211; without appropriate indexes)!\n<\/p>\n<p>\nAnyway, the key point is that they&#39;re very flexible. In many international databases\/localized databases, column collation differs by table (in order to do efficient sorting, etc.) and different language data may be separated (either with a column that described which language\/country code&nbsp;is used OR in different tables).\n<\/p>\n<p>\n<strong>Grief in Changing Database Collations<\/strong>\n<\/p>\n<p>\nActually, changing database collation is *very* simple. Literally, it only takes an ALTER DATABASE to do. For example, the following code runns flawlessly:\n<\/p>\n<p><font size=\"4\" color=\"#0000ff\"><\/p>\n<blockquote dir=\"ltr\" style=\"margin-right: 0px\">\n<p>\n\t<font face=\"Courier New\" size=\"2\">USE<font face=\"Courier New\"><font color=\"#000000\"> master<br \/>\n\tgo\n\t<\/p>\n<p>\t<font color=\"#0000ff\"><\/p>\n<p>\n\t<font face=\"Courier New\">DROP<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#0000ff\">DATABASE<font face=\"Courier New\"><font color=\"#000000\"> TestCollation<br \/>\n\tgo\n\t<\/p>\n<p>\t<font color=\"#0000ff\"><\/p>\n<p>\n\t<font face=\"Courier New\">CREATE<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#0000ff\">DATABASE<font face=\"Courier New\"><font color=\"#000000\"> TestCollation<br \/>\n\t<font color=\"#0000ff\">COLLATE<font face=\"Courier New\"><font color=\"#000000\"> SQL_Latin1_General_CP1_CI_AS<br \/>\n\tgo\n\t<\/p>\n<p>\t<font color=\"#800000\"><\/p>\n<p>\n\t<font face=\"Courier New\">sp_helpdb<font face=\"Courier New\"><font color=\"#000000\"> TestCollation<br \/>\n\tgo\n\t<\/p>\n<p>\t<font color=\"#0000ff\"><\/p>\n<p>\n\t<font face=\"Courier New\">ALTER<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#0000ff\">DATABASE<font face=\"Courier New\"><font color=\"#000000\"> TestCollation<br \/>\n\t<font color=\"#0000ff\">COLLATE Latin1_General_CS_AS_KS_WS<br \/>\n\tgo\n\t<\/p>\n<p>\t<font color=\"#800000\"><\/p>\n<p>\n\t<font face=\"Courier New\">sp_helpdb<font size=\"4\"><font face=\"Courier New\"><font size=\"2\" color=\"#000000\"> TestCollation<br \/>\n\tgo\n\t<\/p>\n<\/blockquote>\n<p>\nBUT&#8230; if you go from case sensitive to case insensitive&#8230; be careful! It is important to realize that ALL of your tables AND data will need to be checked against the new collation. In fact, changing database collation will not be allowed if the objects\/data would no longer adhere to your unique constraints, etc. Check out this more complete script (<a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/content\/binary\/changingdatabasecollation.sql\">ChangingDatabaseCollation.sql (2.85 KB)<\/a>), if you want to see what happens.\n<\/p>\n<p>\n<strong>Grief with temporary objects<\/strong>\n<\/p>\n<p>\nSo.. the other area (and this seems to be the one where everyone has trouble), is with temporary objects. If you create a temp table and your database has a different collation other than TempDB (which has the same collation as the system &#8211; based on installation), then comparisons\/lookups\/joins &#8211; may have problems. A simple trick to get around this is to use database_default. Check out this sample and you&#39;ll see how it works:\n<\/p>\n<p><font size=\"4\" color=\"#0000ff\"><\/p>\n<blockquote dir=\"ltr\" style=\"margin-right: 0px\">\n<p>\n\t<font face=\"Courier New\" size=\"2\">CREATE<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#0000ff\">DATABASE<font color=\"#000000\"> Test<br \/>\n\t<font color=\"#0000ff\"><font face=\"Courier New\">COLLATE<font face=\"Courier New\" color=\"#000000\"> Icelandic_BIN<br \/>\n\t<font face=\"Courier New\">go\n\t<\/p>\n<p>\t<font color=\"#0000ff\"><\/p>\n<p>\n\t<font face=\"Courier New\">USE<font face=\"Courier New\" color=\"#000000\"> Test<br \/>\n\t<font face=\"Courier New\">go\n\t<\/p>\n<p>\t<font color=\"#0000ff\"><\/p>\n<p>\n\t<font face=\"Courier New\">CREATE<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#0000ff\">TABLE<font color=\"#000000\"> #test1<br \/>\n\t<font face=\"Courier New\" color=\"#808080\">(<br \/>\n\t<font face=\"Courier New\">&nbsp;&nbsp;&nbsp;col1 <font color=\"#0000ff\">varchar<font color=\"#808080\">(12<font color=\"#808080\">) <br \/>\n\t<font face=\"Courier New\" color=\"#808080\">)<br \/>\n\t<font face=\"Courier New\">go\n\t<\/p>\n<p>\t<font color=\"#0000ff\"><\/p>\n<p>\n\t<font face=\"Courier New\">CREATE<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#0000ff\">TABLE<font color=\"#000000\"> #test2<br \/>\n\t<font face=\"Courier New\" color=\"#808080\">(<br \/>\n\t&nbsp;&nbsp;&nbsp;<font face=\"Courier New\">col1 <font color=\"#0000ff\">varchar<font color=\"#808080\">(12<font color=\"#808080\">) <font color=\"#0000ff\">COLLATE database_default<br \/>\n\t<font face=\"Courier New\" color=\"#808080\">)<br \/>\n\t<font face=\"Courier New\">go\n\t<\/p>\n<p>\t<font color=\"#0000ff\"><\/p>\n<p>\n\t<font face=\"Courier New\">USE<font face=\"Courier New\" color=\"#000000\"> Tempdb<br \/>\n\t<font face=\"Courier New\">go\n\t<\/p>\n<p>\t<font color=\"#0000ff\"><\/p>\n<p>\n\t<font face=\"Courier New\">CREATE<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#0000ff\">TABLE<font color=\"#000000\"> #test3<br \/>\n\t<font face=\"Courier New\" color=\"#808080\">(<br \/>\n\t&nbsp;&nbsp;&nbsp;<font face=\"Courier New\">col1 <font color=\"#0000ff\">varchar<font color=\"#808080\">(12<font color=\"#808080\">) <font color=\"#0000ff\">COLLATE database_default<br \/>\n\t<font face=\"Courier New\" color=\"#808080\">)<br \/>\n\t<font face=\"Courier New\">go\n\t<\/p>\n<p>\t<font color=\"#800000\"><\/p>\n<p>\n\t<font face=\"Courier New\">sp_help<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#ff0000\">&#39;tempdb..#test1&#39;<font color=\"#000000\"> <font color=\"#008000\">&#8212; Will use TempDB&#39;s collation<br \/>\n\t<font color=\"#0000ff\"><font face=\"Courier New\">exec<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#800000\">sp_help<font color=\"#000000\"> <font color=\"#ff0000\">&#39;tempdb..#test2&#39;<font color=\"#000000\"> <font color=\"#008000\">&#8212; Will use Test&#39;s collation (Icelandic BIN)<br \/>\n\t<font color=\"#0000ff\"><font face=\"Courier New\">exec<font face=\"Courier New\"><font color=\"#000000\"> <font color=\"#800000\">sp_help<font color=\"#000000\"> <font color=\"#ff0000\">&#39;tempdb..#test3&#39;<font color=\"#000000\"> <font color=\"#008000\">&#8212; Will use TempDB&#39;s collation<br \/>\n\t<font size=\"4\"><font face=\"Courier New\" size=\"2\">go\n\t<\/p>\n<\/blockquote>\n<p>\nSo simple, so obvious&#8230; and, well &#8211; I just found out about that one?! I used to recommend that you <em>explcitly <\/em>set the collation for every column. Now, that still works &#8211; but, it doesn&#39;t offer you any flexbility. So, you could get around that with dynamic string execution but that can also get very complicated, very quickly. So&#8230; database_default is a VERY simple and clean way of doing this.\n<\/p>\n<p>\nHave fun,<br \/>\nkt<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OK, so&#8230; I don&#39;t know how many of you use different collations but if you do then you know that there are two truths: 1) They&#39;re very flexible 2) They can cause you a bit of grief (changing collations and tempdb) Flexibility As of SQL Server 2000 (or heck, maybe it was 7.0?), database collations [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65,75,78],"tags":[],"class_list":["post-636","post","type-post","status-publish","format-standard","hentry","category-sql-server-2005","category-tempdb","category-tips"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/comments?post=636"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/636\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}