I'm kind of stumped at this point. I have a block of code that when I run hangs on my SQL2005 server. In the past it's run fine but over the past couple weeks, it seems to despise me. Here's the odd thing, I can take the code out of the UDF and run it on it's own and it returns in <1 second. Putting the code into a UDF yields the function taking over 45 minutes to complete. Here's my UDF which seems to hate the thought of completing at the moment:CREATE FUNCTION [dbo].[fnAgentProductionReport]( @sp DATETIME, @ep DATETIME)RETURNS @report TABLE( unqempid int, [name] varchar(128), Sch int, callTime int, pr float, conf int, confTime int, cr float)ASBEGIN -- increment day to account for sql using midnight as a base date --SET @ep = @ep + 1 INSERT INTO @report (unqempid, [name], Sch, callTime, pr, conf, confTime, cr) SELECT DISTINCT c.unqempid, e.firstname + ' ' + e.lastname AS Name,0,0,0,0,0,0 FROM contacts c INNER JOIN employees e ON c.unqempid = e.unqempid WHERE contactdt between @sp and @ep UPDATE @report SET sch = fn.count FROM @report r INNER JOIN fnEmpScheduled(@sp,@ep) fn ON r.unqempid = fn.unqempid UPDATE @report SET callTime = fn.seconds FROM @report r INNER JOIN fnEmployeeTrackingTime(@sp,@ep,1) fn ON r.unqempid = fn.unqempid UPDATE @report SET pr = round(convert(float,sch) / convert(float,convert(float,callTime)/3600),2) WHERE isnull(callTime,0) IS NOT NULL AND convert(float,convert(float,callTime)/3600) > 0 UPDATE @report SET conf = fn.count FROM @report r INNER JOIN fnEmpConf(@sp,@ep) fn ON r.unqempid = fn.unqempid UPDATE @report SET confTime = fn.seconds FROM @report r INNER JOIN fnEmployeeTrackingTime(@sp,@ep,2) fn ON r.unqempid = fn.unqempid UPDATE @report SET cr = round(convert(float,conf) / convert(float,convert(float,confTime)/3600),2) WHERE isnull(confTime,0) IS NOT NULL AND convert(float,convert(float,confTime)/3600) > 0 RETURNEND
So in trying to troubleshoot what the holdup is (thinking it's an indexing issue somewhere) I modified the udf to this:DECLARE @sp DATETIMEDECLARE @ep DATETIMESET @sp = '5/8/09'SET @ep = '5/16/09'CREATE TABLE ##report ( unqempid int, [name] varchar(128), Sch int, callTime int, pr float, conf int, confTime int, cr float) -- increment day to account for sql using midnight as a base date --SET @ep = @ep + 1 INSERT INTO ##report (unqempid, [name], Sch, callTime, pr, conf, confTime, cr) SELECT DISTINCT c.unqempid, e.firstname + ' ' + e.lastname AS Name,0,0,0,0,0,0 FROM contacts c INNER JOIN employees e ON c.unqempid = e.unqempid WHERE contactdt between @sp and @ep UPDATE ##report SET sch = fn.count FROM ##report r INNER JOIN fnEmpScheduled(@sp,@ep) fn ON r.unqempid = fn.unqempid UPDATE ##report SET callTime = fn.seconds FROM ##report r INNER JOIN fnEmployeeTrackingTime(@sp,@ep,1) fn ON r.unqempid = fn.unqempid UPDATE ##report SET pr = round(convert(float,sch) / convert(float,convert(float,callTime)/3600),2) WHERE isnull(callTime,0) IS NOT NULL AND convert(float,convert(float,callTime)/3600) > 0 UPDATE ##report SET conf = fn.count FROM ##report r INNER JOIN fnEmpConf(@sp,@ep) fn ON r.unqempid = fn.unqempid UPDATE ##report SET confTime = fn.seconds FROM ##report r INNER JOIN fnEmployeeTrackingTime(@sp,@ep,2) fn ON r.unqempid = fn.unqempid UPDATE ##report SET cr = round(convert(float,conf) / convert(float,convert(float,confTime)/3600),2) WHERE isnull(confTime,0) IS NOT NULL AND convert(float,convert(float,confTime)/3600) > 0SELECT * FROM ##report
Any thoughts as to what I'm doing wrong or what could be going on?