Solved

The housekeeping task of MSDB

Posted on 2012-04-10
6
694 Views
Last Modified: 2012-05-09
Dear all,

I found out that if I use the SSIS method to create the history cleanup task, which is only designed for MSDB, I can generate the following t-SQL script:


declare @dt datetime select @dt = cast(N'2012-03-13T19:21:30' as datetime) exec msdb.dbo.sp_delete_backuphistory @dt
GO
EXEC msdb.dbo.sp_purge_jobhistory  @oldest_date='2012-03-13T19:21:30'
GO
EXECUTE msdb..sp_maintplan_delete_log null,null,'2012-03-13T19:21:30'

But it seems that it "Hard code" a date for me and I can't see how can I make the script dynamic so that the current date is the current data everyday. So I can script this one to safely remove historical data older than 2 weeks.

Any ideas?

DBA100.
0
Comment
Question by:marrowyung
  • 3
  • 3
6 Comments
 
LVL 21

Accepted Solution

by:
Jason Yousef, MS earned 500 total points
ID: 37827448
Hi, can you use  " dateadd(WK, -2, getdate()) " to get you the date for 2 weeks ago?

something like that

DECLARE @dt DATETIME = dateadd(WK, -2, getdate())

exec msdb.dbo.sp_delete_backuphistory @dt

EXEC msdb.dbo.sp_purge_jobhistory  @oldest_date= @dt

EXECUTE msdb..sp_maintplan_delete_log null,null,@dt

Open in new window


in a shceduled sql agent job ..
0
 
LVL 1

Author Comment

by:marrowyung
ID: 37850655
let me check
0
 
LVL 1

Author Comment

by:marrowyung
ID: 37879207
how can I check how many days of history I have kept on the MSDB?  I have to use it to verify what I have purged .
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 21

Expert Comment

by:Jason Yousef, MS
ID: 37880637
Well, you can visualize the "run_date" column from the  msdb.dbo.sysjobhistory

or something simple like that...

SELECT min(run_date),  max(run_date) FROM  msdb.dbo.sysjobhistory
0
 
LVL 1

Author Comment

by:marrowyung
ID: 37884835
but it seems that the min(run_date) and  max(run_date) is the same.
0
 
LVL 21

Expert Comment

by:Jason Yousef, MS
ID: 37886919
did you deleted everything?

The query works good in my environment.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question