"how to automatically delete records in sql server after a certain amount of time " Code Answer

1

you might be able to get what you want without actually deleting the records. you can add a computed column into the table to say whether the record is valid.

 isvalid as (case when getdate() - createdat > 1 then 0 else 1 end)

you can also do this for key fields in the record, so you cannot find them:

_name varchar(255),
name as (case when getdate() - createdat < 1 then _name end)

you can then use a view to access the table:

create vw_logins as
    select name, . . .
    from t
    where isvalid = 1;

then, at your leisure, you can actually remove the rows if you need to for performance reasons.

edit:

you don't actually need a computed column, if you phrase the view as:

create vw_logins as
    select name, . . .
    from t
    where getdate() - createdat < 1;
By Phillip A on October 9 2022

Answers related to “how to automatically delete records in sql server after a certain amount of time ”

Only authorized users can answer the Search term. Please sign in first, or register a free account.