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 2000 Forums
 SQL Server Development (2000)
 Deleteing last month records.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-01-04 : 11:28:12
Kulwinder writes "I want to create a job in SQL Server that runs once a month to delete all the records of last month."

mfemenel
Professor Frink

1421 Posts

Posted - 2002-01-04 : 11:55:11
Please state your post in the form of a question....

Ok. Create a stored procedure and then schedule a job to run it. Your SP should look something like this

Delete from tablename
where(
month(getdate)-1=month(datecolumn)
and
year(getdate)=year(datecolumn)
)

You'll have to be careful when the year changes though and account for that in your code..i'm being lazy. For example if you ran this to remove december data, it wouldn't work as the year for get date would return 2002, but your data has a 2001 year. Of course if you're just getting rid of everything but the current month, you can just omit the year portion of the where clause.

Mike
"A program is a device used to convert data into error messages."
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2002-01-04 : 12:12:58
DELETE FROM tablename
WHERE DATEDIFF(m, datecolumn, GETDATE()) = 1


Edited by - Arnold Fribble on 01/04/2002 12:13:50
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2002-01-04 : 18:51:46
Mike,

Why put this in a stored procedure and then a job? Why not just put this text in the Job step itself? Is this just in case you need to run the same command elsewhere? Or do you perfer to keep everything in sprocs?

Mark

--------------------------------------------------------------
1000 Posts, Here I come! I wonder what my new title will be...
Go to Top of Page
   

- Advertisement -