SQLserver函数编写和使用方法

函数编写:

USE [CarTransport]
GO

/****** Object:  UserDefinedFunction [dbo].[Fn_GetFactBJ]    Script Date: 09/22/2016 14:24:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:    lnb
-- Create date: 2016-09-18
-- Description:    计算客户累计还款本金
-- =============================================
create FUNCTION [dbo].[Fn_GetFactMoney](@id int)

RETURNS decimal(18,2)
AS
BEGIN
 declare @TotalMoney decimal(18,2)
  set @TotalMoney=0.0
  select @TotalMoney=isnull(sum(isnull(FactBJ,0.0)),0.0)
  from  CustomOrderPay with (nolock) where MPaymentid=@id
  return @TotalMoney

END


GO

------------------------------------------------------------------------

 

函数的使用:   select [dbo].[Fn_GetFactMoney](b.pkid) as FactBJTotal,* from Custom b;

直接调用函数传入一个pkid就行了,是不是很简单

 

posted @ 2016-09-22 14:27  会一  阅读(13023)  评论(0编辑  收藏  举报