在很多程序中,我们都要用到汉字的首拼音,如用他来方便查找数据等,在网上我们可以看到很多这样的算法。下面这段代码是SQLServer2000的存储过程,调用该存储过程,自动更新数据库中的拼音首字母列。用到了procedure,cursor技术。
1
USE Database Name
2
GO
3
4
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_GetChineseSpell')
5
BEGIN
6
PRINT 'Dropping Procedure sp_GetChineseSpell'
7
DROP Procedure sp_GetChineseSpell
8
END
9
10
GO
11
12
PRINT 'Creating Procedure sp_GetChineseSpell'
13
GO
14
CREATE Procedure sp_GetChineseSpell
15
16
AS
17
/******************************************************************************
18
** File: sp_GetChineseSpell.sql
19
** Name: sp_GetChineseSpell
20
** Desc: 返回某列汉字的拼音码,在Sql server 2000安装为中文版时调试通过
21
**
22
**
23
** Return values: null
24
**
25
** Called by:
26
**
27
** Parameters:
28
** Input: null Output:null
29
** ---------- -----------
30
**
31
** Auth: Xinhua Yuan
32
** Date:
33
*******************************************************************************
34
** Change History
35
*******************************************************************************
36
** Date: Author: Description:
37
** -------- -------- -------------------------------------------
38
**
39
*******************************************************************************/
40
BEGIN
41
42
--返回某列汉字的拼音码
43
44
DECLARE @VChChinese VARCHAR(60) --汉字列数据量
45
DECLARE @VChSp VARCHAR(30) --拼音码数据列
46
DECLARE @VChSpell VARCHAR(30)
47
DECLARE @VChControl CHAR(2) --存储临时取出的单个汉字
48
DECLARE @IntLen INT
49
DECLARE @IntControl INT
50
51
SELECT @VChSp=''
52
53
DECLARE @IntCount INT
54
DECLARE @IntTotalCount INT
55
SELECT @IntCount=0
56
SELECT @IntTotalCount=(SELECT COUNT(*) FROM table name)
57
58
DECLARE CursorSpell CURSOR FOR --Declare cursor
59
SELECT Chinese field,Spell field
60
FROM table name
61
62
OPEN CursorSpell --Open cursor
63
64
BEGIN TRAN
65
WHILE @@Fetch_Status=0
66
67
BEGIN
68
--About operation
69
SELECT @IntCount = @IntCount + 1
70
SELECT @VChChinese=''
71
SELECT @VChSpell=''
72
SELECT @VCHSp=''
73
74
FETCH NEXT FROM CursorSpell INTO @VChChinese,@VChSpell
75
SELECT @IntControl=1
76
77
PRINT @VChChinese + @VChSpell
78
79
SELECT @IntLen=Len(@VChChinese)
80
81
WHILE @IntControl<= @IntLen
82
83
BEGIN
84
SELECT @VChControl=SUBSTRING(@VCHCHinese,@IntControl,1)
85
86
87
IF @VChControl>'啊' AND @VChControl<'芭'
88
SELECT @VChSpell='A'
89
ELSE IF @VChControl>='芭' AND @VChControl<'擦'
90
SELECT @VChSpell='B'
91
ELSE IF @VChControl>='擦' AND @VChControl<'搭'
92
SELECT @VChSpell='C'
93
ELSE IF @VChControl>='搭' AND @VChControl<'娥'
94
SELECT @VChSpell='D'
95
ELSE IF @VChControl>='娥' AND @VChControl<'发'
96
SELECT @VChSpell='E'
97
ELSE IF @VChControl>='发' AND @VChControl<='嘎'
98
SELECT @VChSpell='F'
99
ELSE IF @VChControl>'嘎' AND @VChControl<'哈'
100
SELECT @VChSpell='G'
101
ELSE IF @VChControl>='哈' AND @VChControl<'击'
102
SELECT @VChSpell='H'
103
ELSE IF @VChControl>='击' AND @VChControl<'喀'
104
SELECT @VChSpell='J'
105
ELSE IF @VChControl>='喀' AND @VChControl<'垃'
106
SELECT @VChSpell='K'
107
ELSE IF @VChControl>='垃' AND @VChControl<'妈'
108
SELECT @VChSpell='L'
109
ELSE IF @VChControl>='妈' AND @VChControl<'拿'
110
SELECT @VChSpell='M'
111
ELSE IF @VChControl>='拿' AND @VChControl<'哦'
112
SELECT @VChSpell='N'
113
ELSE IF @VChControl>='哦' AND @VChControl<'啪'
114
SELECT @VChSpell='O'
115
ELSE IF @VChControl>='啪' AND @VChControl<'期'
116
SELECT @VChSpell='P'
117
ELSE IF @VChControl>='期' AND @VChControl<'然'
118
SELECT @VChSpell='Q'
119
ELSE IF @VChControl>='然' AND @VChControl<'撒'
120
SELECT @VChSpell='R'
121
ELSE IF @VChControl>='撒' AND @VChControl<'塌'
122
SELECT @VChSpell='S'
123
ELSE IF @VChControl>='塌' AND @VChControl<'挖'
124
SELECT @VChSpell='T'
125
ELSE IF @VChControl>='挖' AND @VChControl<'昔'
126
SELECT @VChSpell='W'
127
ELSE IF @VChControl>='昔' AND @VChControl<'压'
128
SELECT @VChSpell='X'
129
ELSE IF @VChControl>='压' AND @VChControl<'匝'
130
SELECT @VChSpell='Y'
131
ELSE IF @VChControl>='匝' AND @VChControl<='座'
132
SELECT @VChSpell='Z'
133
ELSE
134
SELECT @VChSpell=@VChControl
135
136
SELECT @VChSp=@VChSp + RTRIM(UPPER(@VChSpell))
137
SELECT @IntControl=@IntControl + 1
138
139
END
140
--PRINT @VChSp
141
UPDATE table name SET Spell field = @VChSp
142
143
WHERE CURRENT OF CursorSpell
144
145
END
146
147
COMMIT TRAN
148
149
CLOSE CursorSpell --Close cursor
150
DEALLOCATE CursorSpell --Deallocate cursor
151
152
END
153
154
GO
155
156
GRANT EXEC ON sp_GetChineseSpell TO PUBLIC
157
158
GO
159
本存储过程在SqlServer2000中测试通过.
USE Database Name2
GO3

