Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to check & delete data automatically.

Author  Topic 

partap26
Starting Member

27 Posts

Posted - 2013-06-03 : 23:46:12
how to check and delete data that is 2 months old in the database. I want my query automatically run everyday and delete the data from the database that is 2 months old... what is the mechanism behind this...

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-04 : 00:09:30
do a procedure like

CREATE PROC PurgeOldData
AS
DELETE FROM table WHERE datefield < DATEADD(mm,DATEDIFF(mm,0,GETDATE())-2,0)
... repeat for each table you want
GO


then create a sql agent job to execute this procedure daily as per your convenient schedule

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

partap26
Starting Member

27 Posts

Posted - 2013-06-04 : 00:18:16
What is PurgeOldData and it will automatically run.
If not then i want a mechanism that runs automatically
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-04 : 00:32:49
quote:
Originally posted by partap26

What is PurgeOldData and it will automatically run.
If not then i want a mechanism that runs automatically


PurgeOldData is just the name i gave to procedure
You can name it whatever you want like partapdeleteprocedure etc

It will run automatically once you call it from sql agent job and schedule it as I specified

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

partap26
Starting Member

27 Posts

Posted - 2013-06-04 : 00:41:43
I have no idea how to create agent job. I am using MS SQL Server Management Studio.... If you do not mind then would please help me for creating agent jobs.........
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-04 : 00:47:20
see

http://blog.ropardo.ro/2011/07/27/how-to-create-a-sql-server-agent-job-transact-sql-with-only-needed-fields/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -