一个词组转化成拼音的函数

Posted on 2009-04-11 15:01  GeorgeHuang  阅读(401)  评论(1)    收藏  举报

该函数是用SQL SERVER 2008编写的,由于时间有限,目前还存在比较多问题有待解决:如需要一个有字的拼音的数据库(有些数据库只有字的首拼音,不合适),目前这个函数只能对纯中文字(中间没有任何标点符号,字母或者特殊字符)的一串文字进行转换.有时间我会完善这个函数,同时请大家多多指点

create function streetii_PinYin_Convert_Class
(
 @word nvarchar(128)
)
returns @temp table
(
 id  int identity(1,1),
 word nvarchar(128),
 pinyin nvarchar(255)
)
as
begin
 declare @pinyin nvarchar(16)
 declare @char nvarchar(4)
 declare @currentindex int=1
 declare @totalpinyin nvarchar(256)=''
 declare @length  int=len(@word) --检测字符串的长度
 
 if @word is not null
 begin
  while @currentindex<=@length
  begin
    set @char=substring(@word,@currentindex,1)--逐个字符拆分,并赋值给变量@char
    set @pinyin =(select pinyin from streetii_pinyinforword where word=@char)--获取@char里面的字符的拼音
    set @totalpinyin=@totalpinyin+@pinyin--加入到变量中
    set @currentindex=@currentindex+1--检测下一个汉字
  end
  insert into @temp values(@word,@totalpinyin)
 end
return
end


/*
select * from streetii_PinYin_Convert_Class ('北京欢迎你')
*/