Server in SQL 2000, the user can create a custom function, the function return value can be a value, can also be a table.
Maybe it's not clear how the custom function works. Previously mentioned in the optimizing database posts, try not to use cursors, because this will bring greater
System overhead. But sometimes you have to use a cursor, for example, I want to get the pinyin of a field where the content is a piece of Chinese characters. But if you want to transform the Chinese character
For pinyin, you have to check the table to complete, then you have to start a cursor, and then the field of each word to check the table.
But now we can use custom functions to do the same thing, greatly saving the overhead of the system. example:/* using a custom function */
CREATE FUNCTION Translate (@Original_Text varchar) RETURNS varchar
BEGIN DECLARE @intLength as tinyint DECLARE @strTemp as varchar (10)
DECLARE @strResult as varchar 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 Endgo
UPDATE phonecode SET namesame=translate (name), Addsame=translate (address)
/* Using Cursors */create proc trans asdeclare @i Integer, @char varchar (2),
@tempchr varchar (2), @strtemp varchar (m), @strtemp1 varchar (100),
@strname (varchar), @straddr varchar (100)
Declare Cur_trans Cursor Local DYNAMIC for SELECT [Name],[address]
From Phonecodeselect @char = "" Open cur_transfetch Cur_trans
Into @strname, @straddrSet nocount onwhile @ @Fetch_Status =0begin 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 CURRENT of Cur_trans
Fetch next from Cur_trans into @strname, @straddrendclose Cur_trans
deallocate cur_transset nocount Offreturn 0go
In contrast, the efficiency of the implementation is improved, and the code is much more readable. In fact, other databases have provided such a feature (Do not remember what the database is), do not
Ms's following speed is getting faster and higher, and this is why its market share is getting bigger. Next announcement: Indexed view try to stick it out every day, write something really is very tired