22. Automating Maintenance Routines

SQL Server Agent is a service that provides the ability to create automated routines with decision-based logic and schedule to run them one time only, on a recurring basis, when SQL Server Agent starts, or when a CPU idle condition occurs.

Schedules:

Operator - individual or team who receives notification of job status or alert notified via e-mail, pager, and NET SEND. Pager and NET SEND are deprecated. Need to configure DB mail.

Jobs

Alerts repond to events

msdb.dbo.sysalerts_performance-counters_view

MSDB:

sysadmin gets all of these roles. Not all SQL Agent activities are covered by these roles.

T-SQL run as user (no proxy)

Credential -> proxy account -> schedule operator used to receive e-mail

EXEC sp_configure 'show advanced options',1;
GO
RECONFIGURE
GO
EXEC sp_configure 'Database Mail XPs',1;
GO
RECONFIGURE
GO

Can have a default profile which sends e-mail to one or more operators

After configuring DB mail, create operator who receives e-mails in event of job failures. Then create job.

sp_start_job @job_name=N'...';

sp_start_job @job_name=N'...' @step_name=N'...';

Multiserver jobs:

Use regedit to set AllowDownloadedJobsToMatchProxyName MsxEncryotChannelOptions registry key:

connect server\instance
CREATE CREDENTIAL ...;
EXEC msdb.dbo.sp_add_proxy ...;
EXEC msdb.dbo.sp_grant_proxy_to_subsystem ...;

EXEC sp_delete_jobserver
EXEC sp_add_jobserver

Force poll TSX -> MSX to distributed list for latest jobs

SQL Server Agent is a scheduling engine for SQL Server that allows you to create powerful maintenance jobs, whith decision based logic on a variety of schedules. A job is a container for the tasks that should be performed, and each of these tasks is known as a step. Job step can have different accounts/subsystems.

Good security: proxy accounts -> credentials -> AD principal