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
对于多语句表值函数要首先指定返回表格中的数据类型
调用形式如下:
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
对于多语句表值函数要首先指定返回表格中的数据类型
相关文章推荐
- 100 个最佳 Ubuntu 应用(中)
- 在 AppImage、Flathub 和 Snapcraft 平台上搜索 Linux 应用
- 24 个必备的 Linux 应用程序
- 注册表趣味应用小集
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 远程控制技术的应用
- 文件遍历排序函数
- 路由器访问列表的应用
- xDSL技术及其应用
- 关于C#中排序函数的总结
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- 推荐Sql server一些常见性能问题的解决方法
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例