I have this function that adds working days to a specific date and returns the date:
ALTER FUNCTION [dbo].[CalcWorkDaysAddDays]
(@StartDate AS DATETIME, @Days AS INT)
RETURNS DATE
AS
BEGIN
DECLARE @Count INT = 0
DECLARE @WorkDay INT = 0
DECLARE @Date DATE = @StartDate
WHILE @WorkDay < @Days
BEGIN
SET @Count = @Count + 1
SET @Date = DATEADD(DAY, @Count, @StartDate)
IF NOT (DATEPART(WEEKDAY, @Date) IN (1, 7)
OR EXISTS (SELECT *
FROM Holidays_Weeknds
WHERE Date = @Date))
BEGIN
SET @WorkDay = @WorkDay + 1
END
END
RETURN @Date
END
This function works perfectly in a small dataset. However I have almost 70k records in my table and I need to use this function for more than 10 columns in my table. The problem is that when I use this function it takes too long to run. Is there any way I can speed it up or maybe modify the UPDATE
statement that updates the columns in my table? My update statement looks like this:
UPDATE pt
SET [Predicted_Eng_Comp_date] = [dbo].[CalcWorkDaysAddDays](pt.Actual_Eng_Start_date, 20)
FROM [cntrra20-devbrs].PST.[dbo].[tbl_Project_tracker1] pt