SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Changing Collation


Changing Collation

Author
Message
ljmonteiro
ljmonteiro
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 2
Comments posted to this topic are about the item Changing Collation



Rampall Sona
Rampall Sona
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 156
ALTER DATABASE mydb COLLATE SQL_Latin1_General_CP1_CI_AS

also can do the trick.
Rich James
Rich James
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 182
There 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 <collation>,'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.



------------------------------------------------------------
-- 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
AS

DECLARE
@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 FOR

select
'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 ' ' end
from syscolumns a, sysobjects b
where a.number = 0
and a.id =b.id
and 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.colid

OPEN C1
FETCH NEXT FROM C1
INTO @table,@column,@type,@length,@nullable


WHILE @@FETCH_STATUS=0
BEGIN

IF @new_collation is null
set @new_collation=@servercollation

set @sql='ALTER TABLE [' + @table + '] ALTER COLUMN [' + @column + '] ' + @type
if (@type<>'text' AND @type<>'ntext')
set @sql=@sql+'('+@length+')'
set @sql=@sql+' COLLATE '+@new_collation+' '+@nullable
IF (@mode='live')
EXEC (@sql)
ELSE
print @sql
FETCH NEXT FROM C1
INTO @table,@column,@type,@length,@nullable

END
CLOSE C1
DEALLOCATE C1
GO


Thanks Luis for giving a headstart that saved me boat loads of time!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search