I have table with the expiry date, if the expiry date is less than today's date I have to update the flag IsExpired = 1. I have tried with scheduling job, but it's not happening.
I have tried the following steps:
I have created a stored procedure to update the column
then I created a schedule that will run (execute the stored procedure) daily at 12:00 AM
You could create a view which populates its IsExpired column by checking the current date and the expiry date. You could then select from this view to know if a row has expired.
Make sure all the required authentication is correct. I mean (SQL Agent UserName ans Password).
Restart the Sql Server Services and Sql Agent as well. Then start you job schedule.
Related
I want to give users a period of time in which they can undo their account deletion. Someone suggested that I should use a CRON job that (1) checks my Supabase table daily for users who deleted their accounts over 30 days ago and (2) deletes those accounts. I have never used CRON jobs and I have very little experience with SQL. I have enabled the pg_cron extension in the Supabase Database settings but I don't know how to schedule the job or what to use for the SQL code. Below is my attempt at this, using a slightly modified Supabase example.
NOTE: The deleted_time is formatted as milliseconds since epoch so I need to compare that as a date.
select cron.schedule (
'webhook-every-minute', -- name of the cron job
'0 3 * * *', -- every day at 3 A.M.
$$ delete from users where DATEADD(ss, deleted_time,'01 JAN 1970') >= DATEADD(month,-1,GETDATE())
);
I am observing a strange behavior on our SQL Server 2014, Standard Edition (64-bit) which I cannot explain:
A simple select statement behaves differently when executed manually or via an SQL Job:
The sql-statement is as follows:
[USE DB2]
GO
Select * from DB1.dbo.price p
where
p.sec_id = 10 and
p.dt = CONVERT(date,getdate() - (case when datename(dw,getdate()) = 'Monday' then 3 else 1 end))
The statement pulls out the price record from table dbo.price for a certain security (sec_id = 10) for the previous business day which normally is 1 day prior, however on Mondays it is 3 days prior as there are only price records on business days available (1 price record per security per business day).
This sql-statement is embedded in a stored procedure which itself is executed via an SQL Server Agent Job.
The strange thing happening is:
If the above sql statement is executed "manually", i.e. via a query editor, it yields the correct result, i.e. one price record is returned when executed Monday to Friday.
The same is true when the above sql statement is executed "manually" via a stored procedure.
However, when the stored procedure containing the above statement is executed via an SQL Server Agent Job, the statement only returns a price record on Tuesday to Friday. On Mondays, the statement returns no record. (Even though the stored procedure respectively the sql statement return a record when executed manually).
Since the job is working Tuesdays to Fridays, it should not be any issue of privileges etc. And since the statement is working when executed manually, there shouldn't be any issue with the statement per se neither.
But why would it not work on a Monday when executed via an SQL job?
Would anybody have an idea what the reason could be? I have none unfortunately ...
Thanks a lot for any help.
Cheers
It's due to the default language of the identity that the Agent job runs under.
In your agent job add this to the script :
SET DATEFIRST 7
[or whatever day of week you expect to be deemed first day of week]
(it's connection specific, so won't affect other connections.)
Or you could change the default language of the login used by SQL Agent (or proxy if you are using one):
USE [master]
GO
ALTER LOGIN [LoginName] WITH DEFAULT_LANGUAGE = [SomeLanguage]
GO
Ref: SET DATEFIRST
As Mitch says, it's likely to be because of different language/date settings used by the Agent job.
My preferred fix though is not to fiddle with settings, but instead to pick a "known good" day with the correct property:
datename(dw,getdate()) = datename(dw,'20150720')
It so happens that 20th July 2015 (selection was entirely arbitrary, I just happen to have a 2015 desk calendar in eyesight) was a Monday and I'm using an unambiguous date format as my literal. So, whetever datename(dw,getdate()) happens to return on Mondays should always be what datename(dw,'20150720') produces.
Let's say that I have Equipment table and MaintenanceSchedule table in SQL database.
So I need to update the value of the Notification (bit) field in the Equipment table to be true if the Date field in the MaintenanceSchedule table approached.
So, how to track some data stored in the database and do some actions if a specific condition achieved ?
You should probably use a sql job to accomplish this. The problem is you want this process to be resilient in case of failure.
If you just set the flag after the Date field has approached, there are a couple potential bugs. For example, lets say the job doesn't run for some reason. If you run the job on the following day, can you be sure that the flag hasn't been properly set, then unset by the process which does the notification (or whatever processing is done). If you set it again, could it be duplicating work?
It would be best to create a MaintenanceHistory table which logs each time the Notification bit is set. Then you could build a stored procedure to run a job which checks if the Notification bit has been set for a particular Date, and if not, set the bit and log to the history table.
Then you could schedule this as a job which just executes this procedure and set to run at the desired frequency (hourly, daily, monthly, whatever). With this type of implementation, you can run the job as often as you like as it won't re-run for the same Date.
I would create a sql server job to check if the maintenance window aproaches and set the flag accordingly.
I'm rather new to SQL Server, but I am working on an app where a record is added to a table, and is given a DateTime stamp.
I want to be able to dynamically update the Status column of this row, 1 hour after the row was added.
Is this possible without running some server side script or store procedure every couple minutes? Is there an efficient way to accomplish this?
In Sql Server you can have Time Dependant or Action Dependent code execution.
Time Dependent
Time Dependant Code execution is handled via SQL Server Agent Jobs. You can execute a stored procedure or ad-hoc T-SQL code on a certain time of the day. It can be scheduled to execute on regular basis.
Action Dependent
Action Dependent Code execution is handled via Triggers (After/Instead of Triggers). A piece of code that is executed in response to a DML action INSERT, UPDATE or DELETE.
Solution
In your case you are trying to execute code in response to an action (Insert) after a certain period of time. I dont think there is an efficient way of doing it I would rather do the following....
You can have a Column called Created of Datetime datatype in your table and set a default value of GETDATE().
Now you dont need the status column. All you need is a query/View which will check at runtime if the row was added more than an hour ago and will return it STATUS as required.
Something like.....
CREATE VIEW dbo.vw_Current_Status
AS
SELECT *
, CASE WHEN DATEDIFF(MINUTE, Created, GETDATE()) >= 60
THEN 'OLD'
ELSE 'New' END AS [Status]
FROM TABLE_NAME
In MSSQL Server 2008, how would I go about creating a scheduled process that:
Takes the sum of a float column from specific users in a user column and then comparing which is has the greatest sum and storing that number along with the user whom has that value into a separate table on a weekly basis?
Create a SQL Server scheduled job that executes a stored procedure or raw SQL.
Based on your description, the query could look like this:
insert into table (username, sumofcolumn)
select top 1 username, sum(column)
from table2
group by username
order by sum(column) desc
Personally I prefer to write a service which performs actions periodically, since I have better control of when the actions are to be executed, and everything is in a single place.
If you want to solve your problem with database means only, just create a stored procedure implementing your logic, and call that stored procedure from a scheduled job.