mssql 创建函数简单实例

CREATE FUNCTION [dbo].[f_DailyIncome]
(
    @userId int,
    @date date
)
RETURNS decimal(18,2)
AS
BEGIN
declare @income decimal(18,2);
    select @income = ISNULL(sum(uc.[TotalFee]*bcl.[Percent]),0) from Channel_User as cu
    left join Back_Purse as bp
    on bp.OwnerId = cu.LeaderId
    left join Back_ChannelLevel bcl
    on cu.ChannelLevelId=bcl.Id
    left join [Users] as u
    on cu.ChannelId=u.ChannelId
    left join [User_Consumptions] as uc
    on uc.UserId=u.Id
    where cu.UserId=@userId and uc.[Status]=3 and uc.AddDate=@date;
    -- Return the result of the function
    RETURN @income;
END

GO

 

posted @ 2016-06-01 09:34  小白ACE  阅读(744)  评论(0)    收藏  举报