4
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_GetChineseSpell')5
BEGIN6
PRINT 'Dropping Procedure sp_GetChineseSpell'7
DROP Procedure sp_GetChineseSpell8
END9

10
GO11

12
PRINT 'Creating Procedure sp_GetChineseSpell'13
GO14
CREATE Procedure sp_GetChineseSpell15

16
AS17
/******************************************************************************18
** File: sp_GetChineseSpell.sql19
** Name: sp_GetChineseSpell20
** Desc: 返回某列汉字的拼音码,在Sql server 2000安装为中文版时调试通过21
**22
** 23
** Return values: null24
** 25
** Called by: 26
** 27
** Parameters:28
** Input: null Output:null29
** ---------- -----------30
**31
** Auth: Xinhua Yuan32
** Date: 33
*******************************************************************************34
** Change History35
*******************************************************************************36
** Date: Author: Description:37
** -------- -------- -------------------------------------------38
** 39
*******************************************************************************/40
BEGIN41

42
--返回某列汉字的拼音码43

44
DECLARE @VChChinese VARCHAR(60) --汉字列数据量45
DECLARE @VChSp VARCHAR(30) --拼音码数据列46
DECLARE @VChSpell VARCHAR(30) 47
DECLARE @VChControl CHAR(2) --存储临时取出的单个汉字48
DECLARE @IntLen INT49
DECLARE @IntControl INT50

51
SELECT @VChSp=''52

53
DECLARE @IntCount INT54
DECLARE @IntTotalCount INT55
SELECT @IntCount=056
SELECT @IntTotalCount=(SELECT COUNT(*) FROM table name)57

58
DECLARE CursorSpell CURSOR FOR --Declare cursor59
SELECT Chinese field,Spell field60
FROM table name61

