函數編寫:
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就行了,是不是很簡單