脸颊贴近月球
===========================================================
无法解决 equal to 操作的排序规则冲突(ZT)
===========================================================

今天遇到错误,无法解决 equal to 操作的排序规则冲突,查到一篇不错的文章,收藏了


SQL SERVER的排序规则平时使用不是很多,也许不少初学者还比较陌生,但有
一个错误大家应是经常碰到: SQL SERVER数据库,在跨库多表连接查询时,若两数据
库默认字符集不同,系统就会返回这样的错误:
??????
?????????? “无法解决 equal to 操作的排序规则冲突。”

一.错误分析:

  这个错误是因为排序规则不一致造成的,我们做个测试,比如:
create table #t1(
name varchar(20) collate Albanian_CI_AI_WS,?
value int)

create table #t2(
name varchar(20) collate Chinese_PRC_CI_AI_WS,???
value int )

表建好后,执行连接查询:

select * from #t1 A inner join #t2 B on A.name=B.name

这样,错误就出现了:

?????????? 服务器: 消息 446,级别 16,状态 9,行 1
?????????? 无法解决 equal to 操作的排序规则冲突。
  要排除这个错误,最简单方法是,表连接时指定它的排序规则,这样错误就
不再出现了。语句这样写:

select *
from #t1 A inner join #t2 B
on A.name=B.name collate Chinese_PRC_CI_AI_WS


二.排序规则简介:

??? 什么叫排序规则呢?MS是这样描述的:"在 Microsoft SQL Server 2000 中,
字符串的物理存储由排序规则控制。排序规则指定表示每个字符的位模式以及存
储和比较字符所使用的规则。"
  在查询分析器内执行下面语句,可以得到SQL SERVER支持的所有排序规则。

    select * from ::fn_helpcollations()

排序规则名称由两部份构成,前半部份是指本排序规则所支持的字符集。
如:
  Chinese_PRC_CS_AI_WS
前半部份:指UNICODE字符集,Chinese_PRC_指针对大陆简体字UNICODE的排序规则。
排序规则的后半部份即后缀 含义:
  _BIN 二进制排序
  _CI(CS) 是否区分大小写,CI不区分,CS区分
  _AI(AS) 是否区分重音,AI不区分,AS区分   
  _KI(KS) 是否区分假名类型,KI不区分,KS区分 
??? _WI(WS) 是否区分宽度 WI不区分,WS区分 

区分大小写:如果想让比较将大写字母和小写字母视为不等,请选择该选项。
区分重音:如果想让比较将重音和非重音字母视为不等,请选择该选项。如果选择该选项,
???????? 比较还将重音不同的字母视为不等。
区分假名:如果想让比较将片假名和平假名日语音节视为不等,请选择该选项。
区分宽度:如果想让比较将半角字符和全角字符视为不等,请选择该选项


三.排序规则的应用:

  SQL SERVER提供了大量的WINDOWS和SQLSERVER专用的排序规则,但它的应用往往
被开发人员所忽略。其实它在实践中大有用处。

  例1:让表NAME列的内容按拼音排序:

create table #t(id int,name varchar(20))
insert #t select 1,'中'
union all select 2,'国'
union all select 3,'人'
union all select 4,'阿'

select * from #t order by name collate Chinese_PRC_CS_AS_KS_WS
drop table #t
/*结果:
id????????? name????????????????
----------- --------------------
4?????????? 阿
2?????????? 国
3?????????? 人
1?????????? 中
*/

  例2:让表NAME列的内容按姓氏笔划排序:

create table #t(id int,name varchar(20))

insert #t select 1,'三'
union all select 2,'乙'
union all select 3,'二'
union all select 4,'一'
union all select 5,'十'
select * from #t order by name collate Chinese_PRC_Stroke_CS_AS_KS_WS?
drop table #t
/*结果:
id????????? name????????????????
----------- --------------------
4?????????? 一
2?????????? 乙
3?????????? 二
5?????????? 十
1?????????? 三
*/

四.在实践中排序规则应用的扩展

  SQL SERVER汉字排序规则可以按拼音、笔划等排序,那么我们如何利用这种功能
来处理汉字的一些难题呢?我现在举个例子:

          用排序规则的特性计算汉字笔划

  要计算汉字笔划,我们得先做准备工作,我们知道,WINDOWS多国汉字,UNICODE目前
收录汉字共20902个。简体GBK码汉字UNICODE值从19968开始。
  首先,我们先用SQLSERVER方法得到所有汉字,不用字典,我们简单利用SQL语句就
可以得到:

select top 20902 code=identity(int,19968,1) into #t from syscolumns a,syscolumns b

再用以下语句,我们就得到所有汉字,它是按UNICODE值排序的:

  select code,nchar(code) as CNWord from #t

  然后,我们用SELECT语句,让它按笔划排序。

