SQL自定義函數(shù)實例
作者:佚名
學(xué)習(xí)SQL數(shù)據(jù)庫,不能提到SQL自定義函數(shù),它在SQL函數(shù)中的地位非常重要,下面就為您示例一個SQL自定義函數(shù),供您參考。
SQL自定義函數(shù)在SQL語言中應(yīng)用非常廣泛,下面就為您介紹一個SQL函數(shù)定義的例子,供您參考,希望對您學(xué)習(xí)SQL自定義函數(shù)有所幫助。
- --將檔案IDS的字符串轉(zhuǎn)換成名字的字符串
- ALTER FUNCTION GetArchivesNameStr(@SourceSql nvarchar(500),@StrSeprate nvarchar(10)) --字符串格式41,56,77,
- RETURNS nvarchar(500)
- AS
- begin
- declare @names nvarchar(500)
- declare @i int
- set @names=''
- set @SourceSql=rtrim(ltrim(@SourceSql))
- set @i=charindex(@StrSeprate,@SourceSql)
- while @i>=1
- begin
- set @names=@names+(select name from Archives where ArchivesID=convert(int,left(@SourceSql,@i-1)))+','
- set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i) --已經(jīng)轉(zhuǎn)換好的去掉
- set @i=charindex(@StrSeprate,@SourceSql) --重新定位
- end
- --return @names
- if @SourceSql<>''and @i>=1 --最后一個
- begin
- set @names=@names+(select name from Archives where ArchivesID=convert(int,left(@SourceSql,@i-1)))
- end
- if(right(@names,1)=',') --去掉后面的逗號
- set @names=substring(@names,1,len(@names)-1)
- return @names
- end
【編輯推薦】
責(zé)任編輯:段燃
來源:
互聯(lián)網(wǎng)