Unix Technical Forum

Changing Collation

This is a discussion on Changing Collation within the SQL Server forums, part of the Microsoft SQL Server category; --> Hey all I have written a script to change the collation of user defined columns "en masse" in case ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:42 PM
manstein
 
Posts: n/a
Default Changing Collation

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:55 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com