2008年4月份的時候 ,我在asp當中遇到一個問題,就是漢字轉為拼音的問題,裡面需要處理多音字。
在sql2000資料庫當中存著漢字的對應的拼音是這樣的
Id Hanzi Pinyin
1 啊 a e
2 你 ni
3 重 chong zhong
4 … …
實現的效果:
我想要的效果是如果輸入的是多音字比如“你重啊”,可以列出
(1)ni chong a
(2)ni zhong a
(3)ni chong e
(4)ni zhong e
原來我自己用二維數組,弄了半天沒有出來。後來csdn上的 gingerkang 指點,就解決了。
下面是解決的方法:
主要是在sql裡面建立一個Function:
create table table_py (id int identity(1,1),hanzi nvarchar(1),pinyin nvarchar(30)) go insert into table_py (hanzi,pinyin) select '啊','a e' union all select '你','ni' union all select '重','chong zhong' go create function s2p(@s nvarchar(1)) returns @t table(pinyin nvarchar(10)) as begin if not exists(select 1 from table_py where hanzi=@s) begin insert into @t select @s return end declare @tstr nvarchar(30) select @tstr=pinyin from table_py where hanzi=@s while charindex(' ',@tstr)>0 begin insert into @t select left(@tstr,charindex(' ',@tstr)) select @tstr=stuff(@tstr,1,charindex(' ',@tstr),'') end insert into @t select @tstr return end go select * from s2p('你') t0,s2p('重') t1,s2p('啊') t2 go drop function s2p drop table table_py go
以上代碼在sql2000和sql2005中測試通過