A trigger that periodically deletes data

I would like to have a trigger that deletes some rows in a given table as a given time (for example, 10am every day). How to implement this?

+3


source to share


1 answer


Full SQL Server

Instead of a trigger, you can set your code in a stored procedure and define a scheduled job to call that sp every day at 10am.

You create an assignment:

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

Its command should be a call to your sp cleanup, something like:

exec (your stored proc name) (and possibly add parameters)

      

And you plan to call it once a day:



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

SQL Server Express

Since you have SQL Server Express and cannot use SQL Server Agent, you can instead use the Windows Task Scheduler itself to invoke the cleanup code once a day.

Create a script file clean.sql with your delete commands and create a batch file clean.bat as well to execute it. Sample batch file:

sqlcmd -i cleaning.sql

      

Place these two files in the same folder and set the task scheduler to run this batch every day at 10am.

+6


source







All Articles