dbTalk Databases Forums  

Changing Collation

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Changing Collation in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
manstein
 
Posts: n/a

Default Changing Collation - 10-03-2007 , 10:18 AM






Hey all

I have written a script to change the collation of user defined
columns "en masse" in case anyone is interested:

SET NOCOUNT ON;

DECLARE @i int,
@imax int,
@Col varchar(255),
@Table varchar(255),
@SQL varchar(255)

DECLARE @Cols TABLE(
indx int IDENTITY(1,1),
ColName varchar(255),
ColType varchar(255),
ColSize varchar(255),
TableName varchar(255)
);

-- Load up the tmp table with the user defined cols
INSERT INTO @Cols (
ColName,
ColType,
ColSize,
TableName
)
SELECT
c.[name],
y.[name],
c.[max_length],
t.[name]
FROM
sys.columns c
INNER JOIN sys.tables t ON t.[object_id] = c.[object_id]
INNER JOIN sys.types y ON y.system_type_id = c.system_type_id
WHERE
t.type_desc = 'USER_TABLE' AND
y.[name] IN ( 'nchar','nvarchar','char','varchar','ntext','text' )

SELECT
@i = 1,
@imax = MAX( INDX )
FROM
@Cols;

WHILE ( @i <= @imax )
BEGIN

SELECT
@Table = '[' + TableName + ']',
@Col = '[' +
CASE
WHEN ColType IN ( 'text', 'ntext' ) THEN ColName + ']'
ELSE ColName + '] ' + ColType + '(' + ColSize + ')'
END
FROM
@Cols
WHERE
indx = @i;

SET @SQL = 'ALTER TABLE ' + @Table + ' ALTER COLUMN ' + @Col +
' COLLATE SQL_Latin1_General_CP1_CI_AS;';
PRINT @SQL
--EXEC (@SQL);

SET @i = @i + 1;
END


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.