How to change a column value based on date (every year) - sql

I am working on a leave management system, and this is my first web application. I have a LocalDB sql server in visual studio 2010 and I need some columns to be updated every year.
For example, I have SickLeave column (int), I need this column to be updated with the value (15) in a specific date (every year), the date will be different from one employee to another based on his joining date.
I have searched a lot, there is something called scheduled event in mySQL that can do something similar, but it doesn't work for sql server.
So anyone has an idea how to do it? Your help will be appreciated.
Thanks in advance.

You can use a SQL Agent Job.
SQL Agent: https://msdn.microsoft.com/en-us/library/ms189237.aspx
Create a Job: https://msdn.microsoft.com/en-us/library/ms190268.aspx
We use these for all sorts of scheduled tasks.
In your case you could create Job that runs Daily, the job can execute a StoredProc (or simple Update statement).
UPDATE Employee SET SickLeave = 15 WHERE DAY(JoinedDate) = DAY(GETDATE()) AND MONTH(JoinedDate) = MONTH(GETDATE())

Related

How to calculate the next run date for a SQL Server agent job

I am developing a custom system for scheduling task & I am following Microsoft SQL Server Agent approach to schedule task.
I have replicated sysschedules table for capturing users scheduling occurrences and timings.
Now I need to know how SQL Server get the Next_run_date and time from this table.
I have found a solution on the internet which are using sysjobhistory table to get the next run date, but in my case, I have an only sysschedules table. So, what is the formula to calculate the next_run_date and time for any job?
Note: I am looking for entire code, concept/algorithm will work for me.

Hyperion Reporting - Query for prior day (DB2 and Oracle)

I am using Hyperion Reporting Studio. I have a report where I want to calculate the turn around time for messages that come in to my department.
I need to find a way, whether it's custom SQL or just a feature for the report to always pull the data from the prior day. I have an Open_Date filter where the setting is > 06/06/16 12:00 AM.
However I will always need the date to be the day prior to the current one. I will be using EPM which allows you to setup recurring reports, that run then get emailed to you on a daily basis, automatically. I need to figure out some custom SQL Hyperion can use in my date field and have not found any solutions.
Additional info: Using Hyperion Interactive Reporting Studio; DB2 and Oracle Databases.
It depends on your backend (DB2 or Oracle) which syntax you use. Also, do you want "yesterday" relative to the user, or to the server? Assuming the latter, because this sounds like a job on the server.
I think what you're looking for is:
CURRENT DATE for DB2 and
SYSDATE for Oracle
These are the equivalent of "today" relative to the server's date and time. Will the job run after midnight? It might be as simple as adding -1 but you could run into trouble if the job runs before midnight sometimes, and after midnight other times (don't know what would happen if the job ran through midnight).

Creating EVENTS in SQL Server

I want to execute a query at a specific time. In MySQL we use events for that, example is as follows:
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
Please let me know how to do the same in SQL Server. Thanks.
PS : Sorry if its a repeated question. I tried searching for some time but didn't get it in SQL Server
You can use Sql server agent for this task and you can schedule there. Its easy way.
Another one is "waitfor time" check this link http://msdn.microsoft.com/en-IN/library/ms187331.aspx
(and you need to create a trigger with while loop logic and there wait for time is useful)
Another one create a bat file and schedule that in windows scheduler (if you are using sql sever express edition)

Using windows service read/update SQL server 2008 R2

I am new to Entity Framework (MVC) methodology, what I need to do is create a windows service that would query the table messageinfo in the database for a column msgtype.
msgtype that I am looking for is 10 (which translates as to be archived)
In such a case I need to move (not copy) the whole corresponding row to another table backupmessageinfo.
This has to be done using a windows service, which can be scheduled to run at specific time lets say 12 am every day.
Please help!
Any pointers much appreciated!!
Philip
IMHO you can achieve the desired result a lot easier utilizing SQL Server Agent for scheduling and writing a simple sql script that will consist of INSERT INTO ... SELECT FROM ... and DELETE statements.

SQL Server Agent Job for the first 4 days of Month

I was wondering if anyone knew how to create a job that will run the first 4 days of the month in SQL Server 2000? I found how to run a job once on the first or second day, but the only way I figured I could run each of the four days would be to create a job for each day meaning I would have 4 jobs created. I was hoping for a better way.
Instead of creating 4 jobs, you can create one job with 4 schedules.
Fire up enterprise manager, create your job as you normally would and on the schedules tab of the Job properties add 4 schedules, one for each day of the month you'd like the job to run.
Sorry I don;t have SQL 2000 to hand but can you not once the job is created add 4x schedules to the job (Edit Job > New Schedule or similar)
Sorry if this isn't available, you can use this method in 2008
he he, As Jason says