sqlserver 函数 详细例子

sqlserver 函数 详细例子,第1张

概述  --两种UDF       --标量函数  返回单个数据值,并且其类型是在return字句中定义的   create function SUMEmp2(@emp1 int)   returns  int    AS    begin   --声明变量@emp2count ,存储总值  

 

  --两种UDF                --标量函数  返回单个数据值,并且其类型是在return字句中定义的      create function SUMEmp2(@emp1 int)      returns  int       AS       begin      --声明变量@emp2count ,存储总值      declare @emp2Count int      select @emp2Count =SUM (emp2)      from dbo.pp where Emp1=@emp1      --如果变量为空,重新赋值0      if(@emp2Count is null)      set @emp2Count =0     print @emp2count      return @emp2count      end           --函数调用      select VerdorID,dbo.SUMEmp2(emp1) from pp              --PS :标量函数返回一个单独的值,通常在列表和where子句中使用       --标量函数位置              --查询中: select 语句中       -- where 或having 字句中       -- 作为 update 中set字句中              -- T-SQL语句: case表达式中       -- print 语句中(只适用于字符串函数)       -- 作为存储过程的return 语句(只适用于返回整数的标量函数)               -- 函数和存储过程中:作为用户自定义函数的return字句,前提是被调用的用户函数返回的值可隐式转换为进行调用的函数的返回值类型                                                 ---表值函数       --表值函数遵守与标量函数相同的规则,区别在于表值函数返回一个表作为输出       ---一般在select 语句的from 字句中进行引用,并可以与其他表或视图进行连接                   --内联表值函数可以实现参数化视图的功能       --视图的一个局限性是在创建视图是,不允许在视图中包括用户提供的参数,通常可以在调用视图时提供的where字句解决问题       --创建表值函数       create function FunctionAA (@emp1 int)       returns table  --return 指定表为返回的数据类型       as        return(       select A.* from pp A inner join pp B on a.VerdorID=b.VerdorID        where a.Emp1=@emp1)--select 语句的结果定义了返回变量的格式              --函数的内容是一个单条的select语句,内联函数使用的select语句与视图中使用的select语句,受到同样的限制       --该函数的主体不需要包含在begin ..end 中       --调用        select * from FunctionAA(4)               --可在通常使用视图的任何地方使用内联表值函数                                 --多语句表值函数 是视图和存储过程的结合,可使用返回表的用户定义函数来代替存储过程或视图       create function functionBB (@sex varchar(20))       returns @emptable table  ---returns 定义表为返回值类型,并定义了结构(名称和格式)       (ID int IDentity(1,1) primary key not null,name varchar(20),class varchar(20))       AS       begin --begin...end 界定函数主体        if(@sex='1')        begin        insert into @emptable        select name,('T'+class) from student3 where sex=1         --或 select name,('T'+class) from student3 where sex='true'       end         else if(@sex='0')        begin         insert into @emptable        select name,('F'+class) from student3 where sex='false'       --或 select name,('F'+class) from student3 where sex=0       end         else        begin        insert into @emptable        select name,('null'+class) from student3 where sex is null        end       return        end              --调用       select * from functionBB ('0')                ---表值函数返回一个表变量,并在from字句使用                       ---确定性与非确定性函数      --对于相同的输入值函数,每次调用确定性函数则返回相同的值 如:内置函数cos      --对于相同的输入值函数,每次调用非确定性函数则返回不同的值 如:内置函数getdate()      --一个函数是非确定性还是确定性,决定了是否在该函数结果集上建立索引,以及能否在引用该函数      --的视图上定义聚集函数, 如果一个函数是非确定行的,就不能索引该函数的结果    
总结

以上是内存溢出为你收集整理的sqlserver 函数 详细例子全部内容,希望文章能够帮你解决sqlserver 函数 详细例子所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1179404.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存