---
title: "Create a Maintenance Plan | Microsoft Docs"
ms.custom: ""
ms.date: "08/01/2016"
ms.prod: sql
ms.prod_service: "database-engine"
ms.reviewer: ""
ms.technology: supportability
ms.topic: conceptual
helpviewer_keywords:
- "maintenance plans [SQL Server], creating"
ms.assetid: a945cb65-ba7a-42f4-bbd9-6ec675745523
author: MikeRayMSFT
ms.author: mikeray
---
# Create a Maintenance Plan
[!INCLUDE[appliesto-ss-xxxx-xxxx-xxx-md](../../includes/appliesto-ss-xxxx-xxxx-xxx-md.md)]
This topic describes how to create a single server or multiserver maintenance plan in [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)]. Using [!INCLUDE[ssManStudio](../../includes/ssmanstudio-md.md)], you can create these maintenance plans in one of two ways: by either using the Maintenance Plan Wizard or the design surface. The Wizard is best for creating basic maintenance plans, while creating a plan using the design surface allows you to utilize enhanced workflow.
**In This Topic**
- **Before you begin:**
[Limitations and Restrictions](#Restrictions)
[Prerequisite](#Prerequisite)
[Security](#Security)
- **To create a maintenance plan, using:**
[SQL Server Management Studio](#SSMSProcedure)
[Transact-SQL](#TsqlProcedure)
## Before You Begin
### Limitations and Restrictions
To create a multiserver maintenance plan, a multiserver environment containing one master server and one or more target servers must be configured. Multiserver maintenance plans must be created and maintained on the master server. These plans can be viewed, but not maintained, on target servers.
### Prerequisite
The [Agent XPs Server Configuration Option](../../database-engine/configure-windows/agent-xps-server-configuration-option.md) must be enabled.
### Security
#### Permissions
To create or manage Maintenance Plans, you must be a member of the **sysadmin** fixed server role.
## Using SQL Server Management Studio
#### To create a maintenance plan using the Maintenance Plan Wizard
1. In Object Explorer, click the plus sign to expand the server where you want to create a maintenance plan.
2. Click the plus sign to expand the **Management** folder.
3. Right-click the **Maintenance Plans** folder and select **Maintenance Plan Wizard**.
4. Follow the steps of the wizard to create a maintenance plan. For more information, see [Use the Maintenance Plan Wizard](../../relational-databases/maintenance-plans/use-the-maintenance-plan-wizard.md).
#### To create a maintenance plan using the design surface
1. In Object Explorer, click the plus sign to expand the server where you want to create a maintenance plan.
2. Click the plus sign to expand the **Management** folder.
3. Right-click the **Maintenance Plans** folder and select **New Maintenance Plan**.
4. Create a maintenance plan following the steps in [Create a Maintenance Plan (Maintenance Plan Design Surface)](../../relational-databases/maintenance-plans/create-a-maintenance-plan-maintenance-plan-design-surface.md).
## Using Transact-SQL
#### To create a maintenance plan
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
2. On the Standard bar, click **New Query**.
3. Copy and paste the following example into the query window and click **Execute**.
```
USE msdb;
GO
-- Adds a new job, executed by the SQL Server Agent service, called "HistoryCleanupTask_1".
EXEC dbo.sp_add_job
@job_name = N'HistoryCleanupTask_1',
@enabled = 1,
@description = N'Clean up old task history' ;
GO
-- Adds a job step for reorganizing all of the indexes in the HumanResources.Employee table to the HistoryCleanupTask_1 job.
EXEC dbo.sp_add_jobstep
@job_name = N'HistoryCleanupTask_1',
@step_name = N'Reorganize all indexes on HumanResources.Employee table',
@subsystem = N'TSQL',
@command = N'USE AdventureWorks2012
GO
ALTER INDEX AK_Employee_LoginID ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON )
GO
USE AdventureWorks2012
GO
ALTER INDEX AK_Employee_NationalIDNumber ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON )
GO
USE AdventureWorks2012
GO
ALTER INDEX AK_Employee_rowguid ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON )
GO
USE AdventureWorks2012
GO
ALTER INDEX IX_Employee_OrganizationLevel_OrganizationNode ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON )
GO
USE AdventureWorks2012
GO
ALTER INDEX IX_Employee_OrganizationNode ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON )
GO
USE AdventureWorks2012
GO
ALTER INDEX PK_Employee_BusinessEntityID ON HumanResources.Employee REORGANIZE WITH ( LOB_COMPACTION = ON )
GO
',
@retry_attempts = 5,
@retry_interval = 5 ;
GO
-- Creates a schedule named RunOnce that executes every day when the time on the server is 23:00.
EXEC dbo.sp_add_schedule
@schedule_name = N'RunOnce',
@freq_type = 4,
@freq_interval = 1,
@active_start_time = 233000 ;
GO
-- Attaches the RunOnce schedule to the job HistoryCleanupTask_1.
EXEC sp_attach_schedule
@job_name = N'HistoryCleanupTask_1',
@schedule_name = N'RunOnce' ;
GO
```
For more information, see:
- [sp_add_job (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-add-job-transact-sql.md)
- [sp_add_jobstep (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-add-jobstep-transact-sql.md)
- [sp_add_schedule (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-add-schedule-transact-sql.md)
- [sp_attach_schedule (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-attach-schedule-transact-sql.md)