Vous êtes sur la page 1sur 22

Automating Routine

Maintenance
Topics
• Database Mail
• SQL Server Agent
• Maintenance Plans
Database Mail
• The first thing you should do when automating maintenance tasks is
to set up Database Mail.
• This will allow you to receive automatic notifications for things like
job failures and alerts.
• After all, what good is automating tasks if you constantly have to go
out to each server and make sure everything looks okay?
Database Mail Configuration Wizard
Configuring Database Mail Using T-SQL
--MAKE SURE TO STOP SQL SERVER AGENT BEFORE @profile_name = 'DBA Mail Profile',
RUNNING THIS SCRIPT! @description = 'Profile used by the DBA to send email.'
USE msdb --Add the account
GO EXECUTE msdb.dbo.sysmail_add_account_sp
--Enable Database Mail @account_name = 'DBA Mail Account',
sp_configure 'show advanced options', 1; @description = 'Profile used by the DBA to send email.',
GO @email_address = 'DBA@somecompany.com',
RECONFIGURE; @display_name = (Select @@ServerName),
GO @mailserver_name = 'KEN-PC'
sp_configure 'Database Mail XPs', 1; --Associate the account with the profile
GO EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
RECONFIGURE @profile_name = 'DBA Mail Profile',
GO @account_name = 'DBA Mail Account',
--Enable Service Broker @sequence_number = 1
ALTER DATABASE msdb SET ENABLE_BROKER Print 'Don't Forget To Restart SQL Server Agent!'
--Add the profile
EXECUTE msdb.dbo.sysmail_add_profile_sp
Database Mail Cleanup Procedures
• You can use the sysmail_delete_mailitems_sp stored procedure to
delete email messages older than a specified date or all emails with a
certain status. If you execute the sysmail_ delete_mailitems_sp stored
procedure without supplying any parameters, all email messages will
be deleted.
sysmail_delete_mailitems_sp
[[ @sent_before =] 'sent_before'] -- '1/1/2009'
[ ,[ @sent_status =] 'sent_status'] -- sent, unsent, retrying, failed

sysmail_delete_log_sp
[[@logged_before =] 'logged_before'] --'1/1/2009'
[,[ @event_type =] 'event_type'] --success, warning, error, informational
SQL Server Agent
• SQL Server Agent is at the heart of automating maintenance tasks.
• Operators
• Enabling SQL Server Agent Notifications
• Alerts
• Jobs
• Proxies
Operators
Enabling SQL Server Agent Notifications
• You have to enable the alert
system in SQL Server Agent
before you can start receiving
notifications.
• Once you have configured
Database Mail and added an
operator, the next thing you
should do is enable the alert
system and designate a fail-safe
operator.
Alerts
• A SQL Server Agent alert is an
automatic response to a
predefined event.
• You can configure SQL ServerAgent
alerts to fire in response to SQL
Server events, SQL Server
performance counters, and
Windows Management
Instrumentation (WMI) events.
• Once an event has caused an alert
to fire, you can respond to the alert
by notifying operators of the event
or even running a job after the
event has occurred.
Alerts
Code to Create an Alert for Severity 19
Messages
USE msdb SELECT *
GO FROM sys.messages
EXEC msdb.dbo.sp_add_alert WHERE language_id = 1033 AND
@name=N'Fatal Error in Resource',
is_event_logged = 1 AND
@message_id=0,
severity < 19
@severity=19,
@enabled=1,
@delay_between_responses=0,
@include_event_description_in=1,
@notification_message=
N'This is a Fatal Alert. Please review immediately.'
GO
EXEC msdb.dbo.sp_add_notification
@alert_name=N'Fatal Error in Resource',
@operator_name=N'DBA Support',
@notification_method = 1
GO
Proxies
• You can create a proxy account
that allows SQL Server Agent to
execute a job step that runs
under the credentials of a
specified Windows user account.
• In order to create a proxy
account, you must first create a
credential that maps to a
Windows user account.
Maintenance Plans
• The Maintenance Plan Wizard provides a simple interface for you to
define and schedule tasks that perform routine maintenance.
• Right-click the Maintenance Plans folder located under the
Management node and select Maintenance Plan Wizard from the
context menu.
• Open your LAPTOP and Practice it…. 
Assignments
• ENJOY 
Thank You

Vous aimerez peut-être aussi