先感谢您阅读此问题并提供宝贵的答案。
即将到来......
我编写了一个sql标量值函数,它由一个临时表和一个简单的while循环组成。功能如下。
功能名称dbo.udfnGetEmpActualPayRate
declare @TotalDeals int
declare @count int=1
declare @EmpRate varchar(Max)=''
declare @DealTemp table (
sno int,
HrsWorked int,
rate money
)
delete from @DealTemp
insert into @DealTemp
select ROW_NUMBER()OVER (ORDER BY inv.consultantrate)
,SUM(td.changedhours)
,inv.ConsultantRate
from Invoice_Transactions_tbl inv
inner join Payroll_Details_tbl pd on pd.TimesheetDetailId=inv.TimeSheetDetailId
inner join TimeSheet_Detail_tbl td on td.TimeSheetDetailId=pd.TimesheetDetailId
where pd.PayrollId=@PayrollId and inv.ConsultantId=@EmpId
group by inv.ConsultantRate
set @TotalDeals=(select count(distinct inv.ConsultantRate) from Invoice_Transactions_tbl inv
inner join Payroll_Details_tbl pd on pd.TimesheetDetailId=inv.TimeSheetDetailId
inner join TimeSheet_Detail_tbl td on td.TimeSheetDetailId=pd.TimesheetDetailId
where pd.PayrollId=@PayrollId and inv.ConsultantId=@EmpId)
while(@count<=@TotalDeals)
begin
if(@EmpRate!='')
begin
set @EmpRate=@EmpRate+' , '
end
set @EmpRate=@EmpRate+(select convert(varchar(30), rate, 1)+' Rate for Hours '+convert(varchar(30), HrsWorked, 1) from @DealTemp where sno=@count)
set @count=@count+1
end
if(@EmpRate='')
begin
set @EmpRate='0.00 Rate for Hours 0'
end
return @EmpRate
但是当我在select查询中使用相同的函数时,返回值需要很长时间。
以下2个图像表示使用函数
之前和之后的选择查询的响应使用后
任何人都可以帮我找出问题所在