Create a SQL Server Agent Job to Archive Database Mail Messages and Event Logs
Copies of Database Mail messages and their attachments are retained in msdb tables along with the Database Mail event log. Periodically you might want to reduce the size of the tables and archive messages and events that are no longer needed. The following procedures create a SQL Server Agent job to automate the process.
Before you begin: , Prerequisites, Recommendations, Permissions
To Archive Database Mail messages and logs using : SQL Server Agent
Before You Begin
Prerequisites
The new tables to store the archive data might be located in a special archive database. Alternatively the rows could be exported to a text file.
Top
Recommendations
In your production environment, you might want to add additional error checking and send an e-mail message to operators if the job fails.
Top
Permissions
You must be a member of the sysadmin fixed server role to execute the stored procedures described in this topic.
Top
Overview of the Process
The first procedure creates a job named Archive Database Mail with the following steps.
Copy all messages from the Database Mail tables to a new table named after the previous month in the format DBMailArchive_<year_month>.
Copy the attachments related to the messages copied in the first step, from the Database Mail tables to a new table named after the previous month in the format DBMailArchive_Attachments_<year_month>.
Copy the events from the Database Mail event log that are related to the messages copied in the first step, from the Database Mail tables to a new table named after the previous month in the format DBMailArchive_Log_<year_month>.
Delete the records of the transferred mail items from the Database Mail tables.
Delete the events related to the transferred mail items from the Database Mail event log.
Schedule the job to run periodically.
Top
To create a SQL Server Agent job
In Object Explorer, expand SQL Server Agent, right-click Jobs, and then click New Job.
In the New Job dialog box, in the Name box, type Archive Database Mail.
In the Owner box, confirm that the owner is a member of the sysadmin fixed server role.
In the Category box, click the Database Maintenance.
In the Description box, type Archive Database Mail messages, and then click Steps.
Overview
To create a step to archive the Database Mail messages
On the Steps page, click New.
In the Step name box, type Copy Database Mail Items.
In the Type box, select Transact-SQL script (T-SQL).
In the Database box, select msdb.
In the Command box, type the following statement to create a table named after the previous month, containing rows older than the start of the current month:
DECLARE @LastMonth nvarchar(12); DECLARE @CopyDate nvarchar(20) ; DECLARE @CreateTable nvarchar(250) ; SET @LastMonth = (SELECT CAST(DATEPART(yyyy,GETDATE()) AS CHAR(4)) + '_' + CAST(DATEPART(mm,GETDATE())-1 AS varchar(2))) ; SET @CopyDate = (SELECT CAST(CONVERT(char(8), CURRENT_TIMESTAMP- DATEPART(dd,GETDATE()-1), 112) AS datetime)) SET @CreateTable = 'SELECT * INTO msdb.dbo.[DBMailArchive_' + @LastMonth + '] FROM sysmail_allitems WHERE send_request_date < ''' + @CopyDate +''''; EXEC sp_executesql @CreateTable ;
Click OK to save the step.
Overview
To create a step to archive the Database Mail attachments
On the Steps page, click New.
In the Step name box, type Copy Database Mail Attachments.
In the Type box, select Transact-SQL script (T-SQL).
In the Database box, select msdb.
In the Command box, type the following statement to create an attachments table named after the previous month, containing the attachments that correspond to the messages transferred in the previous step:
DECLARE @LastMonth nvarchar(12); DECLARE @CopyDate nvarchar(20) ; DECLARE @CreateTable nvarchar(250) ; SET @LastMonth = (SELECT CAST(DATEPART(yyyy,GETDATE()) AS CHAR(4)) + '_' + CAST(DATEPART(mm,GETDATE())-1 AS varchar(2))) ; SET @CopyDate = (SELECT CAST(CONVERT(char(8), CURRENT_TIMESTAMP- DATEPART(dd,GETDATE()-1), 112) AS datetime)) SET @CreateTable = 'SELECT * INTO msdb.dbo.[DBMailArchive_Attachments_' + @LastMonth + '] FROM sysmail_attachments WHERE mailitem_id in (SELECT DISTINCT mailitem_id FROM [DBMailArchive_' + @LastMonth + '] )'; EXEC sp_executesql @CreateTable ;
Click OK to save the step.
Overview
To create a step to archive the Database Mail log
On the Steps page, click New.
In the Step name box, type Copy Database Mail Log.
In the Type box, select Transact-SQL script (T-SQL).
In the Database box, select msdb.
In the Command box, type the following statement to create a log table named after the previous month, containing the log entries that correspond to the messages transferred in the earlier step:
DECLARE @LastMonth nvarchar(12); DECLARE @CopyDate nvarchar(20) ; DECLARE @CreateTable nvarchar(250) ; SET @LastMonth = (SELECT CAST(DATEPART(yyyy,GETDATE()) AS CHAR(4)) + '_' + CAST(DATEPART(mm,GETDATE())-1 AS varchar(2))) ; SET @CopyDate = (SELECT CAST(CONVERT(char(8), CURRENT_TIMESTAMP- DATEPART(dd,GETDATE()-1), 112) AS datetime)) SET @CreateTable = 'SELECT * INTO msdb.dbo.[DBMailArchive_Log_' + @LastMonth + '] FROM sysmail_Event_Log WHERE mailitem_id in (SELECT DISTINCT mailitem_id FROM [DBMailArchive_' + @LastMonth + '] )'; EXEC sp_executesql @CreateTable ;
Click OK to save the step.
Overview
To create a step to remove the archived rows from Database Mail
On the Steps page, click New.
In the Step name box, type Remove rows from Database Mail.
In the Type box, select Transact-SQL script (T-SQL).
In the Database box, select msdb.
In the Command box, type the following statement to remove rows older than the current month from the Database Mail tables:
DECLARE @CopyDate nvarchar(20) ; SET @CopyDate = (SELECT CAST(CONVERT(char(8), CURRENT_TIMESTAMP- DATEPART(dd,GETDATE()-1), 112) AS datetime)) ; EXECUTE msdb.dbo.sysmail_delete_mailitems_sp @sent_before = @CopyDate ;
Click OK to save the step.
Overview
To create a step to remove the archived items from Database Mail event log
On the Steps page, click New.
In the Step Name box type Remove rows from Database Mail event log.
In the Type box, select Transact-SQL script (T-SQL).
In the Command box, type the following statement to remove rows older than the current month from the Database Mail event log:
DECLARE @CopyDate nvarchar(20) ; SET @CopyDate = (SELECT CAST(CONVERT(char(8), CURRENT_TIMESTAMP- DATEPART(dd,GETDATE()-1), 112) AS datetime)) ; EXECUTE msdb.dbo.sysmail_delete_log_sp @logged_before = @CopyDate ;
Click OK to save the step.
Overview
To schedule the job to run periodically
In the New Job dialog box, click Schedules.
On the Schedules page, click New.
In the Name box, type Archive Database Mail.
In the Schedule type box, select Recurring.
In the Frequency area, select the options to run the job periodically, for example once every month.
In the Daily frequency area, select Occurs once at <time>.
Verify that the other options are configured as you wish, and then click OK to save the schedule.
Click OK to save the job.
Overview