SQL Server: Delete a record after a period of time automatically? -


i have table has 2 attributes: id & datetime when record created. how can make trigger (procedure?) delete record after, say, 1 day?

edit: added 'automatically' in title, implies want task executes every x time instead of manually having it.

i suggest use sql server agent , write stored procedure deletes every rows date passed 1 day.

you can find how use sql server agent jobs here :

https://docs.microsoft.com/en-us/sql/ssms/agent/schedule-a-job

and stored procedure :

create  procedure deleterows() begin delete mytable (datediff(day,  dateadd(day, -1, datecolum), getdate())) >= 1 end 

edit : number 1 in statement days. can change want use.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -