定义

对于SQL Server来讲,我们声明一个变量的方式是用@变量名,而且相对于编程来讲,SQL Server声明的方式跟我们开了个玩笑,是先变量后面才是类型。对于需要传参跟不需要传参的方式,其实跟我们编程的方式一样。有参数则是如下方式:

CREATE FUNCTION GetSum
(
@firstNum int,
@secondNum int
)

如果没有参数,则只要保留括号即可。跟我们理解的函数写法一致。

CREATE FUNCTION GetSum
(
)

标量函数

所谓标量函数简单点来讲就是返回的结果只是一个标量,对于我来讲,返回的结果就是一种类型的一个值。
写法如下:

CREATE FUNCTION <Scalar_Function_Name, sysname, FunctionName>
(
-- Add the parameters for the function here
<@Param1, sysname, @p1> <Data_Type_For_Param1, , int>
)
RETURNS <Function_Data_Type, ,int>
AS
BEGIN
-- Declare the return variable here
DECLARE <@ResultVar, sysname, @Result> <Function_Data_Type, ,int> -- Add the T-SQL statements to compute the return value here
SELECT <@ResultVar, sysname, @Result> = <@Param1, sysname, @p1> -- Return the result of the function
RETURN <@ResultVar, sysname, @Result> 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

内联表值函数

内联表值函数返回的是表数据,表数据就是Table类型。
写法如下:

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

例子:

CREATE FUNCTION selectTeacherTest
(
@Name varchar(20)
)
RETURNS TABLE
AS
RETURN
(
select * from Teacher where Teacher.Name=@Name
)
GO

调用: 必须从返回表里面进行“查询”

select * from selectTeacherTest('刘英')

多语句表值函数

多语句表值函数跟内联表值函数都是表值函数,它们返回的结果都是Table类型。多语句表值函数顾名思义,就是可以通过多条语句来创建Table类型的数据。这里不同于内联表值函数,内联表值函数的返回结果是由函数体内的SELECT语句来决定。而多语句表值函数,则是需要指定具体的Table类型的结构。也就是说返回的Table,已经定义好要哪些字段返回。所以它能够支持多条语句的执行来创建Table数据。
写法如下:

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
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

例子:

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER FUNCTION DemoFun
(
)
RETURNS
@result TABLE
(
name nvarchar(20),
city nvarchar(20),
age int,
salary int
)
AS
BEGIN
-- Fill the table variable with the rows for your result set
insert into @result(name, city, age, salary)
select FName,FCity,FAge,FSalary from dbo.T_Person where FSalary>8000
insert into @result(name, city, age, salary) values
('测试','China', 1, 0)
RETURN
END
GO

其他

可以看得出,多语句表值函数的返回结果是定义好表结构的虚拟表。这又跟标量函数一样了吧,只不过标量函数是返回一种类型的标量值而已。而且在多语句表值函数里面,你也会发现最后一句是RETURN。告诉执行程序,多语句表值函数已经执行完成。函数体结构跟标量函数的结构一样。对于类型放在变量后面这种方式确实需要好好转换一下观念。

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>
)

参考:http://www.cnblogs.com/csdbfans/p/3514538.html

自定义函数Function的更多相关文章

  1. Sqlserver自定义函数Function

    一.FUNCTION: 在sqlserver2008中有3中自定义函数:标量函数/内联表值函数/多语句表值函数,首先总结下他们语法的异同点: 同点:1.创建定义是一样的:                ...

  2. SQL server 自定义函数FUNCTION的使用

    原文链接:https://blog.csdn.net/lanxingbudui/article/details/81736402 前言:        在SQL server中不仅可以可以使用系统自带 ...

  3. Oracle 自定义函数Function

    示例代码: CREATE OR REPLACE  FUNCTION "MY_DATABASE"."F_GET_USER_COUNT_BY_DEPART" ( D ...

  4. SQL中的自定义函数Function

    先给出一个链接吧,别人写的:http://www.cnblogs.com/diony/archive/2010/12/17/1909014.html 总结得很全面,感谢感谢!自己练习了一下后面的例子, ...

  5. SqlServer自定义函数Function中调用with as

    SET QUOTED_IDENTIFIER ON 标识符可以由双引号分隔,而文字必须由单引号分隔 SET QUOTED_IDENTIFIER OFF 标识符不可加引号,且必须遵守所有 Transact ...

  6. 使用 {$INCLUDE} 或 {$I} 指令管理和调用自定义函数

    这是一个简单.方便而又实用的小技巧. 譬如这段代码中有四个定义函数: MyAdd.MyDec.MyMul.MyDiv unit Unit1; interface uses   Windows, Mes ...

  7. FastReport调用Delphi中的人民币大写转换自定义函数

    FastReport调用Delphi中的人民币大写转换自定义函数   FastReport调用Delphi中的人民币大写转换自定义函数 function TJzpzEdit1.MoneyCn(mmje ...

  8. smarty 自定义函数

    自定义函数:<{方法名称}> 在lib/plugins中新建文件,命名方式是固定的:function.方法名称.php 或者 block.方法名称.php 1.<{literal}& ...

  9. 5.Smart使用内置函数或者自定义函数

    1.使用内置函数 例如使用date函数 {"Y-m-d"|date:$time}格式{第一个参数|方法:第二个参数:第三个参数}即可转换成 2016-07-19  2.使用resi ...

随机推荐

  1. Android String与十六进制数互转

    /** * 字符串转换成十六进制字符串 * @param String str 待转换的ASCII字符串 * @return String 每个Byte之间空格分隔,如: [61 6C 6B] */ ...

  2. php输出文件,数组

    file_put_contents('C://zll.txt',var_export($data,true));//输出数组 file_put_contents('C://zll.txt','你好啊' ...

  3. 关于android 怎样安装 assets文件下的apk

    在自己的app中安装assets文件夹下的apk文件 public class MainActivity extends Activity { Context mContext; @Override ...

  4. ie7easyui的书写要规范

    在书写easyui对象的属性,有时候习惯在,属性的末尾再加一个“,”,这个在高版本浏览器是没事的,但是在ie7及以下,会有报错

  5. oracle11g 在azure云中使用rman进行实例迁移

    1,開始备份 备份脚本rman_full_backup.sh内容例如以下: #!/bin/sh export DATE=`date +%F` export BACK_DIR='/backupdisk/ ...

  6. TensorFlow 实现深度神经网络 —— Denoising Autoencoder

    完整代码请见 models/DenoisingAutoencoder.py at master · tensorflow/models · GitHub: 1. Denoising Autoencod ...

  7. Android中再按一次退出实现

    很多应用中都有一个在用户后退的时候显示"再按一次退出"的提醒,这个怎么实现呢?有两种方式 第一种方式(最常用) long waitTime = 2000; long touchTi ...

  8. window.load和ready的差别

    1.运行时机: window.onload:必须等待网页所有加在完成(包含图片等),然后再运行包裹代码 $(document).ready():仅仅须要等待网页中的DOM结构载入完成.就能运行包裹的代 ...

  9. PPT之SmartArt功能

    在PPT中,我们经常看到这样的漂亮的组合图标: 他们是怎么做出来的呢?其实用ppt自带的SmartArt功能就能做出来了. Tips:SmartArt可以直接先选择组合图标再填文字,还可以写好了文字, ...

  10. C++ 类包含关系Demo 笔记

    is-a关系  类包含关系 构造 拷贝构造函数 重载福值运营商 析构函数 动态内存分配和释放 new delete操作 static 数据成员 好友功能 重载输入>>输出<<操 ...