62
OPEN CursorSpell --Open cursor63

64
BEGIN TRAN65
WHILE @@Fetch_Status=066

67
BEGIN68
--About operation69
SELECT @IntCount = @IntCount + 170
SELECT @VChChinese=''71
SELECT @VChSpell=''72
SELECT @VCHSp=''73

74
FETCH NEXT FROM CursorSpell INTO @VChChinese,@VChSpell75
SELECT @IntControl=176
77
PRINT @VChChinese + @VChSpell78
79
SELECT @IntLen=Len(@VChChinese)80
81
WHILE @IntControl<= @IntLen82
83
BEGIN84
SELECT @VChControl=SUBSTRING(@VCHCHinese,@IntControl,1)85

86
87
IF @VChControl>'啊' AND @VChControl<'芭' 88
SELECT @VChSpell='A'89
ELSE IF @VChControl>='芭' AND @VChControl<'擦' 90
SELECT @VChSpell='B'91
ELSE IF @VChControl>='擦' AND @VChControl<'搭' 92
SELECT @VChSpell='C'93
ELSE IF @VChControl>='搭' AND @VChControl<'娥' 94
SELECT @VChSpell='D'95
ELSE IF @VChControl>='娥' AND @VChControl<'发' 96
SELECT @VChSpell='E'97
ELSE IF @VChControl>='发' AND @VChControl<='嘎'98
SELECT @VChSpell='F'99
ELSE IF @VChControl>'嘎' AND @VChControl<'哈' 100
SELECT @VChSpell='G'101
ELSE IF @VChControl>='哈' AND @VChControl<'击' 102
SELECT @VChSpell='H'103
ELSE IF @VChControl>='击' AND @VChControl<'喀'104
SELECT @VChSpell='J'105
ELSE IF @VChControl>='喀' AND @VChControl<'垃' 106
SELECT @VChSpell='K'107
ELSE IF @VChControl>='垃' AND @VChControl<'妈' 108
SELECT @VChSpell='L'109
ELSE IF @VChControl>='妈' AND @VChControl<'拿' 110
SELECT @VChSpell='M'111
ELSE IF @VChControl>='拿' AND @VChControl<'哦' 112
SELECT @VChSpell='N'113
ELSE IF @VChControl>='哦' AND @VChControl<'啪' 114
SELECT @VChSpell='O'115
ELSE IF @VChControl>='啪' AND @VChControl<'期'116
SELECT @VChSpell='P'117
ELSE IF @VChControl>='期' AND @VChControl<'然' 118
SELECT @VChSpell='Q'119
ELSE IF @VChControl>='然' AND @VChControl<'撒' 120
SELECT @VChSpell='R'121
ELSE IF @VChControl>='撒' AND @VChControl<'塌' 122
SELECT @VChSpell='S'123
ELSE IF @VChControl>='塌' AND @VChControl<'挖'124
SELECT @VChSpell='T'125
ELSE IF @VChControl>='挖' AND @VChControl<'昔' 126
SELECT @VChSpell='W'127
ELSE IF @VChControl>='昔' AND @VChControl<'压'128
SELECT @VChSpell='X'129
ELSE IF @VChControl>='压' AND @VChControl<'匝' 130
SELECT @VChSpell='Y'131
ELSE IF @VChControl>='匝' AND @VChControl<='座' 132
SELECT @VChSpell='Z'133
ELSE134
SELECT @VChSpell=@VChControl135
136
SELECT @VChSp=@VChSp + RTRIM(UPPER(@VChSpell))137
SELECT @IntControl=@IntControl + 1138

139
END140
--PRINT @VChSp 141
UPDATE table name SET Spell field = @VChSp142
143
WHERE CURRENT OF CursorSpell144

145
END146

147
COMMIT TRAN 148

149
CLOSE CursorSpell --Close cursor 150
DEALLOCATE CursorSpell --Deallocate cursor151

152
END153

154
GO155

156
GRANT EXEC ON sp_GetChineseSpell TO PUBLIC157

158
GO159


浙公网安备 33010602011771号