UDF和存储过程很类似,用户自定义函数是一组有序的T-SQL语句,这些语句被预先优化和编译,并且可以作为一个单元来测试调用。UDF和存储过程的主要区别在于结果返回方式,为了能支持更多返回值,UDF比存储过程有跟多限制。

UDF基本语法:CREATE FUNCTION <function name> (<@parameter name> <data type>[default value][......n)

                    RETURNS  <返回类型>

                    AS  begin  /*代码*/  end

试一试最简单是UDF:

CREATE FUNCTION  fun_GetDateDIff(@startDate datetime,@endDate datetime)
returns int
AS 
BEGIN
   return  datediff(month,@startDate,@endDate);
END

GO

select ProductID,sum(LineTotal) from Purchasing.PurchaseOrderDetail where dbo.fun_GetDateDIff(DueDate,getdate())=142 /**/  group by ProductID --查询前的142的数据
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-08
猜你喜欢
  • 2021-06-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-16
  • 2021-11-19
相关资源
相似解决方案