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


Change Existing Field Collation


Change Existing Field Collation

Author
Message
Kosmas
Kosmas
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 25
How can I change the collation of one existing database ? I want to change also all existing fields not only the database collation.

In SQL 2000 in properties was a flag "Allow modifications to be made directly to the system catalogs". This not exists in 2005.
Nikhil Shikarkhane
Nikhil Shikarkhane
SSC Veteran
SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)

Group: General Forum Members
Points: 234 Visits: 203
Use "alter database" and "alter table" commands with COLLATE clause.
Wilfred van Dijk
Wilfred van Dijk
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1413 Visits: 1359
Had the same issue, try this script for fixing collation in your tables:


------------------------------------------------------------
-- LM_ChangeCollation - Change collation in all tables
-- made by Luis Monteiro - ljmonteiro@eurociber.pt
-- modified by wilfred van dijk - wvand@wilfredvandijk.nl
------------------------------------------------------------

DECLARE @new_collation varchar(100)
DECLARE @debug bit

DECLARE
@table sysname,
@previous sysname,
@column varchar(60),
@type varchar(20),
@legth varchar(4),
@nullable varchar(8),
@sql varchar(8000),
@msg varchar(8000),
@servercollation varchar(120)

/*
uncomment one of the following lines:
*/
set @new_collation = convert(sysname, databasepropertyex(DB_NAME(), 'collation'))
-- set @new_collation = convert(sysname, serverproperty('collation'))

/*
@debug = 0 to execute
*/
set @debug = 1

if @new_collation is null
begin
print 'which collation?'
goto einde
end

DECLARE C1 CURSOR FOR
select 'Table' = b.name,
'Column' = a.name,
'Type' = type_name(a.system_type_id),
'Length' = a.max_length,
'Nullable' = case when a.is_nullable = 0 then 'NOT NULL' else ' ' end
from sys.columns a
join sysobjects b
on a.object_id = b.id
where b.xtype = 'U'
and b.name not like 'dt%'
and type_name(a.system_type_id) in ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext')
and a.[collation_name] <> @new_collation
order by b.name,a.column_id

OPEN C1
FETCH NEXT
FROM C1
INTO @table,@column,@type,@legth,@nullable
set @previous = @table

WHILE @@FETCH_STATUS = 0
BEGIN
if @table <> @previous print ''
set @sql = 'ALTER TABLE ' + QUOTENAME(@table) + ' ALTER COLUMN ' + QUOTENAME(@column) + ' '
set @sql = @sql + @type + '(' + @legth + ')' + ' COLLATE ' + @new_collation + ' ' + @nullable

print @SQL

if @debug = 0
begin
begin try
EXEC (@sql)
end try
begin catch
print 'ERROR:' + ERROR_MESSAGE()
print ''
end catch
end

set @previous = @table
FETCH NEXT
FROM C1
INTO @table,@column,@type,@legth,@nullable
END
CLOSE C1
DEALLOCATE C1

einde:



Note: You'll manually have to fix columns which are part of a key/constraint (by using management studio)

Wilfred
The best things in life are the simple things
Kosmas
Kosmas
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 25
thanks. I will try this script.
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