create function getSum(@date date) returns decimal(18,2) as begin declare @number decimal(18,2)=0 while(@date<='2020/10/30') begin declare @tempdata decimal(18,2)=(select CZ_Amount from CZ_ProcessData where BV_VarName='E12M_RN111811' and CZ_Date=year(@date)*10000 + month(@date)*100 + day(@date)) declare @totaltime decimal(18,2)=(case when @tempdata is null or @tempdata=0 then 0 else @tempdata end) set @number+=@totaltime set @date=dateadd(day,1,@date) end return @number end select dbo.getSum('2020/9/2') total
注意returns后面加的是数据类型