Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 ALTER TABLE ALTER COLUMN Collation

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-04-22 : 08:04:28
Michael writes "SQL SERVER 2000 sp3
Windows 2000 Server

Is there any way to alter all the columns in all the tables of a database that use the same collation in one script? Of course, without doing the obvious and specifying an ALTER for each table in one giant script. I know that you can perform an ALTER DATABASE but that will only affect any new tables, columns, etc. that are created without specifying collation."

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-04-22 : 13:59:42
I don't know of an out-of-the-box way to do that. I think you have to do something "home-grown". Here is something mildly tested that will generate the alter statements ("in one giant script") :

declare @OldCollName varchar(200)
,@NewCollName varchar(200)

Select @NewCollName = 'SQL_1Xcompat_CP850_CI_AS'
,@OldCollName = 'SQL_Latin1_General_CP1_CI_AS'

select 'Alter Table [' + t.Table_Schema + '].[' + c.Table_Name + ']' +
' Alter Column [' + c.Column_Name + '] ' +
Data_Type +
case
when data_type like '%char' OR data_type like '%binary' then '(' + convert(varchar,character_maximum_length) + ')'
when data_type IN ('numeric', 'decimal') then '(' + convert(varchar,numeric_precision) + ',' + convert(varchar,numeric_scale ) + ')'
else ''
end +
case when c.data_type like '%char' OR c.data_type like '%text' then ' COLLATE ' + @NewCollName else '' end +
case when Is_Nullable = 'Yes' then ' NULL ' else ' NOT NULL ' end
from information_schema.columns c
JOIN information_schema.tables t ON c.table_name = t.table_name
where t.Table_Type = 'Base Table'

--ONLY Columns that don't have your desired Collation
--AND c.Collation_Name <> @NewCollName

--Only columns that have a spicific Collation
AND c.Collation_Name = @OldCollName

--can't alter text columns
AND data_type not IN ('text', 'ntext', 'image')

Order by c.Table_Name
,c.ordinal_position


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -