---
title: "自动清理 MS SQL Server Table Collation 问题 转载"
author: "java032"
date: "2012-04-10T22:17:16Z"
tags: []
categories: ["数据库"]
slug: "ms-sql-server-table-collation"
draft: false
lastmod: "2012-04-10T22:21:41Z"
id: 1482155
url: "https://java032.iteye.com/blog/1482155"
---

Cannot resolve the collation conflict between "Latin1\_General\_CI\_AS" and "SQL\_Latin1\_General\_CP1\_CI\_AS" in the equal to operation.

 

 

 

declare @CollationName varchar(500);

   set @CollationName = 'SQL\_Latin1\_General\_CP1\_CI\_AS'  
   
   create table #tmp (sqlStr varchar(max));

   insert into #tmp

   select  'alter table \[' + o.name + '] alter column \[' + c.name + ']' +

   (case c.system\_type\_id when 167 then ' varchar(' when 175 then ' char(' else ' nvarchar(' end)

  + convert(varchar,c.max\_length) + ') collate ' + @CollationName

  from sys.columns c,

      sys.objects o

   where  c.object\_id=o.object\_id and o.type='U' and c.system\_type\_id in (167,175,231) and collation\_name&lt;&gt;@CollationName

   and c.name not in (   

  15:     select     cc.COLUMN\_NAME

      from    

          INFORMATION\_SCHEMA.TABLE\_CONSTRAINTS pk ,

          INFORMATION\_SCHEMA.KEY\_COLUMN\_USAGE cc

       where    

           cc.TABLE\_NAME = pk.TABLE\_NAME

          and    cc.CONSTRAINT\_NAME = pk.CONSTRAINT\_NAME)

   

   while (exists (select * from #tmp))

   begin

       declare @sqlStr varchar(max);

       select @sqlStr=(select top 1 sqlstr from #tmp);

       exec(@sqlStr)

       delete from #tmp where sqlStr=@sqlStr

   end

   

   drop table #tmp;