使用触发器更新SQL表

时间:2014-01-22 13:44:43

标签: sql sql-server-2008 triggers

我正在用列表

[ID] [bigint] IDENTITY(1,1) NOT NULL,
[CompanyID] [bigint] NULL,
[EmployeeName] [nvarchar](50) NULL,
[EmployeeGSM] [nvarchar](50) NULL,
[EmployeeNumberOfDaysOfAnnualLeaveInEachMonth] [decimal](5, 2) NULL,
[EmployeeTotalNumberOfAnnualLeave] [decimal](7, 2) NOT NULL

每月1日00:00:00我需要使用触发器更新EmployeeNumberOfDaysOfAnnualLeaveInEachMonth值的EmployeeTotalNumberOfAnnualLeave增量列

问候

1 个答案:

答案 0 :(得分:1)

假设SQL应为UPDATE Table SET EmployeeTotalNumberOfAnnualLeave = EmployeeTotalNumberOfAnnualLeave + EmployeeNumberOfDaysOfAnnualLeaveInEachMonth

你可以:

1)创建一个存储过程来执行任务:

CREATE PROCEDURE UpdateEmployeeTotalNumberOfAnnualLeave
AS
BEGIN
    UPDATE Table 
    SET EmployeeTotalNumberOfAnnualLeave = EmployeeTotalNumberOfAnnualLeave + EmployeeNumberOfDaysOfAnnualLeaveInEachMonth
END

2)创建预定作业:

  

1)展开Sql Server Agent

     

2)右键单击作业 - >新工作

     

3)给它起个名字

     

4)转到步骤 - >新 - >将其设置为执行您的程序

     

5)时间表 - >新 - >将其设置为在每个月的第一天执行

     

6)启用作业