精华区 [关闭][返回]

当前位置:月光软件>>讨论区精华>>〖软件开发〗>>● ASP>>★ASP的关联★>>Wtat's new in MS SQL Server 2000>>What's new in Microsoft SQL Server 200(1)

主题:What's new in Microsoft SQL Server 200(1)
发信人: dongbao()
整理人: dongbao(2001-05-27 09:24:04), 站内信件
在SQL 2000里面,用户可以建立自定义的函数,函数返回值可以是一个值,也可
以是一个表。
可能大家还不是太清楚,自定义函数有什么作用。以前在优化数据库的贴子中提
到过,尽量不要是用游标,因为这样会带来更大的系统开销。但是有的时候你必
须使用游标,举一个例子,比如我希望得到一个内容是一段汉字的字段的拼音。
但是要想把汉字转化为拼音,必须通过查表来完成,那么你就必须先开出一个游
标,然后再对字段中的每一个字进行查表。
但是现在我们可以使用自定义函数来完成同样的操作,就大大的节省了系统开销

Example:
/*使用自定义函数*/
CREATE FUNCTION Translate ( @Original_Text varchar(100))
RETURNS varchar(500)
AS
BEGIN
    DECLARE @intLength as tinyint
    DECLARE @strTemp as varchar(10)
    DECLARE @strResult as varchar(500)
    DECLARE @strChar as varchar(2)
    DECLARE @i as tinyint
    SET @intLength = len(@Original_Text)
    SET strResult = ''
    WHILE @i <= @intLength
BEGIN
SELECT @strChar = substring(@Original_Text, @i, 1)
SET @strTemp = null
SELECT @strTemp = spell FROM wordspell WHERE word = @strChar
SELECT @strResult = @strResult + isnull(@strTemp, @strChar)
SELECT @i=@i+1
END
RETURN strResult
END
GO
UPDATE phonecode SET namesame=Translate(name), addsame=Translate(addre
ss)

/*使用游标*/
create proc trans as
Declare @i integer,
@char varchar(2),
@tempchr varchar(2),
@strtemp varchar(100),
@strtemp1 varchar(100),
@strname varchar(100),
@straddr varchar(100)
Declare Cur_trans Cursor local DYNAMIC
For select [name],[address]
from phonecode
select @char=""
Open Cur_trans
Fetch Cur_trans
Into @strname,@straddr
Set nocount on
while @@Fetch_Status=0
begin
select @i=1
select @strtemp=""
select @strname = ltrim(rtrim(@strname))
while @i<=len(@strname)
begin
select @char = substring(@strname,@i,1)
select @tempchr = null
select @tempchr=spell from TYPER.wordspell where word=@char
select @strtemp=@strtemp + isnull(@tempchr,@char)
select @i=@i+1
end
select @i=1
select @strtemp1=""
select @char=''
select @straddr = ltrim(rtrim(@straddr))
while @i<=len(@straddr)
begin
select @char = substring(@straddr,@i,1)
select @tempchr = null
select @tempchr=spell from TYPER.wordspell where word=@char
select @strtemp1=@strtemp1 + isnull(@tempchr,@char)
select @i=@i+1
end
Update phonecode set namesame=@strtemp,addsame=@strtemp1 where CUR
RENT OF Cur_trans
Fetch next from Cur_trans Into @strname,@straddr
end
close Cur_trans
Deallocate Cur_trans
set nocount off
return 0
go
相比之下,不但执行效率提高了,代码的可读性也好多了。其实其他数据库已经
提供了这样的功能(不记得是什么数据库了),不过MS的跟随速度是越来越快了
,提高也是越来越多了,这也是它的市场份额越来越大的原因。

下期预告:
带索引的视图

争取做到每日一贴吧,写东西真的是很累的


--
ICQ:43395237 OICQ:126132
我自豪我用正版,我骄傲我用盗版!!!

※ 来源:.月光程序代码网 http://www.moon-soft.com.[FROM: 202.108.0.15]

[关闭][返回]






转载请注明:转载自 月光程序代码网 [ http://www.moon-soft.com ]