最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

自动清理MSSQLServerTableCollation问题的解决方法

来源:动视网 责编:小采 时间:2020-11-09 07:07:56
文档

自动清理MSSQLServerTableCollation问题的解决方法

自动清理MSSQLServerTableCollation问题的解决方法:在一个团队项目中,没有约定好Collation, 在 MS SQL Server中编程就会遇到这样的问题:Cannot resolve the collation conflict between Latin1_General_CI_AS and SQL_Latin1_General_CP1_CI_AS in the eq
推荐度:
导读自动清理MSSQLServerTableCollation问题的解决方法:在一个团队项目中,没有约定好Collation, 在 MS SQL Server中编程就会遇到这样的问题:Cannot resolve the collation conflict between Latin1_General_CI_AS and SQL_Latin1_General_CP1_CI_AS in the eq


在一个团队项目中,没有约定好Collation, 在 MS SQL Server中编程就会遇到这样的问题:
Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.

因为编码问题,不同编码的字符串并不能直接进行比较,这种有两个解决方法,1是在 query中 指定 用某一个collation进行比较, 另外一个就是修改column的 collation type 来避免这种错误。

第二种方法,一个一个column改起来很累,写了个script,除了 被当作 constraints比如 primary key, foreign key之外的 varchar, char,nvarchar 都可以统一修改成一个collation…

完整sql代码:
代码如下:
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<>@CollationName
and c.name not in (   
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;

文档

自动清理MSSQLServerTableCollation问题的解决方法

自动清理MSSQLServerTableCollation问题的解决方法:在一个团队项目中,没有约定好Collation, 在 MS SQL Server中编程就会遇到这样的问题:Cannot resolve the collation conflict between Latin1_General_CI_AS and SQL_Latin1_General_CP1_CI_AS in the eq
推荐度:
标签: sql table collation
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top