[T-SQL] 获取拼音

create function [dbo].[fn_GetQuanPin](@words nvarchar(2000))   
returns varchar(8000)   
as 
begin  
    declare @word nchar(1)   
    declare @pinyin varchar(8000)   
    declare @i int 
    declare @words_len int 
    declare @unicode int 
    set @i = 1   
    set @words = ltrim(rtrim(@words))   
    set @words_len = len(@words)   
    while (@i <= @words_len) --循环取字符  
    begin  
    set @word = substring(@words, @i, 1)   
    set @unicode = unicode(@word)   
    set @pinyin = ISNULL(@pinyin +SPACE(1),'')+    
    (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 'ai',N'' 
    union all select 'an',N'' 
    union all select 'ang',N'' 
    union all select 'ao',N'' 
    union all select 'ba',N'' 
    union all select 'bai',N'' --韛兡瓸  
    union all select 'ban',N'' 
    union all select 'bang',N'' 
    union all select 'bao',N'' 
    union all select 'bei',N'' 
    union all select 'ben',N'' 
    union all select 'beng',N'' 
    union all select 'bi',N'' 
    union all select 'bian',N'' 
    union all select 'biao',N'' 
    union all select 'bie',N'' 
    union all select 'bin',N'' 
    union all select 'bing',N'' 
    union all select 'bo',N'' 
    union all select 'bu',N'簿' 
    union all select 'ca',N'' 
    union all select 'cai',N'' --縩乲  
    union all select 'can',N'' 
    union all select 'cang',N'' 
    union all select 'cao',N'' 
    union all select 'ce',N'' 
    union all select 'cen',N'' 
    union all select 'ceng',N'' --硛硳岾猠乽  
    union all select 'cha',N'' 
    union all select 'chai',N'' 
    union all select 'chan',N'' 
    union all select 'chang',N'' 
    union all select 'chao',N'' 
    union all select 'che',N'' 
    union all select 'chen',N'' 
    union all select 'cheng',N'' 
    union all select 'chi',N'' 
    union all select 'chong',N'' 
    union all select 'chou',N'' 
    union all select 'chu',N'' 
    union all select 'chuai',N'' 
    union all select 'chuan',N'' 
    union all select 'chuang',N'' 
    union all select 'chui',N'' 
    union all select 'chun',N'' 
    union all select 'chuo',N'' 
    union all select 'ci',N'' --賜嗭  
    union all select 'cong',N'' 
    union all select 'cou',N'' 
    union all select 'cu',N'' 
    union all select 'cuan',N'' 
    union all select 'cui',N'' 
    union all select 'cun',N'籿' 
    union all select 'cuo',N'' 
    union all select 'da',N'' 
    union all select 'dai',N'' 
    union all select 'dan',N'' 
    union all select 'dang',N'' 
    union all select 'dao',N'' 
    union all select 'de',N'' 
    union all select 'den',N'' 
    union all select 'deng',N'' 
    union all select 'di',N'' 
    union all select 'dia',N'' 
    union all select 'dian',N'' 
    union all select 'diao',N'' 
    union all select 'die',N'' --眰嚸  
    union all select 'ding',N'' 
    union all select 'diu',N'' 
    union all select 'dong',N'' 
    union all select 'dou',N'' 
    union all select 'du',N'' 
    union all select 'duan',N'' --籪叾  
    union all select 'dui',N'' 
    union all select 'dun',N'' 
    union all select 'duo',N'' 
    union all select 'e',N'' 
    union all select 'en',N'' 
    union all select 'eng',N'' 
    union all select 'er',N'' 
    union all select 'fa',N'' 
    union all select 'fan',N'' 
    union all select 'fang',N'' 
    union all select 'fei',N'' 
    union all select 'fen',N'' 
    union all select 'feng',N'' 
    union all select 'fo',N'' 
    union all select 'fou',N'' 
    union all select 'fu',N'' --鰒猤  
    union all select 'ga',N'' 
    union all select 'gai',N'' 
    union all select 'gan',N'' 
    union all select 'gang',N'' 
    union all select 'gao',N'' 
    union all select 'ge',N'' 
    union all select 'gei',N'' 
    union all select 'gen',N'' 
    union all select 'geng',N'' --亙堩啹喼嗰  
    union all select 'gong',N'' --熕贑兝兣  
    union all select 'gou',N'' 
    union all select 'gu',N'' 
    union all select 'gua',N'詿' 
    union all select 'guai',N'' 
    union all select 'guan',N'' 
    union all select 'guang',N'' 
    union all select 'gui',N'' 
    union all select 'gun',N'' 
    union all select 'guo',N'' 
    union all select 'ha',N'' 
    union all select 'hai',N'' 
    union all select 'han',N'' 
    union all select 'hang',N'' 
    union all select 'hao',N'' 
    union all select 'he',N'' 
    union all select 'hei',N'' 
    union all select 'hen',N'' 
    union all select 'heng',N'' --堼囍  
    union all select 'hong',N'' 
    union all select 'hou',N'' 
    union all select 'hu',N'' 
    union all select 'hua',N'' 
    union all select 'huai',N'' 
    union all select 'huan',N'' 
    union all select 'huang',N'' 
    union all select 'hui',N'' 
    union all select 'hun',N'' 
    union all select 'huo',N'' 
    union all select 'ji',N'' 
    union all select 'jia',N'' 
    union all select 'jian',N'' 
    union all select 'jiang',N'' 
    union all select 'jiao',N'' 
    union all select 'jie',N'' 
    union all select 'jin',N'' 
    union all select 'jing',N'' 
    union all select 'jiong',N'' 
    union all select 'jiu',N'' 
    union all select 'ju',N'' 
    union all select 'juan',N'' 
    union all select 'jue',N'' 
    union all select 'jun',N'' 
    union all select 'ka',N'' 
    union all select 'kai',N'' --鎎乫  
    union all select 'kan',N'' 
    union all select 'kang',N'' 
    union all select 'kao',N'' 
    union all select 'ke',N'' 
    union all select 'ken',N'' 
    union all select 'keng',N'' --巪乬唟厼怾  
    union all select 'kong',N'' 
    union all select 'kou',N'' 
    union all select 'ku',N'' 
    union all select 'kua',N'' 
    union all select 'kuai',N'' 
    union all select 'kuan',N'' 
    union all select 'kuang',N'' 
    union all select 'kui',N'' 
    union all select 'kun',N'' 
    union all select 'kuo',N'' 
    union all select 'la',N'' 
    union all select 'lai',N'' 
    union all select 'lan',N'' 
    union all select 'lang',N'' 
    union all select 'lao',N'' 
    union all select 'le',N'' 
    union all select 'lei',N'' --嘞脷  
    union all select 'leng',N'' 
    union all select 'li',N'' 
    union all select 'lia',N'' 
    union all select 'lian',N'' 
    union all select 'liang',N'' 
    union all select 'liao',N'' 
    union all select 'lie',N'' 
    union all select 'lin',N'' --轥拎  
    union all select 'ling',N'' 
    union all select 'liu',N'' --瓼甅囖咯  
    union all select 'long',N'' 
    union all select 'lou',N'' 
    union all select 'lu',N'' 
    union all select 'lv',N'' 
    union all select 'luan',N'' 
    union all select 'lue',N'' 
    union all select 'lun',N'' 
    union all select 'luo',N'' 
    union all select 'ma',N'' 
    union all select 'mai',N'' 
    union all select 'man',N'' 
    union all select 'mang',N'' 
    union all select 'mao',N'' 
    union all select 'me',N'' --癦呅  
    union all select 'mei',N'' 
    union all select 'men',N'' 
    union all select 'meng',N'' --霿踎  
    union all select 'mi',N'' 
    union all select 'mian',N'' 
    union all select 'miao',N'' 
    union all select 'mie',N'' --鱴瓱  
    union all select 'min',N'' 
    union all select 'ming',N'' 
    union all select 'miu',N'' 
    union all select 'mo',N'' --耱乮  
    union all select 'mou',N'' --麰蟱  
    union all select 'mu',N'' 
    union all select 'na',N'' 
    union all select 'nai',N'' 
    union all select 'nan',N'' 
    union all select 'nang',N'' 
    union all select 'nao',N'' 
    union all select 'ne',N'' 
    union all select 'nei',N'' --嫩焾  
    union all select 'nen',N'' 
    union all select 'neng',N'' --莻嗯鈪銰啱  
    union all select 'ni',N'' 
    union all select 'nian',N'' 
    union all select 'niang',N'' 
    union all select 'niao',N'' 
    union all select 'nie',N'' 
    union all select 'nin',N'' 
    union all select 'ning',N'' 
    union all select 'niu',N'' 
    union all select 'nong',N'' 
    union all select 'nou',N'' 
    union all select 'nu',N'' 
    union all select 'nv',N'' 
    union all select 'nue',N'' 
    union all select 'nuan',N'' --硸黁燶郍  
    union all select 'nuo',N'' 
    union all select 'o',N'' --毮夞乯鞰  
    union all select 'ou',N'' 
    union all select 'pa',N'' 
    union all select 'pai',N'' --鎃磗  
    union all select 'pan',N'' 
    union all select 'pang',N'' 
    union all select 'pao',N'' 
    union all select 'pei',N'' 
    union all select 'pen',N'' 
    union all select 'peng',N'' --浌巼闏乶喸  
    union all select 'pi',N'' 
    union all select 'pian',N'' 
    union all select 'piao',N'' 
    union all select 'pie',N'' 
    union all select 'pin',N'' 
    union all select 'ping',N'' 
    union all select 'po',N'' 
    union all select 'pou',N'' --兺哛  
    union all select 'pu',N'' 
    union all select 'qi',N'' 
    union all select 'qia',N'' 
    union all select 'qian',N'' 
    union all select 'qiang',N'' --羻兛瓩  
    union all select 'qiao',N'' 
    union all select 'qie',N'' 
    union all select 'qin',N'' 
    union all select 'qing',N'' 
    union all select 'qiong',N'' 
    union all select 'qiu',N'' 
    union all select 'qu',N'' 
    union all select 'quan',N'' 
    union all select 'que',N'' 
    union all select 'qun',N'' 
    union all select 'ran',N'' 
    union all select 'rang',N'' 
    union all select 'rao',N'' 
    union all select 're',N'' 
    union all select 'ren',N'' 
    union all select 'reng',N'' 
    union all select 'ri',N'' 
    union all select 'rong',N'' 
    union all select 'rou',N'嶿' 
    union all select 'ru',N'' 
    union all select 'ruan',N'' 
    union all select 'rui',N'' 
    union all select 'run',N'' --橍挼  
    union all select 'ruo',N'' 
    union all select 'sa',N'' --櫒栍  
    union all select 'sai',N'' --簺虄  
    union all select 'san',N'' 
    union all select 'sang',N'' 
    union all select 'sao',N'' 
    union all select 'se',N'' --裇聓  
    union all select 'sen',N'' 
    union all select 'seng',N'' --閪縇  
    union all select 'sha',N'' 
    union all select 'shai',N'' 
    union all select 'shan',N'' 
    union all select 'shang',N'' 
    union all select 'shao',N'' 
    union all select 'she',N'' 
    union all select 'shen',N'' 
    union all select 'sheng',N'' 
    union all select 'shi',N'' --鰘齛兙瓧  
    union all select 'shou',N'' 
    union all select 'shu',N'' 
    union all select 'shua',N'' 
    union all select 'shuai',N'' 
    union all select 'shuan',N'' 
    union all select 'shuang',N'' 
    union all select 'shui',N'' 
    union all select 'shun',N'' 
    union all select 'shuo',N'' 
    union all select 'si',N'' --瀃螦乺  
    union all select 'song',N'' 
    union all select 'sou',N'' 
    union all select 'su',N'' 
    union all select 'suan',N'' 
    union all select 'sui',N'' 
    union all select 'sun',N'' 
    union all select 'suo',N'' 
    union all select 'ta',N'' --躢襨  
    union all select 'tai',N'' 
    union all select 'tan',N'' 
    union all select 'tang',N'' 
    union all select 'tao',N'' --討畓  
    union all select 'te',N'' 
    union all select 'teng',N'' --霯唞朰  
    union all select 'ti',N'' 
    union all select 'tian',N'' 
    union all select 'tiao',N'' 
    union all select 'tie',N'' 
    union all select 'ting',N'' --濎乭  
    union all select 'tong',N'' 
    union all select 'tou',N'' 
    union all select 'tu',N'' 
    union all select 'tuan',N'' 
    union all select 'tui',N'' 
    union all select 'tun',N'' 
    union all select 'tuo',N'' 
    union all select 'wa',N'' 
    union all select 'wai',N'' 
    union all select 'wan',N'' 
    union all select 'wang',N'' 
    union all select 'wei',N'' 
    union all select 'wen',N'' 
    union all select 'weng',N'' 
    union all select 'wo',N'' 
    union all select 'wu',N'' 
    union all select 'xi',N'' 
    union all select 'xia',N'' 
    union all select 'xian',N'' 
    union all select 'xiang',N'' 
    union all select 'xiao',N'' 
    union all select 'xie',N'' 
    union all select 'xin',N'' 
    union all select 'xing',N'' 
    union all select 'xiong',N'' 
    union all select 'xiu',N'' 
    union all select 'xu',N'' 
    union all select 'xuan',N'' 
    union all select 'xue',N'' 
    union all select 'xun',N'' 
    union all select 'ya',N'' 
    union all select 'yan',N'' 
    union all select 'yang',N'' 
    union all select 'yao',N'' 
    union all select 'ye',N'' --鸈膶岃  
    union all select 'yi',N'' 
    union all select 'yin',N'' 
    union all select 'ying',N'' 
    union all select 'yo',N'' 
    union all select 'yong',N'' 
    union all select 'you',N'' 
    union all select 'yu',N'' 
    union all select 'yuan',N'' 
    union all select 'yue',N'' 
    union all select 'yun',N'' 
    union all select 'za',N'' 
    union all select 'zai',N'' 
    union all select 'zan',N'' 
    union all select 'zang',N'' 
    union all select 'zao',N'' 
    union all select 'ze',N'' 
    union all select 'zei',N'' 
    union all select 'zen',N'' 
    union all select 'zeng',N'' 
    union all select 'zha',N'' 
    union all select 'zhai',N'' 
    union all select 'zhan',N'' 
    union all select 'zhang',N'' 
    union all select 'zhao',N'' 
    union all select 'zhe',N'' 
    union all select 'zhen',N'' 
    union all select 'zheng',N'' 
    union all select 'zhi',N'' 
    union all select 'zhong',N'' 
    union all select 'zhou',N'' 
    union all select 'zhu',N'' 
    union all select 'zhua',N'' 
    union all select 'zhuai',N'' 
    union all select 'zhuan',N'' 
    union all select 'zhuang',N'' 
    union all select 'zhui',N'' 
    union all select 'zhun',N'' 
    union all select 'zhuo',N'' 
    union all select 'zi',N'' --漬唨  
    union all select 'zong',N'' 
    union all select 'zou',N'' 
    union all select 'zu',N'' 
    union all select 'zuan',N'' 
    union all select 'zui',N'' 
    union all select 'zun',N'' 
    union all select 'zuo',N'') t    
    where word >= @word collate Chinese_PRC_CS_AS_KS_WS    
    order by word collate Chinese_PRC_CS_AS_KS_WS ASC) else @word end)   
    set @i = @i + 1   
    end  
    return @pinyin   
END 
原文地址:https://www.cnblogs.com/wsion/p/4189255.html