0

i'm having table with columns

[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

on every month 1st at 00:00:00 i need to update the column EmployeeTotalNumberOfAnnualLeave increment by EmployeeNumberOfDaysOfAnnualLeaveInEachMonth value using trigger

With Regards

6
  • 5
    You may be looking for a scheduled job as opposed to a trigger Commented Jan 22, 2014 at 13:47
  • Triggers are meant to take action when some other action is performed in a table, not for executing scheduled tasks. Commented Jan 22, 2014 at 13:47
  • thank you...if i create a job and scheduled it....if backup my database and restored in other pc do i need to create a new job? Commented Jan 22, 2014 at 13:53
  • Yes. Jobs are not a part of database. Commented Jan 22, 2014 at 14:03
  • It would almost always be better to store the raw information about e.g. leave taken as discrete rows in an appropriate table and then just calculate these totals when required. When you store totals that could be computed from other data, you're immediately opening yourself up to the possibility that the total is incorrectly updated. Commented Jan 22, 2014 at 14:23

1 Answer 1

1

Assuming the SQL should be UPDATE Table SET EmployeeTotalNumberOfAnnualLeave = EmployeeTotalNumberOfAnnualLeave + EmployeeNumberOfDaysOfAnnualLeaveInEachMonth

You could:

1) Create a stored procedure to perform the task:

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

2) Create a scheduled job:

1) Expand Sql Server Agent

2) Right Click Jobs --> New Job

3) Give it a name

4) Go to Steps --> New --> Set it to execute your procedure

5) Schedules --> New --> Set it to execute on the first of each month

6) Enable the job

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.