MySQL实现汉字转拼音,赶快一起学起来( 二 )


'Q','Q'),'R','R'),'S','S'),'T','T'),'U','U'),'V','V'),'W','W'),'X','X'),
'Y','Y'),'Z','Z'),'+',''),'-',''),'*',''),'/',''),
'[',''),']',''),'{',''),'}',''),'(',''),')',''),
'<',''),'>',''),'《',''),'》',''),'(',''),')',''),'"',''),
'“',''),'”',''),'.',''),' 。',''),'-',''),'-',''),'/',''),
'/',''),'',''),' ',''),'1','一'),'2','二'),'3','三'),
'4','四'),'5','五'),'6','六'),'7','七'),'8','八'),'9','九'),'0','零') R3
into V_NAME_TEMP
from dual;
 
#循环获得字符串拼音码
myloop:loop
if V_NAME is null then
leave myloop;
end if;
 
select substr(V_NAME_TEMP, i, 1) into V_NAME_SIN from dual;
set i=i+1;
if V_NAME_SIN <> ' ' then
select count(*)
into v_counter1
from bst_wbjq
where bst_wbjq.charactor=v_name_sin;
if v_counter1 > 0 then
select WORD
into V_PINYIN_SIN
from bst_wbjq
where bst_wbjq.CHARACTOR=V_NAME_SIN
limit 1;
select concat_ws('',V_PINYIN_TEMP,V_PINYIN_SIN)
into V_PINYIN_TEMP
from dual;
end if;
end if;
select char_length(V_NAME) into j from dual;
if i > j then
leave myloop;
end if;
end loop;
#截取32位长度字符
if char_length(V_PINYIN_TEMP) > 32 then
select substr(V_PINYIN_TEMP, 1, 32) into V_PYM from dual;
else
select V_PINYIN_TEMP into V_PYM from dual;
end if;
end;
$$
delimiter ;
3.2. 创建存储过程:SP_PINYIN -- ----------------------------
-- procedure structure for SP_PINYIN
-- ----------------------------
delimiter $$
drop procedure if exists SP_PINYIN;
$$
create procedure SP_PINYIN(IN hanzi varchar(256),OUT pinyin varchar(256))
begin
declare aword varchar(200);
declare aresult varchar(200);
declare temp1 varchar(20);
declare len int default 0;
declare point int default 1;
declare charword varchar(20);
declare charlen int default 1;
#定义游标标志变量
declare done int default false;
#定义游标
declare cur_pinyin cursor for
select PY from TBL_PINYIN
where word=substr(aword, point, charlen);
#指定游标循环结束时的返回值
declare continue HANDLER for not found set done=true;
select ltrim(rtrim(hanzi)) into aword from dual;
select char_length(aword) into len from dual;
#<<LABEL1>>
while point <= len do
select '' into temp1 from dual;
select substr(aword, point, 1) into charword from dual;
if (charword is not null and charword != ' ') then
select concat_ws(' ',aresult,charword) into aresult from dual;
else
select 2 into charlen from dual;
end if;
#打开游标
open cur_pinyin;
#开始循环处理游标里的数据
read_loop:loop
#获得游标当前指向的一条数据
fetch cur_pinyin into temp1;
#判断游标的循环是否结束
if done then
leave read_loop;
end if;
end loop; #结束游标循环
#关闭游标
close cur_pinyin;
if (point = 1) then
set aresult = temp1;
else
select concat_ws(' ',aresult,temp1) into aresult from dual;
end if;
select point+charlen into point from dual;
end while;
#输出结果
select aresult into pinyin from dual;
end;
$$
delimiter ;
3.3. 创建函数:to_pinyin -- ----------------------------
-- function structure for to_pinyin
-- ----------------------------
delimiter $$
drop function if exists to_pinyin;
$$
create function to_pinyin(v_hanzi varchar(256),v_type int)
returns varchar(256)
begin
declare strTemp VARCHAR(200);
declare strResult VARCHAR(200);
declare strHanzi VARCHAR(200);
declare strTemp1 VARCHAR(200);
declare v_subb VARCHAR(100);
declare V_NAME_TEMP VARCHAR(200);


推荐阅读