select code,nchar(code) as CNWord
from #t
order by nchar(code) collate Chinese_PRC_Stroke_CS_AS_KS_WS,code

结果:
code??????? CNWord
----------- ------
19968?????? 一
20008?????? 丨
20022?????? 丶
20031?????? 丿
20032?????? 乀
20033?????? 乁
20057?????? 乙
20058?????? 乚
20059?????? 乛
20101?????? 亅
19969?????? 丁
..........

 ? 从上面的结果,我们可以清楚的看到,一笔的汉字,code是从19968到20101,从小到大排,但到
了二笔汉字的第一个字“丁”,CODE为19969,就不按顺序而重新开始了。有了这结果,我们就可以轻
松的用SQL语句得到每种笔划汉字归类的第一个或最后一个汉字。
下面用语句得到最后一个汉字:

create table #t1(id int identity,code int,cnword nvarchar(2))

insert #t1(code,cnword)
select code,nchar(code) as CNWord? from #t
order by nchar(code) collate Chinese_PRC_Stroke_CS_AS_KS_WS,code


select A.cnword
from #t1 A
left join #t1 B on A.id=B.id-1 and A.codewhere B.code is null
order by A.id

得到36个汉字,每个汉字都是每种笔划数按Chinese_PRC_Stroke_CS_AS_KS_WS排序规则排序后的
最后一个汉字:

亅阝马风龙齐龟齿鸩龀龛龂龆龈龊龍龠龎龐龑龡龢龝齹龣龥齈龞麷鸞麣龖龗齾齉龘

  上面可以看出:“亅”是所有一笔汉字排序后的最后一个字,“阝”是所有二笔汉字排序后的最后
一个字......等等。
  但同时也发现,从第33个汉字“龗(33笔)”后面的笔划有些乱,不正确。但没关系,比“龗”笔划
多的只有四个汉字,我们手工加上:齾35笔,齉36笔,靐39笔,龘64笔

建汉字笔划表(TAB_HZBH):
create table tab_hzbh(id int identity,cnword nchar(1))
--先插入前33个汉字
insert tab_hzbh
select top 33 A.cnword
from #t1 A
left join #t1 B on A.id=B.id-1 and A.codewhere B.code is null
order by A.id
--再加最后四个汉字
set identity_insert tab_hzbh on
go
insert tab_hzbh(id,cnword)
     select 35,N'齾'
union all select 36,N'齉'
union all select 39,N'靐'
union all select 64,N'龘'
go
set identity_insert tab_hzbh off
go

  到此为止,我们可以得到结果了,比如我们想得到汉字“国”的笔划:

declare @a nchar(1)
set @a='国'
select top 1 id
from? tab_hzbh
where cnword>=@a collate Chinese_PRC_Stroke_CS_AS_KS_WS
order by id

id?????????
-----------
8
(结果:汉字“国”笔划数为8)

  上面所有准备过程,只是为了写下面这个函数,这个函数撇开上面建的所有临时表和固
定表,为了通用和代码转移方便,把表tab_hzbh的内容写在语句内,然后计算用户输入一串
汉字的总笔划:

create function fun_getbh(@str nvarchar(4000))
returns int
as
begin
declare @word nchar(1),@n int
set @n=0
while len(@str)>0
begin
set @word=left(@str,1)
--如果非汉字,笔划当0计
set @n=@n+(case when unicode(@word) between 19968 and 19968+20901
then (select top 1 id from (
select 1 as id,N'亅' as word
union all select 2,N'阝'
union all select 3,N'马'
union all select 4,N'风'
union all select 5,N'龙'
union all select 6,N'齐'
union all select 7,N'龟'
union all select 8,N'齿'
union all select 9,N'鸩'
union all select 10,N'龀'
union all select 11,N'龛'
union all select 12,N'龂'
union all select 13,N'龆'
union all select 14,N'龈'
union all select 15,N'龊'
union all select 16,N'龍'
union all select 17,N'龠'
union all select 18,N'龎'
union all select 19,N'龐'
union all select 20,N'龑'
union all select 21,N'龡'
union all select 22,N'龢'
union all select 23,N'龝'
union all select 24,N'齹'
union all select 25,N'龣'
union all select 26,N'龥'
union all select 27,N'齈'
union all select 28,N'龞'
union all select 29,N'麷'
union all select 30,N'鸞'
union all select 31,N'麣'
union all select 32,N'龖'
union all select 33,N'龗'
union all select 35,N'齾'
union all select 36,N'齉'
union all select 39,N'靐'
union all select 64,N'龘'
) T
where word>=@word collate Chinese_PRC_Stroke_CS_AS_KS_WS
order by id ASC) else 0 end)
set @str=right(@str,len(@str)-1)
end
return @n
end

