In a table in my database, I have a date and time column (datetime) that stores the time when the event will occur. How can I delete that event 30 minutes after it happened?
Advertisement
Answer
You can schedule a task to run. This would do:
delete from t where datetimecol < dateadd(minute, -30, getdate());
But how do you prevent people from seeing this until it is deleted? Use a view:
create view v_t as select t.* from t where datetimecol >= dateadd(minute, -30, getdate());
Anyone accessing the view will only see the more recent events. Periodically — once a day, once a week, you can schedule a job to clean out the older events.