sql server 2008学习11 UDF用户自定义函数
用戶自定義函數(shù)? 和存儲過程是類似的,
是一組 有序的t-sql語句,udf被 預(yù)先優(yōu)化和編譯,并且可以作為一個單元來進行調(diào)用.
使用存儲過程 時 可傳入?yún)?shù),傳出參數(shù).可以返回值,不過該值用于指示成功或者失敗,而非返回數(shù)據(jù).也可以返回結(jié)果集,
但是在沒有將結(jié)果集插入到某種表(通常是臨時表)中以供后面使用的情況下,不能在 查詢中真正使用它們. 即使使用
表值 輸出參數(shù),在查詢中使用結(jié)果之前,也要額外的一個步驟.
那么 UDF可以傳入?yún)?shù),但是不可傳出參數(shù). 但是可以返回值,和系統(tǒng)函數(shù)一樣,可以返回標(biāo)量值,這個值的好處是? 不像存儲過程那樣只限于
整型數(shù)據(jù)類型,而是可以返回大多數(shù)sqlserver的數(shù)據(jù)類型.
下面具體介紹兩種 UDF
返回標(biāo)量的UDF
select name,(select avg(sec) from stu) as average,sec-(select avg(sec) from stu) as [Difference] from stu where SID=2 .csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }這是簡單的sql語句,下面 有兩個UDF代替其中的兩個列
create function averages()returns intasbeginreturn (select avg(sec) from stu);endgocreate function sdifference(@arg int)returns intasbeginreturn @arg - dbo.averages();endgoselect name,dbo.averages() as average,dbo.sdifference(sec) as [Difference] from stu where SID=2 .csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }那么會在數(shù)據(jù)中看到:
執(zhí)行結(jié)果和之前的sql語句是一樣的.
返回表的UDF
既然UDF可以傳入?yún)?shù),又可以返回表,那是不是 就相當(dāng)于參數(shù)化的視圖……是不是聽起來很讓人興奮呢.
下面創(chuàng)建一個簡單函數(shù):
create function getPeopleByname(@name nvarchar(30)) returns table as return (select * from stu where name = @name) .csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }下面調(diào)用這個函數(shù):
select * from getPeopleByname('gao') .csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }結(jié)果如下:
下面 UDF結(jié)合 遞歸, 查詢所有樹的 節(jié)點
表:
數(shù)據(jù):
下面定義函數(shù):
create function sss(@id as int)returns @t table(id int not null,name int not null,pid int null)asbegindeclare @lay as int;insert into @t select * from tree where pid =@id;select @lay = min(id) from tree where pid =@id; --第一次 @lay=5while @lay is not nullbegininsert into @t select * from sss(@lay);select @lay=min(id) from treewhere id>@lay and pid=@idendreturn;endgo .csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }執(zhí)行函數(shù),參數(shù) 是 任意父節(jié)點 id
select * from sss(6) .csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }效果:
執(zhí)行:?
select * from sss(4) .csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }效果:
將查出 所有的子節(jié)點來.
.net調(diào)用函數(shù):http://www.cnblogs.com/Mr-Joe/archive/2012/05/10/2494093.html
總結(jié)
以上是生活随笔為你收集整理的sql server 2008学习11 UDF用户自定义函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 郑州房价为什么这么高?
- 下一篇: sql server 2008学习12