Solved

Collation Problem

Posted on 2004-09-29
9
1,488 Views
Last Modified: 2011-10-03
This is a strange one.

I have a SQL2000 server that has been set up for collation SQL_Latin1_General_Pref_CP850_CI_AS

However when i add a new table, the collation for varchar fileds is created as SQL_Latin1_General_CP1_CI_AS

What the hell is going on?

All the tables i have moved from my otherserver that were cp850 are now also cp1! How can i get the default collation to work again?

Thanks

Simon
0
Comment
Question by:CaptainRantflaps
  • 5
  • 4
9 Comments
 
LVL 12

Accepted Solution

by:
patrikt earned 250 total points
ID: 12178108
In SQL 2000 is server collation used only for compatibility and as default, but default for new databases not tables.
Table colleation default is collation of its database.

Look on database collation.

Patrik
0
 
LVL 5

Author Comment

by:CaptainRantflaps
ID: 12178185
Ok, as a final note, dont worry patrik, the points are already yours, but is there any easy wasy to alter every col in every table already created to the new collation?

0
 
LVL 12

Expert Comment

by:patrikt
ID: 12178205
Not easy. There is way how to list columns in existing tables so there must be way how to construst ALTER statements.

Give me a minute and I'll try to find.
0
 
LVL 12

Expert Comment

by:patrikt
ID: 12178275
Not tested but you can try this:

-----

declare @table varchar(100),@coll varchar(100),@sql varchar(1000)

declare tab  cursor for
select *,sysobjects.name as tbl, syscolumns.name as coll from syscolumns
inner join  sysobjects on sysobjects.id=syscolumns.id
where collation is not null
and sysobjects.xtype='U'

OPEN tab

FETCH NEXT FROM tab INTO @table, @coll

WHILE @@fetch_status=0
BEGIN
      set @sql='ALTER TABLE '+@table+' ALTER COLUMN '+@coll+'COLLATE Latin1_General_CI_AS'
      EXECUTE @sql
      SELECT @table,@coll,@sql
      FETCH NEXT FROM tab INTO @table, @coll
END

close tab
DEALLOCATE  tab
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 5

Author Comment

by:CaptainRantflaps
ID: 12178291
Thaks Patrik, My network admin set the server up last week when I was off, and then transfered all 200+ tables over!

DOH!

Your help is must appreciated, i will give it a spin.
0
 
LVL 12

Expert Comment

by:patrikt
ID: 12178300
Let me know how it worked.
For 200+ tables you have to check statements produced by that procedure first and then run it.
0
 
LVL 5

Author Comment

by:CaptainRantflaps
ID: 12178541
Hi Patrik,

Still having a little trouble,

I amended the statement to remove the * from select * etc, and added a space in the alter statement - the whole thing now reads (see below) however there is a syntax error in the statement created near the collate statement. The statement produced is


ALTER TABLE table1 ALTER COLUMN test1 COLLATE Latin1_General_CI_AS

however looking it up, i need to include the datatype of the column ie


ALTER TABLE table1 ALTER COLUMN test1 char(10) COLLATE Latin1_General_CI_AS

Is there anyway you know of to get this from the system tables, because the tables all have differing datatypes and lengths etc.

Thanks again,

Simon




----------------------------------------------------------------------------------------------

declare @table varchar(100),@coll varchar(100),@sql varchar(1000)

declare tab  cursor for
select sysobjects.name as tbl, syscolumns.name as coll from syscolumns
inner join  sysobjects on sysobjects.id=syscolumns.id
where collation is not null
and sysobjects.xtype='U'

OPEN tab

FETCH NEXT FROM tab INTO @table, @coll

WHILE @@fetch_status=0
BEGIN
     set @sql='ALTER TABLE '+@table+' ALTER COLUMN '+@coll+' COLLATE Latin1_General_CI_AS'
     EXECUTE @sql
     SELECT @table,@coll,@sql
     FETCH NEXT FROM tab INTO @table, @coll
END

close tab
DEALLOCATE  tab

----------------------------------------------------------------------------------------------

0
 
LVL 12

Expert Comment

by:patrikt
ID: 12178630
Ok. Whats about this:

declare @table varchar(100),@coll varchar(100),@sql varchar(1000),@type varchar(100),@isnull varchar(100)


declare tab  cursor for
select
table_name, column_name,data_type+'('+cast(character_maximum_length as varchar)+')',
case IS_NULLABLE WHEN 'No' THEN  'NOT NULL' ELSE '' END
from INFORMATION_SCHEMA.COLUMNS
where collation_name is not null
and table_name not like 'sys%'

OPEN tab

FETCH NEXT FROM tab INTO @table, @coll, @type,@isnull

WHILE @@fetch_status=0
BEGIN
      set @sql='ALTER TABLE '+@table+' ALTER COLUMN '+@coll+' '+@type+' COLLATE Latin1_General_CI_AS '+@isnull
      --EXECUTE @sql
      SELECT @sql
      FETCH NEXT FROM tab INTO @table, @coll, @type,@isnull
END

close tab
DEALLOCATE  tab

0
 
LVL 5

Author Comment

by:CaptainRantflaps
ID: 12178648
Thanks brilliant works perfectly , thanks once again for your help.

Simon
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now