Solved

The housekeeping task of MSDB

Posted on 2012-04-10
6
700 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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