WhatsnewinMicrosoft2000(二)

来源:互联网 发布:油画在线网络课程 编辑:程序博客网 时间:2024/05/21 12:42
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
在SQL2000里面,用户可以建立自定义的函数,函数返回值可以是一个值,也可以是一个表。
可能大家还不是太清楚,自定义函数有什么作用。以前在优化的贴子中提到过,尽量不要是用游标,因为这样会带来更大的
系统开销。但是有的时候你必须使用游标,举一个例子,比如我希望得到一个内容是一段汉字的字段的拼音。但是要想把汉字转化
为拼音,必须通过查表来完成,那么你就必须先开出一个游标,然后再对字段中的每一个字进行查表。
但是现在我们可以使用自定义函数来完成同样的操作,就大大的节省了系统开销。Example:/*使用自定义函数*/
CREATEFUNCTIONTranslate(@Original_Textvarchar(100))RETURNSvarchar(500)AS
BEGIN&nbsp;&nbsp;&nbsp;&nbsp;DECLARE@intLengthastinyint&nbsp;&nbsp;&nbsp;&nbsp;DECLARE@strTempasvarchar(10)
&nbsp;&nbsp;&nbsp;&nbsp;DECLARE@strResultasvarchar(500)&nbsp;&nbsp;&nbsp;&nbsp;DECLARE@strCharasvarchar(2)
&nbsp;&nbsp;&nbsp;&nbsp;DECLARE@iastinyint&nbsp;&nbsp;&nbsp;&nbsp;SET@intLength=len(@Original_Text)
&nbsp;&nbsp;&nbsp;&nbsp;SETstrResult=''&nbsp;&nbsp;&nbsp;&nbsp;WHILE@i&lt;=@intLength&nbsp;&nbsp;&nbsp;&nbsp;BEGIN
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT@strChar=substring(@Original_Text,@i,1)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SET@strTemp=null
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT@strTemp=spellFROMwordspellWHEREword=@strChar
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT@strResult=@strResult+isnull(@strTemp,@strChar)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT@i=@i+1&nbsp;&nbsp;&nbsp;&nbsp;END&nbsp;&nbsp;&nbsp;&nbsp;RETURNstrResultENDGO
UPDATEphonecodeSETnamesame=Translate(name),addsame=Translate(address)
/*使用游标*/createproctransasDeclare@iinteger,&nbsp;&nbsp;&nbsp;&nbsp;@charvarchar(2),
&nbsp;&nbsp;&nbsp;&nbsp;@tempchrvarchar(2),&nbsp;&nbsp;&nbsp;&nbsp;@strtempvarchar(100),&nbsp;&nbsp;&nbsp;&nbsp;@strtemp1varchar(100),
&nbsp;&nbsp;&nbsp;&nbsp;@strnamevarchar(100),&nbsp;&nbsp;&nbsp;&nbsp;@straddrvarchar(100)
DeclareCur_transCursorlocalDYNAMICFor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select[name],[address]
&nbsp;&nbsp;&nbsp;&nbsp;fromphonecodeselect@char=&quot;&quot;OpenCur_transFetchCur_trans
Into@strname,@straddrSetnocountonwhile@@Fetch_Status=0begin&nbsp;&nbsp;&nbsp;&nbsp;select@i=1
&nbsp;&nbsp;&nbsp;&nbsp;select@strtemp=&quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;select@strname=ltrim(rtrim(@strname))
&nbsp;&nbsp;&nbsp;&nbsp;while@i&lt;=len(@strname)&nbsp;&nbsp;&nbsp;&nbsp;begin
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select@char=substring(@strname,@i,1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select@tempchr=null1<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击