每年的SQL Server重置列

时间:2019-01-08 14:50:52

标签: sql-server

在SQL Server中可以这样做吗?如果是这样,我将如何处理?

我有一个名为“雇员”的表,如下所示:

enter image description here

最后三列是使用以下查询制成的计算列:

带薪休假:

ALTER TABLE [dbo].[Employees]
ADD TotalHoursThisYear as 
(
CASE 

WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate]),01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+1,12,31)  THEN 0 

WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+2,01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+2,12,31) THEN 8 

WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+3,01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+3,12,31) THEN 16

WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+4,01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+4,12,31) THEN 24  

WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+5,01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+5,12,31) THEN 32 

WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+6,01,01) AND D 
DATEFROMPARTS(YEAR([StartDate])+1000,12,31) THEN 40

END 


)
GO

今年总时长:

ALTER TABLE [dbo].[Employees]
ADD TotalHoursThisYear as CAST 
(
CASE 

WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate]),01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+1,12,31)  THEN 0 + [HoursCarriedForward]  
WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+2,01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+2,12,31) THEN 8 + [HoursCarriedForward] 
WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+3,01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+3,12,31) THEN 16 + [HoursCarriedForward] 
WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+4,01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+4,12,31) THEN 24 + [HoursCarriedForward] 
WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+5,01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+5,12,31) THEN 32 + [HoursCarriedForward] 
WHEN GETDATE() BETWEEN DATEFROMPARTS(YEAR([StartDate])+6,01,01) AND 
DATEFROMPARTS(YEAR([StartDate])+1000,12,31) THEN 40 + [HoursCarriedForward] 

END 

+

CASE 
WHEN DATEFROMPARTS(YEAR(StartDate), 12,31) >= GETDATE() THEN datediff(day, 
[StartDate],datefromparts(datepart(year,[StartDate]),(12), 
(31)))/(30.42)*13.33
ELSE [ALCategory]
END AS INT 

)
GO

剩余的权利:与刚刚的TotalHoursThisYear-在HoursCarriedForward之后花费的小时数相同。

我的问题是,是否可以设置触发器或其他方式,以便在每年的年末(12月31日午夜)将EntitlementRemaing的剩余值添加到HoursCarriedForward列中,然后将HoursTaken重置为0。

我还应该添加,HoursTaken与另一个记录假期请求的表保持连接。

使用它在asp.net mvc中构建应用

1 个答案:

答案 0 :(得分:1)

您要做的是编写一个简单的存储过程,以进行所需的数据修改。只是外观上的UPDATE声明。

然后使用适合您的组织的作业计划软件(SQL Agent,Control-M,您拥有什么)在年底运行作业。