0

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??

2 Answers2

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
  • 1
    I 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