用户自定义函数  和存储过程是类似的,

是一组 有序的t-sql语句,udf被 预先优化和编译,并且可以作为一个单元来进行调用.

使用存储过程 时 可传入参数,传出参数.可以返回值,不过该值用于指示成功或者失败,而非返回数据.也可以返回结果集,

但是在没有将结果集插入到某种表(通常是临时表)中以供后面使用的情况下,不能在 查询中真正使用它们. 即使使用

表值 输出参数,在查询中使用结果之前,也要额外的一个步骤.

那么 UDF可以传入参数,但是不可传出参数. 但是可以返回值,和系统函数一样,可以返回标量值,这个值的好处是  不像存储过程那样只限于

整型数据类型,而是可以返回大多数sqlserver的数据类型.

下面具体介绍两种 UDF

  1. 返回标量的UDF
  2. 返回表 的UDF

返回标量的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) }

那么会在数据中看到:

执行结果和之前的sql语句是一样的.

返回表的UDF

既然UDF可以传入参数,又可以返回表,那是不是 就相当于参数化的视图……是不是听起来很让人兴奋呢.

下面创建一个简单函数:

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

下面调用这个函数:

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

结果如下:

下面 UDF结合 递归, 查询所有树的 节点

表:

数据:

下面定义函数:

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

执行函数,参数 是 任意父节点 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) }

效果:

执行:

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

效果:

将查出 所有的子节点来.

.net调用函数:http://www.cnblogs.com/Mr-Joe/archive/2012/05/10/2494093.html

sql server 2008学习11 UDF用户自定义函数相关推荐

  1. 使用一下SQL Server 2008中的新日期函数

    在我们开始讨论SQL Server 2008中新的日期数据类型之前,先来回顾一下SQL Server 2005中以及更老版本中提供的两种日期数据类型,这些旧的数据类型是DATETIME和SMALLDA ...

  2. sql server 2008 学习笔记

    sql server 2008 删除已有的实例 想从setup.exe中区卸载,没找到. 原来还是要从控制面板中卸载,卸载Microsoft SQL Server 2008 卸载界面会提示让你选择要删 ...

  3. sql server 2008学习10 存储过程

    输入输出参数: 给存储过程传参数,叫做输入参数,用户告诉存储过程需要 利用这个参数干些什么. 输出参数: 从存储过程得到那些数据. 创建一个可选参数的存储过程: create proc pa1 @na ...

  4. sql server 2008学习12 事务和锁

    事务 事务的点: 1.begin tran 是事务开始的地方,也是 事务回滚的起点.也就说他会忽略这个起点之后的最终没有提交的所有语句, 2.commit tran 事务的提交 是一个事务的终点 当发 ...

  5. sql server 2008学习8 sql server存储和索引结构

    sql server的存储机制 区段: 是用来为表和索引 分配空间的基本存储单元. 由 8个连续的页面构成,大小为64kb. 区段的注意事项: 一旦区段已满,那么下一记录 将要占据的空间不是记录的大小 ...

  6. sql server 2008学习5 sql基础

    查看数据库的信息: INFORMATION_SCHEMA.CHECK_CONSTRAINTS INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE INFORMATION_SC ...

  7. sql server 2008学习9 视图

    创建简单视图: use test go create view v1(视图名) as select name from b 这样视图就创建好了. 下面说下视图的本质: 当执行  select * fr ...

  8. sql server 2008学习4 设计索引的建议

    索引设计的建议: 一.检查where子句和连接条件列 当一个查询提交到sql server时,查询优化器尝试为查询中引用的所有表查找最佳的数据访问机制, 一下是它所进行的方式. 1.优化器识别Wher ...

  9. sql server 2008学习3 表组织和索引组织

    表组织 表包含在一个或多个分区中,每个分区在一个堆或一个聚集索引结构包含数据行.堆页或聚集索引页在一个或多个分配单元中进行管理,具体的分配单元数取决于数据行中的列类型. 聚集表.堆和索引 SQL Se ...

最新文章

  1. asp.net 调用vc dll_“双通道”独立通讯,稀有钛膜单元,击音运动果VC真无线耳机...
  2. js如何改变HTML属性,javascript – 如何动态设置HTML lang属性?
  3. JavaScript instanceof 运算符深入剖析
  4. sql 中位数_【PL/SQL 自定义函数】 常用场景
  5. 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?
  6. Extjs 实现Iframe的子窗口遮罩整个页面
  7. python tkinter教程-事件绑定_详解python tkinter教程-事件绑定
  8. 看宗萨蒋扬清者仁波切写的《正见:佛陀的证悟》的书评
  9. penetration test:渗透测试
  10. 【JVM】元空间与永久代区别
  11. 逆向工程实验——lab8(C/C++反逆向、Java字节码反逆向)
  12. Linux实现ppp拨号4G模块联网全球APN之中国(China)
  13. 物理光学2 麦克斯韦方程组与电磁波
  14. 基于社交模型的权证交易概念产品-废话性前言
  15. java格式化金额千位数,java金额格式化解决思路
  16. 【日常学习笔记】2019/1/(4,7)(SSM再熟悉与网页传值)
  17. 数据结构练手小项目(AVL树、哈希表、循环链表、MySQL数据库)
  18. MacOS专用防火墙Paragon Firewall可有效监视控制网络接入
  19. 微型计算机的功率,自己动手估算电脑的功率
  20. ARUBA无线移动网络产品和与CISCO无线产品比较

热门文章

  1. QT的QDBusContext类的使用
  2. QT的QAxFactory类的使用
  3. C++加号运算符重载
  4. ati显卡驱动安装linux,恭喜自己 ati显卡驱动安装成功
  5. 载荷谱matlab,收获机车架载荷测试及载荷谱编制方法研究
  6. 28 Java类的加载机制、什么是类的加载、类的生命周期、加载:查找并加载类的二进制数据、连接、初始化、类加载器、双亲委派模型、自定义类加载器
  7. SecurityUtil
  8. 打开高效文本编辑之门_Linux awk之自定义变量与操作符
  9. 检索数据_13_从表中查询空值
  10. Python 卡方检验演算