You are on page 1of 28

Module 8

Automating SQL Server


Management
Module Overview

Automating SQL Server Management


Working with SQL Server Agent
Managing SQL Server Agent Jobs
• Multiserver Management
Lesson 1: Automating SQL Server Management

Benefits of Automating SQL Server Management


Available Options for Automating SQL Server
Management
Overview of SQL Server Agent
• Demonstration: SQL Server Agent
Benefits of Automating SQL Server Management

• Reduce administrative workload by automating


and scheduling regular tasks

• Execute routine tasks reliably and consistently

• Proactive management:
• Monitor performance
• Recognize and respond to potential problems
Available Options for Automating SQL Server
Management

• SQL Server Agent

• Maintenance Plans

• PowerShell

• System Center Operations Manager


Overview of SQL Server Agent

• Runs as a Windows Service


• By default this is set to be manually started

• Jobs can perform a number of tasks


• Schedules can be defined to run one or more
jobs
• Alerts can be used to respond to system events
• Operators can be notified by jobs or alerts
Demonstration: SQL Server Agent

In this demonstration, you will see how to:


• Create a simple job, and a step
• Run a job, and review the output
Lesson 2: Working with SQL Server Agent

Defining Jobs, Job Types, and Job Categories


Creating Job Steps
Scheduling Jobs for Execution
Scripting Jobs
• Demonstration: Scripting Jobs
Defining Jobs, Job Types, and Job Categories

• Jobs consist of a series of steps

• Job step types include:


• Command-line script, batch of commands or
application
• Transact-SQL statement
• PowerShell script
• SSIS and SSAS commands and queries

• Jobs can be assigned to categories


Creating Job Steps
Scheduling Jobs for Execution

• Recurrence:
• One time
• When SQL Server Agent starts
• Whenever the CPU is idle

• One job can have multiple schedules

• Multiple jobs can share one schedule


Scripting Jobs

• Script a database task to a job

• Generate scripts for existing jobs


Demonstration: Scripting Jobs

In this demonstration, you will see how to:


• Script a task to a job
• Generate scripts for existing jobs
Lesson 3: Managing SQL Server Agent Jobs

Viewing Job History


Querying SQL Server Agent-related System Tables
and Views
Troubleshooting Failed Jobs
• Demonstration: Viewing Job History and Resolving
Failed Jobs
Viewing Job History

• Information about job history is written to tables


in the msdb database
• Can be viewed via a Transact-SQL query
• Can be viewed in SSMS by using the Log File Viewer
• Optionally, the information can be written to log files

• SSMS also has the Job Activity Monitor that


shows the current job activity, and the schedules
for active jobs
Querying SQL Server Agent-related System
Tables and Views

• Configuration and history information is stored in


msdb.dbo schema

• Use history tables to automate collection of job


history over several systems

• The dbo.sysjobhistory table contains a row for


each job with a step_id = 0. This contains the
result for the whole job
Troubleshooting Failed Jobs

• SQL Server Agent status:


• Is the service account valid?
• Is the msdb database online?
• Job history:
• Job outcome identifies the last step to execute
• Job step outcome identifies why the step failed
• Job execution:
• Is the job enabled?
• Is the job scheduled?
• Is the schedule enabled?
• Access to dependencies:
• Are all dependent objects available?
Demonstration: Viewing Job History and
Resolving Failed Jobs

In this demonstration, you will see how to:


• Run jobs
• Troubleshoot a failed job
Lesson 4: Multiserver Management

Multiserver Management Concepts


Considerations for Multiserver Management
Demonstration: Configuring Master and Target
Servers
Running Jobs on Target Servers
• Automating Multiserver Maintenance
Multiserver Management Concepts
Considerations for Multiserver Management

• The master server service can impact


performance

• Each target server can link to only one master


server

• Cannot change a target server name while


enlisted

• SQL Server Agent and SQL Server services must


use domain accounts
Demonstration: Configuring Master and Target Servers

In this demonstration, you will see how to:


• Use the Master Server Wizard
• Use Transact-SQL to register target servers
Running Jobs on Target Servers

• Two methods to distribute jobs to target servers


• Using SSMS, choose the target servers on the Target
page
• Using Transact-SQL command: sp_add_jobserver

• If jobs are updated, they need to be pushed to


target servers
• Transact-SQL command: sp_post_msx_operation
Automating Multiserver Maintenance

• Maintenance plans support creating SQL Server


Agent jobs that can run on master and target
servers

• The execution and monitoring is all undertaken


on the master server

• The Maintenance Plan Wizard can create simple


maintenance plans
Lab: Automating SQL Server Management

Exercise 1: Create a SQL Server Agent Job


Exercise 2: Test a Job
Exercise 3: Schedule a Job
• Exercise 4: Configure Master and Target Servers

Logon Information
Virtual machine: 20764B-MIA-SQL
User name: ADVENTUREWORKS\Student
Password: Pa$$w0rd

Estimated Time: 75 minutes


Lab Scenario

You are a database administrator (DBA) at


Adventure Works Cycles, with responsibility for
databases on the MIA-SQL instance of SQL Server.
Routine tasks that must be performed on this
instance have previously been performed
manually, but you now plan to automate these
tasks using SQL Server Agent.
Lab Review

In this lab, you learned how to create SQL Server


Agent jobs, test jobs, schedule jobs and configure
master and target servers.
Module Review and Takeaways

• Best Practice

You might also like