SQL Server自定义函数

1、标量函数

 

Create function AvgResult(@scode varchar(10))

 

Returns real

 

As

 

Begin

 

   Declare @avg real

 

   Declare @code varchar(11)

 

   Set @code=@scode + ‘%’

 

   Select @avg=avg(result) from LearnResult_baijiali

 

Where scode like @code

 

Return @avg

 

End

 

执行用户自定义函数

 

select 用户名。函数名 as 字段别名

 

 

2、表格值函数

 

a、 内联表格值函数

 

格式:

 

create function 函数名(参数)

 

returns table

 

[with {Encryption | Schemabinding }]

 

as

 

return(一条SQL语句)

 

 

 

create function tabcmess(@code varchar(10))

 

returns table

 

as

 

return(select ccode,scode from cmessage where ccode like @ccode)

 

 

 

b、 多句表格值函数

 

   create function 函数名(参数)

 

   returns 表格变量名table (表格变量定义)

 

   [with {Encryption | Schemabinding }]

 

as

 

   begin

 

    SQL语句

 

   end

 

 

 

 

 

 

posted @ 2013-09-27 14:07  杨胖胖  阅读(150)  评论(0编辑  收藏  举报