我需要編寫一個存儲過程,給定日期和工作日數,將給定日期添加到該日期並返回新日期,工作日和週末。非工作日存儲在另一個表中。SQL Server存儲過程將日期添加到日期
這是我的第二個存儲過程,所以我對lexic不太熟悉,所以,如果您發現明顯的錯誤,請提前抱歉。
到目前爲止,我已經得到了這一點:
CREATE PROCEDURE DateAdd
(@GivenDate DATE, @DaysToAdd int)
DECLARE @ReturnDate DATE,
DECLARE @Counter int,
DECLARE @NextDate DATE
AS
SET @Counter = 0
SET @ReturnDate = @GivenDate
SET @NextDate = @GivenDate
GO
WHILE (@Counter < @DaysToAdd)
@Counter + 1
IF(datepart(weekday, @FechaVariable) !=6 &&
datepart(weekday, @FechaVariable) != 7)
IF(#TODO-- call the query and check it with @NextDate)
@FechaRetorno = DateAdd(dd, 1, @FechaRetorno)
ELSE IF @NextDate = DateAdd(dd, 1, @NextDate)
EN IF
END WHILE
-- I don't know where to put this query, or how to call ir from the IF
SELECT Date
FROM non_working_days
WHERE Date = $Variable
RETURN @FechaRetorno
[壞習慣踢:使用帶有日期/時間操作速記 - 亞倫貝特朗(http://sqlblog.com/blogs/aaron_b ertrand/archive/2011/09/20/bad-habit-to-kick-using-short-with-date-time-operations.aspx) – SqlZim