SQL语句创建函数
Posted 幽默是一种心情
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句创建函数相关的知识,希望对你有一定的参考价值。
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 @[email protected]+‘,‘+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语句创建函数的主要内容,如果未能解决你的问题,请参考以下文章