--函数调用实例:
select dbo.fun_getbh('中华人民共和国'),dbo.fun_getbh('中華人民共和國')
 
  执行结果:笔划总数分别为39和46,简繁体都行。

当然,你也可以把上面“UNION ALL”内的汉字和笔划改存在固定表内,在汉字
列建CLUSTERED INDEX,列排序规则设定为:
    Chinese_PRC_Stroke_CS_AS_KS_WS
这样速度更快。如果你用的是BIG5码的操作系统,你得另外生成汉字,方法一样。
但有一点要记住:这些汉字是通过SQL语句SELECT出来的,不是手工输入的,更不
是查字典得来的,因为新华字典毕竟不同于UNICODE字符集,查字典的结果会不正
确。

  
?????????     用排序规则的特性得到汉字拼音首字母

  用得到笔划总数相同的方法,我们也可以写出求汉字拼音首字母的函数。如下:

create function fun_getPY(@str nvarchar(4000))
returns nvarchar(4000)
as
begin
declare @word nchar(1),@PY nvarchar(4000)
set @PY=''
while len(@str)>0
begin
set @word=left(@str,1)
--如果非汉字字符,返回原字符
set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
then (select top 1 PY from (
select 'A' as PY,N'驁' as word
union all select 'B',N'簿'
union all select 'C',N'錯'
union all select 'D',N'鵽'
union all select 'E',N'樲'
union all select 'F',N'鰒'
union all select 'G',N'腂'
union all select 'H',N'夻'
union all select 'J',N'攈'
union all select 'K',N'穒'
union all select 'L',N'鱳'
union all select 'M',N'旀'
union all select 'N',N'桛'
union all select 'O',N'漚'
union all select 'P',N'曝'
union all select 'Q',N'囕'
union all select 'R',N'鶸'
union all select 'S',N'蜶'
union all select 'T',N'籜'
union all select 'W',N'鶩'
union all select 'X',N'鑂'
union all select 'Y',N'韻'
union all select 'Z',N'咗'
) T
where word>=@word collate Chinese_PRC_CS_AS_KS_WS
order by PY ASC) else @word end)
set @str=right(@str,len(@str)-1)
end
return @PY
end

--函数调用实例:
select dbo.fun_getPY('中华人民共和国'),dbo.fun_getPY('中華人民共和國')
结果都为:ZHRMGHG

 ? 你若有兴趣,也可用相同的方法,扩展为得到汉字全拼的函数,甚至还可以得到全拼的读
音声调,不过全拼分类大多了。得到全拼最好是用对照表,两万多汉字搜索速度很快,用对照
表还可以充分利用表的索引。
??? 排序规则还有很多其它的巧妙用法,限于篇幅在此就不再详细说明。欢迎大家共同探讨。


myhimalayas 发表于:2005.04.26 08:55 ::分类: ( SQL Server ) ::阅读:(59213次) :: 评论 (8)
re: 无法解决 equal to 操作的排序规则冲突(ZT) [回复]

laughing 果然很有帮助

Geena 评论于: 2007.05.09 11:21
Some xxx movies [回复]

black porn star
asian porn
gay porn
anal porn
porn game
celebrity porn
toon porn
free gay porn
young porn
free picture xxx
clitoris
free sex clip
russian sex
amateur sex
free porn site
porn dvd
live sex cam
hot porn
mature porn
hardcore sex
hardcore porn
sex woman
micro bikini
porn movie
free sex video
free sex story
teen porn
lesbian sex
download porn
free xxx porn
porn clip
mature sex
paris hilton
asian sex
free password xxx
sex offender
sex video
transsexuals
hot sex
world sex
porn video
free porn movie
disney porn
porn star finder
free porn xxx
porn site
clip free video xxx
free teen porn
black porn
sex position
ebony porn
oral sex
teen sex
asian porn star
free porn video clip
live sex
group sex
shemale
indian porn
porn film
video porn gratis
latina porn
download sex movie
clip free xxx
porn trailer
free xxx webcam
sex chat
cyber sex
virgin teens
free black porn
free sex pic
fisting
free porn vids
sex movie
sex toy
free trailer xxx
my first sex teacher
live porn
free porn trailer
porn star
man porn
anal sex
twins sex
free movie xxx
adult porn
incest sex
free xxx
free hardcore porn
midget porn
interracial porn
big butts
indian sex
homemade porn
sex picture
reality porn
sex dating
free lesbian porn
sex game
adult sex movie
porn picture
classic porn
hard porn
vintage porn
cartoon porn
free porn downloads
japanese porn
free story xxx
porn gratis
sex story
porn comic
porn cam
hd xxx movies
lesbian porn
pink porn star
porn sex
adult sex
porno casting
black sex
pokemon porn
free porn gallery
double penetration
arabian sex
anime porn
free video xxx
free asian porn
xxx porn

