Solved

Automatically Alter Colation

Posted on 2004-03-31
3
595 Views
Last Modified: 2007-12-19
Hi All,

I have the following script I use to automatically alter the colation of all varchar fields in a database:

Declare @SQL VarChar(1000)
Declare AlterColation Cursor
For

select 'ALTER TABLE [' + so.Name + '] ALTER COLUMN [' + sc.Name + '] varchar(' + Cast(sc.Length As VarChar(10)) + ') COLLATE SQL_Latin1_General_CP1_CI_AS'
from syscolumns sc
Inner Join sysObjects so on so.id = sc.id
Where Not (sc.CollationId Is Null)
And so.XType = 'U '
And sc.type = 39
Order By so.Name, sc.Name

Open AlterColation
Fetch Next From AlterColation
Into @SQL
WHILE @@FETCH_STATUS = 0
Begin
      Exec(@SQL)
      Fetch Next From AlterColation
      INTO @SQL
      Print @SQL
End
Close AlterColation
DeAllocate AlterColation
Go    


My problems are:
1) When an error occurs in the execution of a piece of dynamic sql the script stops and doesn't continue with the remainder
2) It only works for varchar and I think my system of isolating varchar fields is flawed (ie I may end up changing char or nvarchar fields to varchar!)
3) I'd like to exclude variables that are being used as primary keys.

Thanks in advance,

Jackie Lee
0
Comment
Question by:billy21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Author Comment

by:billy21
ID: 10724474
I've adjusted the script to ignore fields that already have the correct collation.


select 'ALTER TABLE [' + so.Name + '] ALTER COLUMN [' + sc.Name + '] varchar(' + Cast(sc.Length As VarChar(10)) + ') COLLATE SQL_Latin1_General_CP1_CI_AS'
from syscolumns sc
Inner Join sysObjects so on so.id = sc.id
Where Not (sc.CollationId Is Null)
And so.XType = 'U '
And sc.type = 39
And sc.Collation <> 'COLLATE SQL_Latin1_General_CP1_CI_AS'
Order By so.Name, sc.Name

Open AlterCollation
Fetch Next From AlterCollation
Into @SQL
WHILE @@FETCH_STATUS = 0
Begin
      Exec(@SQL)
      Fetch Next From AlterCollation
      INTO @SQL
      Print @SQL
End
Close AlterCollation
DeAllocate AlterCollation
Go  
0
 
LVL 1

Expert Comment

by:JackieLee
ID: 10724567
No Probs stand by for answer...
0
 
LVL 1

Accepted Solution

by:
JackieLee earned 500 total points
ID: 10724599
Here is something I wrote to drop all foreign key constraints.  Hope it helps...

Declare @SQL VarChar(1000)
Declare DropFKs Cursor
For
select 'ALTER TABLE [' + so1.name + '] Drop Constraint [' + so2.name + ']'
from sysobjects so1
inner join sysobjects so2 on so2.parent_obj = so1.id
where so2.xtype = 'F'
Open DropFKs
Fetch Next From DropFKs
Into @SQL
WHILE @@FETCH_STATUS = 0
Begin
      Exec(@SQL)
      Fetch Next From DropFKs
      INTO @SQL
End
Close DropFKs
DeAllocate DropFKs
Go    
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question