今天接到一个新的业务需求,客户需要将指定的中文汉字转换成拼音(含:简拼、首全拼、尾全拼)。1。创建基础数据表 Tablestructureforbstwbjq DROPTABLEIFEXISTSbstwbjq; CREATETABLEbstwbjq( CHARACTORvarchar(200)NOTNULL, WORDvarchar(100)NOTNULL, CODEvarchar(100)DEFAULTNULL, STROKEvarchar(200)DEFAULTNULL )ENGINEInnoDBDEFAULTCHARSETutf8; Tablestructurefortblpinyin DROPTABLEIFEXISTStblpinyin; CREATETABLEtblpinyin( SNbigint(20)NOTNULL, WORDvarchar(200)NOTNULL, PYvarchar(200)NOTNULL, PYLEVELint(11)DEFAULTNULL )ENGINEInnoDBDEFAULTCHARSETutf8;2。插入基础数据记录 Recordsofbstwbjq truncatetablebstwbjq; INSERTINTObstwbjqVALUES(禅,C,P,K); INSERTINTObstwbjqVALUES(讶,Y,Y,2T); INSERTINTObstwbjqVALUES(焉,Y,G,Pa); INSERTINTObstwbjqVALUES(阉,Y,U,V2); INSERTINTObstwbjqVALUES(烟,Y,O,Ng); INSERTINTObstwbjqVALUES(淹,Y,I,V); INSERTINTObstwbjqVALUES(圊,Q,L,Rz); INSERTINTObstwbjqVALUES(圉,Y,L,S?); INSERTINTObstwbjqVALUES(帔,P,M,;); 。。。。。。。。 commit; Recordsoftblpinyin truncatetabletblpinyin; INSERTINTOtblpinyinVALUES(33641,胫,jing4,0); INSERTINTOtblpinyinVALUES(30749,箅,bi4,0); INSERTINTOtblpinyinVALUES(30750,箢,yuan1,0); INSERTINTOtblpinyinVALUES(30751,篁,huang2,0); INSERTINTOtblpinyinVALUES(30752,篦,bi4,0); INSERTINTOtblpinyinVALUES(30753,篾,mie4,0); INSERTINTOtblpinyinVALUES(30754,簋,gui3,0); INSERTINTOtblpinyinVALUES(30755,簪,zan1,0); INSERTINTOtblpinyinVALUES(30756,籀,zhou4,0); INSERTINTOtblpinyinVALUES(30757,舄,xi4,0); INSERTINTOtblpinyinVALUES(30758,舢,shan1,0); INSERTINTOtblpinyinVALUES(30759,舨,ban3,0); 。。。。。。。。 commit;3。创建汉字转拼音函数(存储过程、函数)3。1。创建存储过程:PRCGETPYM procedurestructureforPRCGETPYM delimiter dropprocedureifexistsPRCGETPYM; createprocedurePRCGETPYM(INVNAMEvarchar(256),OUTVPYMvarchar(256)) begin declareiintdefault1; declarejintdefault0; declareVPINYINTEMPVARCHAR(70); declareVNAMETEMPvarchar(200); declareVNAMESINvarchar(10); declareVPINYINSINvarchar(10); declarevcounter1int(8); 替换各种特殊符号 selectreplace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(VNAME, ,A),,B),,C),,D),,E),,F),,G),,H), ,I),,J),,K),,L),,M),,N),,O),,P), ,Q),,R),,S),,T),,U),,V),,W),,X), ,Y),,Z),,),,),,),,), 〔,),〕,),{,),},),(,),),), ,),,),《,),》,),(,),),),,), ,),,),。,),。,),,),,),,), ,),,),,),,一),,二),,三), ,四),,五),,六),,七),,八),,九),,零)R3 intoVNAMETEMP fromdual; 循环获得字符串拼音码 myloop:loop ifVNAMEisnullthen leavemyloop; endif; selectsubstr(VNAMETEMP,i,1)intoVNAMESINfromdual; setii1; ifVNAMESINthen selectcount() intovcounter1 frombstwbjq wherebstwbjq。charactorvnamesin; ifvcounter10then selectWORD intoVPINYINSIN frombstwbjq wherebstwbjq。CHARACTORVNAMESIN limit1; selectconcatws(,VPINYINTEMP,VPINYINSIN) intoVPINYINTEMP fromdual; endif; endif; selectcharlength(VNAME)intojfromdual; ifijthen leavemyloop; endif; endloop; 截取32位长度字符 ifcharlength(VPINYINTEMP)32then selectsubstr(VPINYINTEMP,1,32)intoVPYMfromdual; else selectVPINYINTEMPintoVPYMfromdual; endif; end; delimiter;3。2。创建存储过程:SPPINYIN procedurestructureforSPPINYIN delimiter dropprocedureifexistsSPPINYIN; createprocedureSPPINYIN(INhanzivarchar(256),OUTpinyinvarchar(256)) begin declareawordvarchar(200); declarearesultvarchar(200); declaretemp1varchar(20); declarelenintdefault0; declarepointintdefault1; declarecharwordvarchar(20); declarecharlenintdefault1; 定义游标标志变量 declaredoneintdefaultfalse; 定义游标 declarecurpinyincursorfor selectPYfromTBLPINYIN wherewordsubstr(aword,point,charlen); 指定游标循环结束时的返回值 declarecontinueHANDLERfornotfoundsetdonetrue; selectltrim(rtrim(hanzi))intoawordfromdual; selectcharlength(aword)intolenfromdual; LABEL1 whilepointlendo selectintotemp1fromdual; selectsubstr(aword,point,1)intocharwordfromdual; if(charwordisnotnullandcharword!)then selectconcatws(,aresult,charword)intoaresultfromdual; else select2intocharlenfromdual; endif; 打开游标 opencurpinyin; 开始循环处理游标里的数据 readloop:loop 获得游标当前指向的一条数据 fetchcurpinyinintotemp1; 判断游标的循环是否结束 ifdonethen leavereadloop; endif; endloop;结束游标循环 关闭游标 closecurpinyin; if(point1)then setaresulttemp1; else selectconcatws(,aresult,temp1)intoaresultfromdual; endif; selectpointcharlenintopointfromdual; endwhile; 输出结果 selectaresultintopinyinfromdual; end; delimiter;3。3。创建函数:topinyin functionstructurefortopinyin delimiter dropfunctionifexiststopinyin; createfunctiontopinyin(vhanzivarchar(256),vtypeint) returnsvarchar(256) begin declarestrTempVARCHAR(200); declarestrResultVARCHAR(200); declarestrHanziVARCHAR(200); declarestrTemp1VARCHAR(200); declarevsubbVARCHAR(100); declareVNAMETEMPVARCHAR(200); declarevpinyinVARCHAR(200); 替换各种特殊符号 selectreplace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace(replace(vhanzi, ,A),,B),,C),,D),,E),,F),,G),,H), ,I),,J),,K),,L),,M),,N),,O),,P), ,Q),,R),,S),,T),,U),,V),,W),,X), ,Y),,Z),,),,),,),,), 〔,),〕,),{,),},),(,),),), ,),,),《,),》,),(,),),),,), ,),,),。,),。,),,),,),,), ,),,),,),,一),,二),,三), ,四),,五),,六),,七),,八),,九),,零)R3 intoVNAMETEMP fromdual; ifvtype1then简拼 setVNAMETEMPVNAMETEMP; setstrResultnull; callPrcGetPym(VNAMETEMP,strResult); elseifvtype2then尾全拼 判断结尾字符是否是中文 selectltrim(rtrim(substr(VNAMETEMP,charlength(VNAMETEMP),charlength(VNAMETEMP)))) intovsubb fromdual; ifvsubbisnullthen如果不是中文则直接生成开口码 setVNAMETEMPVNAMETEMP; setstrResultnull; callPrcGetPym(VNAMETEMP,strResult); else selectsubstr(VNAMETEMP,1,charlength(VNAMETEMP)1)intostrHanzifromdual; setstrHanzistrHanzi; setstrTemp1null; callPrcGetPym(strHanzi,strTemp1); selectsubstr(VNAMETEMP,charlength(VNAMETEMP),charlength(VNAMETEMP))intostrHanzifromdual; setstrHanzistrHanzi; setstrTempnull; callSpPinyin(strHanzi,strTemp); selectsubstr(strTemp,1,charlength(strTemp)1)intostrResultfromdual; selectconcatws(,strTemp1,strResult)intostrResultfromdual; endif; elseifvtype3then首全拼 判断开头字符是否是中文 selectltrim(rtrim(substr(VNAMETEMP,1,1)))intovsubbfromdual; ifvsubbisnullthen如果不是中文则直接生成开口码 setVNAMETEMPVNAMETEMP; setstrResultnull; callPrcGetPym(VNAMETEMP,strResult); else selectsubstr(VNAMETEMP,2,charlength(VNAMETEMP))intostrHanzifromdual; setstrHanzistrHanzi; setstrResultnull; callPrcGetPym(strHanzi,strResult); selectsubstr(VNAMETEMP,1,1)intostrHanzifromdual; setstrHanzistrHanzi; setstrTempnull; callSpPinyin(strHanzi,strTemp); selectconcatws(,substr(strTemp,1,charlength(strTemp)1),strResult)intostrResultfromdual; endif; endif; setvpinyinUPPER(strResult); returnvpinyin; end; delimiter;4。使用方法案例