Dynamically update "Status" column after "X" amount of time - sql

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

Related

How to execute sql TRIGGER automatically after every 5 seconds in SQL SERVER?

I want to make a stored procedure by which a Triggers will automatically execute after 5 seconds to check/show whether new row is updated or not.
I have a table called 'Inbox' in Database. I made a trigger for this whenever data in inserted in table.
CREATE TRIGGER tr_Inbox_ForInsert
ON Inbox
FOR INSERT
AS
BEGIN
SELECT * FROM inserted
END
I just want this trigger to execute itself after 5 seconds
That's not how triggers work... They are run after a transaction is carried out on the object they are attached to.
If you want something to run every X time interval, look into SQL Server Agent Jobs instead

SQL Server - determining statement starting time from within trigger

I'm trying to find a good solution for one task problem can't find a good one yet. So my question is - is it possible to get 'statement starting time' inside the trigger context. Basically, the starting time of update (insert, delete) which caused trigger to fire?
I've tried a few data management views, like sys.dm_tran_active_transactions, sys.dm_exec_requests and couple of others.
I can get the starting time of the full SQL batch, or starting time of the transaction from the views I mentioned (using current ##spid), but can't find starting time of 'trigger firing statement'
Do you know if it's even possible in SQL Server?
You can use getdate() functiuon in your trigger like:
select getdate()
from inserted i

How to track records in SQL and do some actions if it achieved a specific condition?

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.

How to update table values automatically every day

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.

how to create a scheduled process in sql server

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.