码迷,mamicode.com
首页 > 数据库 > 详细

SQL语句创建函数

时间:2018-04-21 12:49:19      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:lin   int   sele   alt   ret   max   ted   rom   begin   

alter function fuc (@userid int,@strWhere varchar(max),@strWhere2 varchar(max) )  returns decimal(18,2 )
as
begin
declare @useridlist varchar(max)=‘‘,@res decimal(18,2)=0;
with cte as
(
    select ID,1 Layer from t_user  where ID=64
    union all
    select a.ID,Layer=Layer+1 from cte inner join t_user a on a.UPLineID=cte.ID  and cte.Layer<=4
)select @useridlist+=@useridlist+‘,‘+cast(ID as varchar(10)) from cte   where  cte.Layer<=3
 
declare @UserGradeDateLine varchar(max)=‘‘;
 select @UserGradeDateLine=UserGradeDateLine4 from t_user  where ID=64
    if(@UserGradeDateLine!=‘‘ and datediff(d,@UserGradeDateLine,@strWhere)>0 and datediff(d,@UserGradeDateLine,@strWhere2)>=0)
    begin
    select @res=isnull(sum(amount),0) from t_orderlucre  where CHARINDEX(‘,‘+CAST(UserID2 as varchar(10))+‘,‘,@useridlist+‘,‘)>0  and GroupSign =3  and ( datediff(mi,@UserGradeDateLine,AddTime)>0  and    datediff(d,AddTime,@strWhere2)>=0)   
    end   
    else
     begin
     select @res=isnull(sum(amount),0) from t_orderlucre  where CHARINDEX(‘,‘+CAST(UserID2 as varchar(10))+‘,‘,@useridlist+‘,‘)>0  and GroupSign =3  and ( datediff(d,@strWhere,AddTime)>0  and    datediff(d,AddTime,@strWhere2)>=0)     
     end
return @res;
end

SQL语句创建函数

标签:lin   int   sele   alt   ret   max   ted   rom   begin   

原文地址:https://www.cnblogs.com/weimingxin/p/8900499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!