我需要写一个proc,如果我的实际日期超过3天,我需要更新ACTUAL_FINISH日期。这三天不会考虑周六周日。
select *
from MILESTONE_SCHEDULE
WHERE MILESTONE_TYPE_ID = 398
and ACTUAL_FINISH IS NULL
AND ACTUAL_START IS NOT NULL;
答案 0 :(得分:0)
假设您的MILESTONE_SCHEDULE
表格中有唯一的ID,为了达到您所寻找的目标,您可以尝试以下内容。
UPDATE T
SET ACTUAL_FINISH = GETDATE()--or whatever you want
FROM MILESTONE_SCHEDULE T
INNER JOIN
(
SELECT ID,
(DATEDIFF(dd, ACTUAL_START, GETDATE()) )
-(DATEDIFF(wk, ACTUAL_START, GETDATE()) * 2)
-(CASE WHEN DATENAME(dw, ACTUAL_START) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, GETDATE()) = 'Saturday' THEN 1 ELSE 0 END) AS DayDif
FROM MILESTONE_SCHEDULE T
) T1
ON T1.ID= T.ID
WHERE T1.DayDif > 3
AND T.MILESTONE_TYPE_ID = 398
AND T.ACTUAL_FINISH IS NULL
AND T.ACTUAL_START IS NOT NULL;
使用虚拟数据的示例
以下查询将有助于更新值。
DECLARE @MILESTONE_SCHEDULE TABLE(ID INT ,ACTUAL_START DATETIME ,ACTUAL_FINISH DATETIME)
INSERT INTO @MILESTONE_SCHEDULE
VALUES(1,GETDATE()-10,NULL),(2,GETDATE(),NULL)
UPDATE T
SET ACTUAL_FINISH = GETDATE()
FROM @MILESTONE_SCHEDULE T
INNER JOIN
(
SELECT ID,
(DATEDIFF(dd, ACTUAL_START, GETDATE()) )
-(DATEDIFF(wk, ACTUAL_START, GETDATE()) * 2)
-(CASE WHEN DATENAME(dw, ACTUAL_START) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, GETDATE()) = 'Saturday' THEN 1 ELSE 0 END) AS DayDif
FROM @MILESTONE_SCHEDULE T
) T1
ON T1.ID= T.ID
WHERE T1.DayDif > 3
SELECT * FROM @MILESTONE_SCHEDULE
<强>输出:强>
ID ACTUAL_START ACTUAL_FINISH
1 2018-03-02 11:26:25.707 2018-03-12 11:26:25.707
2 2018-03-12 11:26:25.707 NULL