I want to automatically delete a record from my database sql after 2 days of inserting the record. I am currently developing a simple reservation. How can I achieve this??
Asked
Active
Viewed 739 times
0
-
possible duplicate: http://stackoverflow.com/a/18181618/2460773 – Nitsan Baleli Feb 04 '15 at 09:37
-
possible duplicate of [Get Data From Last 48 Hours](http://stackoverflow.com/questions/21586548/get-data-from-last-48-hours) – wayzz Feb 04 '15 at 09:38
-
You need to be more clear. Does the record contain create date. If so then you need a trigger – szakwani Feb 04 '15 at 09:38
-
Im creating a simple reservation. And what I want is to delete the record automatically after 2 days. DELETE FROM yourTable WHERE DATEDIFF(day,getdate(),thatColumn) < -1 – Dave Espada Feb 04 '15 at 09:52
-
Where do i put this code? – Dave Espada Feb 04 '15 at 09:52
-
@DaveEspada: Put it in an event. Defne one in your SQL engine that runs every day – juergen d Feb 04 '15 at 10:04
2 Answers
0
Add an event to your table that runs once every day and deletes those entries. You need to have a date
column though to recognize these records.

juergen d
- 201,996
- 37
- 293
- 362
0
Assuming you're using some sort of timestamp on insert then you can run a scheduled job that checks the age of the record with datediff
and then deletes the record if datediff = 2
days

Christian Barron
- 2,695
- 1
- 14
- 22
-
I want to Automatically delete a record from the database without a command from the user.. is this code right? DELETE FROM yourTable WHERE DATEDIFF(day,getdate(),thatColumn) < -1 and where do I put it? – Dave Espada Feb 04 '15 at 09:54
-
1I would do: Datediff(Day,thatColumn, GetDate()) >= 2 but yes that will work. You need to create a job for it. If you're in SQL Server go into SQL Server Agent and create a new job, then assign a schedule to that job. If you let me know what DBMS you're using I can try help futher – Christian Barron Feb 04 '15 at 10:13