zoukankan      html  css  js  c++  java
  • SQL Server 排序规则(摘)

    3Sql 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
    解决这个问题语句可以这样写:
    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区分
    区分大小写:是否想让比较将大写字母和小写字母视为不等
    区分重音:是否想让比较将重音和非重音字母视为不等
    区分假名:是否想让比较将片假名和平假名日语音节视为不等
    区分宽度:是否想让比较将半角字符和全角字符视为不等

    三、排序规则的应用:
    例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   

    droptable #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           三
    */

    四、在实践中排序规则应用的扩展
    例1:用排序规则的特性计算汉字笔划
    要计算汉字笔划,Windows多国汉字,Unicode目前收录汉字共20902个。简体GBK码汉字Unicode值从19968开始。
    首先,我们先用SqlServer方法得到所有汉字,用Sql语句就可以得到:
    select top 20902 code=identity(int,19968,1into #t from syscolumns a,syscolumns b
    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是从19968到20101,从小到大排,但到了二笔汉字的第一个字“丁”,code为19969,就不按顺序而重新开始了。有了这个结果,就可以轻松用Sql语句得到每种笔划汉字归类的第一个或最后一个汉字。
    下面用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.code<B.code where 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.code<B.code where B.code is null order by A.id

    --再加最后四个汉字
    set identity_insert tab_hzbh on

    insert tab_hzbh(id,cnword) select 35,N'' union all select 36,N'' union all select 39,N'' union all select 64,N''
    set identity_insert tab_hzbh off
    到此为止,我们就可以得到结果了。比如我们想得到汉字“国”的笔划:
    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
    (结果:汉字“国”笔划数为8)
    有了上面的准备,可以写这样一个函数,用来计算字符串中汉字的笔划数:
    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(@wordbetween 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 ASCelse 0 end)
            set @str=right(@str,len(@str)-1)
        end
        return @n
    end

    函数调用实例:

    select dbo.fun_getbh('中华人民共和国'),dbo.fun_getbh('中華人民共和國')
    执行结果:笔划总数分别为36和46。
    例2:用排序规则得到汉字拼音首字母
    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(@wordbetween 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 ASCelse @word end)
            set @str=right(@str,len(@str)-1)
        end
        return @PY
    end
    函数调用实例:
    select dbo.fun_getPY('中华人民共和国'),dbo.fun_getPY('中華人民共和國')
    执行结果:ZHRMGHG

    例3:获取汉字字符串的拼音首字母
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fGetPy]'and xtype in (N'FN', N'IF', N'TF'))
        drop function [dbo].[fGetPy]
    GO
    --创建取拼音函数
    create function fGetPy(@Str varchar(500)='')

    returns varchar(500)
    as
    begin
     declare @strlen int,@return varchar(500),@ii int
     declare @n int,@c char(1),@chn nchar(1)

     select @strlen=len(@str),@return='',@ii=0
     set @ii=0
     while @ii<@strlen
     begin
      select @ii=@ii+1,@n=63,@chn=substring(@str,@ii,1)
      if @chn>'z'
      select @n = @n +1
         ,@c = case chn when @chn then char(@nelse @c end
       from(
        select top 27 * from (
         select chn = ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''  --because have no 'i'
         union all select ''

         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''
         union all select ''  --no 'u'
         union all select ''  --no 'v'
         union all select ''

         union all select ''
         union all select ''
         union all select ''
         union all select @chnas a
        order by chn COLLATE Chinese_PRC_CI_AS 
       ) as b
      else set @c='a'
      set @return=@return+@c
     end
     return(@return)
    end
    go
    --测试
    select dbo.fgetpy('东莞市'as 东莞市,dbo.fgetpy('ab中c国人'as 中国人

    --删除拼音函数
    drop function fgetpy 
    -------------------------------------------------------------------------------------------------------------------------------------------
    摘自:http://www.cnblogs.com/hzuIT/articles/947411.html

  • 相关阅读:
    软件工程结对作业02(原创版)
    第六周学习进度条
    软件工程结对作业02(借鉴版)
    软件工程课堂测试06
    第五周学习进度条
    构建之法阅读笔记02
    软件工程个人作业03
    第四周学习进度条
    构建之法阅读笔记01
    CentOS7下解决ifconfig command not found的办法
  • 原文地址:https://www.cnblogs.com/sydeveloper/p/2791157.html
Copyright © 2011-2022 走看看