Bombino 评论于: 2007.08.22 01:28
[url=][/url] [回复]

[url=][/url]

lheke 评论于: 2007.12.21 09:45
[url=][/url] [回复]

[url=][/url]

cvvlam 评论于: 2008.01.03 03:35
re: 无法解决 equal to 操作的排序规则冲突(ZT) [回复]

were voluntarily www.nationalwebmeds.com phentermine diet taken off were voluntarily phentermine diet taken off were voluntarily [url=www.nationalwebmeds.com]phentermine diet[/url] taken off were voluntarily [URL]www.nationalwebmeds.com[/URL] phentermine diet taken off the market www.myinternetmedications.com phentermine no prescription at the the market phentermine no prescription at the the market [url=www.myinternetmedications.com]phentermine no prescription[/url] at the the market [URL]www.myinternetmedications.com[/URL] phentermine no prescription at the request of www.discountmedsplace.com buy phentermine online the FDA. request of buy phentermine online the FDA. request of [url=www.discountmedsplace.com]buy phentermine online[/url] the FDA. request of [URL]www.discountmedsplace.com[/URL] buy phentermine online the FDA. Studies later www.onlinemedstoday.com buying viagra proved that Studies later buying viagra proved that Studies later [url=www.onlinemedstoday.com]buying viagra[/url] proved that Studies later [URL]www.onlinemedstoday.com[/URL] buying viagra proved that nearly 30% www.medspointstore.com cheapest viagra of people nearly 30% cheapest viagra of people nearly 30% [url=www.medspointstore.com]cheapest viagra[/url] of people nearly 30% [URL]www.medspointstore.com[/URL] cheapest viagra of people

Chjfkl Chjfklcnt 评论于: 2008.06.29 04:37
re: 无法解决 equal to 操作的排序规则冲突(ZT) [回复]

1/6th that www.onlinemedscentral.com phentermine cod of morphine). 1/6th that phentermine cod of morphine). 1/6th that [url=www.onlinemedscentral.com]phentermine cod[/url] of morphine). 1/6th that [URL]www.onlinemedscentral.com[/URL] phentermine cod of morphine). The (+)-enantiomer www.bestmedsplace.com tramadol ultram is approximately The (+)-enantiomer tramadol ultram is approximately The (+)-enantiomer [url=www.bestmedsplace.com]tramadol ultram[/url] is approximately The (+)-enantiomer [URL]www.bestmedsplace.com[/URL] tramadol ultram is approximately four times www.bargainmedsplace.com generic phentermine more potent four times generic phentermine more potent four times [url=www.bargainmedsplace.com]generic phentermine[/url] more potent four times [URL]www.bargainmedsplace.com[/URL] generic phentermine more potent than the www.medsthrifty.com phentermine no prescription (-)-enantiomer in than the phentermine no prescription (-)-enantiomer in than the [url=www.medsthrifty.com]phentermine no prescription[/url] (-)-enantiomer in than the [URL]www.medsthrifty.com[/URL] phentermine no prescription (-)-enantiomer in terms of www.bestcheapmedsworld.com discount tramadol ?-opioid receptor terms of discount tramadol ?-opioid receptor terms of [url=www.bestcheapmedsworld.com]discount tramadol[/url] ?-opioid receptor terms of [URL]www.bestcheapmedsworld.com[/URL] discount tramadol ?-opioid receptor

Hskbmx Hskbmxoou 评论于: 2008.06.29 05:16
If You want to delete your site from my base [回复]

to: Admin - If You want to delete your site from my spam list, please sent url of your domain to my e-mail: stop.spam.today@gmail.com
And I will remove your site from my base within 24 hours
webmastegz

tubUnfott 评论于: 2008.11.17 03:21
car insurance lower rate [回复]

finance company car insurance
car insurance pricing
west allis car insurance rates
car insurance swift
Look at great [url=http://car-insurance-hirx.netfirms.com/just-cars-car-insurance.html]just cars car insurance[/url]

car insurance per month
arizona cheap car insurance
car insurance overseas rental
national farmer union car insurance
car insurance grand fork minnesota
nc car insurance quotes
car insurance medical conditions
car insurance quote for northern ireland
car insurance florence south carolina
ladies car insurance uk
direct line car insurance spain
florida car insurance laws
century 21 car insurance quote
how to sell car insurance
affordable car insurance texas
brentacre car insurance
21 car insurance
jap car insurance

Lalgaulleywak 评论于: 2008.11.22 09:01

发表评论
标题

在此添加评论
表情符号: smile laughing tongue angry crying sad wassat wink

称呼

邮箱地址(可选)

个人主页(可选)




切换风格
新闻聚合
博客日历
文章归档...
最新发表...
博客统计...
网站链接...