SQLServerCentral.com / Discuss content posted by ljmonteiro / Article Discussions by Author / Article Discussions / Changing Collation / Latest PostsInstantForum.NET v99.99.99SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comFri, 09 Dec 2016 20:39:50 GMT20RE: Changing Collationhttp://www.sqlservercentral.com/Forums/Topic402665-616-1.aspxThere are a few bugs in this script that'll catch the unwary. First being 'TEXT' types; they can't actually be given a column length (which the script does), so attempts to collate using this would fail (now intercepted).Second being nvarchar and nchar types; Running the script will double their length (the reported length is in 8 bit bytes; n{var}char's UTF character set uses 16 bits for character storage). Also, I've changed the way it works a little; default is now to generate output of the SQL code that'll be executed so you can examine it and if necessary check against the original schema, but more specifically so you can save this file and use it as part of a script such that you can do all the tinkering on a 'test' version of the database before running the final script on the live one (when you're sure the generated script does what you want).If you still want to run it 'live', you can use the lm_changecollation &lt;collation&gt;,'live' to have it run as it originally did.Also, to just fix things that are broken (if you get tables that have some badly collated columns) you can constrain it to just move away from a particular collation by adding in a third parameter, so that this only detects and fixes columns of that particular collation.[code="plain"]-------------------------------------------------------------- LM_ChangeCollation - Change collation in all tables-------- made by Luis Monteiro - ljmonteiro@eurociber.pt -- Patched 20100512 - Rich James (rich reallyat richjames,me)-- (Fixed text/ntext and n{var}char bugs)------------------------------------------------------------CREATE PROCEDURE [LM_ChangeCollation]@new_collation varchar(100)=NULL,@mode char(4)=NULL,@fixcollate varchar(100)=NULL ASDECLARE @table varchar(50),@column varchar(60),@type varchar(20),@length varchar(4),@nullable varchar(8),@sql varchar(8000),@msg varchar(8000),@servercollation varchar(120)select @servercollation = convert(sysname, serverproperty('collation'))DECLARE C1 CURSOR FORselect 'Table' = b.name, 'Column' = a.name, 'Type' = type_name(a.xusertype), 'Length' = CASE WHEN type_name(a.xusertype)='nchar' or type_name(a.xusertype)='nvarchar' THEN (a.length/2) ELSE a.length END, 'Nullable' = case when a.isnullable = 0 then 'NOT NULL' else ' ' endfrom syscolumns a, sysobjects bwhere a.number = 0 and a.id =b.idand b.xtype='U'and b.name not like 'dt%'and type_name(a.xusertype) in ('char', 'varchar', 'nchar', 'nvarchar','text','ntext')and a.collation like coalesce(@fixcollate,'%')order by b.name,a.colidOPEN C1FETCH NEXT FROM C1INTO @table,@column,@type,@length,@nullableWHILE @@FETCH_STATUS=0BEGINIF @new_collation is nullset @new_collation=@servercollationset @sql='ALTER TABLE [' + @table + '] ALTER COLUMN [' + @column + '] ' + @typeif (@type&lt;&gt;'text' AND @type&lt;&gt;'ntext') set @sql=@sql+'('+@length+')'set @sql=@sql+' COLLATE '+@new_collation+' '+@nullableIF (@mode='live') EXEC (@sql)ELSE print @sqlFETCH NEXT FROM C1INTO @table,@column,@type,@length,@nullableENDCLOSE C1DEALLOCATE C1GO[/code]Thanks Luis for giving a headstart that saved me boat loads of time!Wed, 12 May 2010 10:52:10 GMTRich JamesRE: Changing Collationhttp://www.sqlservercentral.com/Forums/Topic402665-616-1.aspxALTER DATABASE mydb COLLATE SQL_Latin1_General_CP1_CI_ASalso can do the trick.Thu, 15 May 2008 02:10:04 GMTRampall SonaChanging Collation http://www.sqlservercentral.com/Forums/Topic402665-616-1.aspxComments posted to this topic are about the item [B]<A HREF="/scripts/Miscellaneous/30471/">Changing Collation </A>[/B]Tue, 25 Sep 2007 10:51:22 GMTljmonteiro