您的位置:首页 > 数据库

SQL SERVER Z自定义函数

2016-07-03 09:23 369 查看
SQLSERVER 自定义函数一般有三种形式的应用,首先是标量值函数,顾名思义就是执行完这个函数返回的值是一个变量,
调用形式如下:
  
Create function 函数名(参数)

Returns 返回值数据类型

[with {Encryption | Schemabinding }]

[as]

begin

SQL语句(必须有return 变量或值)

End

示例如下:

CREATE FUNCTION GetSum

(

    @firstNum int,

    @secondNum int

)

RETURNS int

AS

BEGIN

    -- Declare the return variable here

    DECLARE @result int

    -- Add the T-SQL statements to compute the return value here

    SELECT @result=@firstNum+@secondNum

    -- Return the result of the function

    RETURN @result

END

GO

PS: 如果函数里面没有参数可以不用写任何参数,但是必须要有括号

然后是 内联表值函数

所谓内联表值函数就是返回的数据是以表的形式返回的

基本格式如下:

GO

SET QUOTED_IDENTIFIER ON

GO

-- =============================================

-- Author:  <Author,,Name>

-- Create date: <Create Date,,>

-- Description: <Description,,>

-- =============================================

CREATE FUNCTION <Inline_Function_Name, sysname, FunctionName>



 -- Add the parameters for the function here

 <@param1, sysname, @p1> <Data_Type_For_Param1, , int>,

 <@param2, sysname, @p2> <Data_Type_For_Param2, , char>

)

RETURNS TABLE

AS

RETURN

(

 -- Add the SELECT statement with parameter references here

 SELECT 0

)

GO

示例如下:

--use GreDevelopAssessSystem

--go

--create function calculate_pollution_direct_value(@start_time datetime,@stop_time datetime)

--returns table

--as

--return(

--select TB_DataBase_FirePlant_Online.RecTime,

--  SUM( TB_DataBase_FirePlant_Online.OUT_C_SO2*Flow)*6*300+SUM(TB_DataBase_FirePlant_Online.OUT_C_Dust*Flow)*2.2*300+SUM( TB_DataBase_FirePlant_Online.OUT_C_NOX*Flow)*8*300 as amount

--  from TB_DataBase_FirePlant_Online

--  where RecTime  between DATEADD(MINUTE,5,@start_time) and @stop_time

--  group by RecTime)

--go

PS:在使用内联表值函数的时候必须为返回的所有列都指定列的名称,否则就是报错

多语句表值函数

基本格式如下:

CREATE FUNCTION <Table_Function_Name, sysname, FunctionName>

(

 -- Add the parameters for the function here

 <@param1, sysname, @p1> <data_type_for_param1, , int>,

 <@param2, sysname, @p2> <data_type_for_param2, , char>

)

RETURNS

<@Table_Variable_Name, sysname, @Table_Var> TABLE

(

 -- Add the column definitions for the TABLE variable here

 <Column_1, sysname, c1> <Data_Type_For_Column1, , int>,

 <Column_2, sysname, c2> <Data_Type_For_Column2, , int>

)

AS

BEGIN

 -- Fill the table variable with the rows for your result set

 

 RETURN

END

GO

示例代码:

create function electric_amount_other(@start_time datetime,@stop_time datetime)

----计算其他电厂一段时间内的发电量

--returns @result table(

--rectime datetime,

--Eamount float

--)

--as

--begin

----获取其他电厂在一段时间内的上网电量

 

-- insert  into @result (rectime,Eamount)

-- select

--  TB_DataBase_OtherPlant_Online.RecTime ,

--  SUM( TB_DataBase_OtherPlant_Online.Power*1.0/12)

--  from TB_DataBase_OtherPlant_Online

--  where RecTime

--   between DATEADD(MINUTE,5,@start_time) and @stop_time

--   group by RecTime     

--return

--end

--go

对于多语句表值函数要首先指定返回表格中的数据类型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql server 函数 应用