Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
You can delete (drop) a plan guide in SQL Server by using SQL Server Management Studio or Transact-SQL. Using Transact-SQL, you can also delete all of the plan guides in a database.
In This Topic
Before you begin:
To delete a plan guide, using:
Deleting an OBJECT plan guide requires ALTER permission on the object (for example: function, stored procedure) that is referenced by the plan guide. All other plan guides require ALTER DATABASE permission.
Click the plus sign to expand the database in which you want to delete a plan guide, and then click the plus sign to expand the Programmability folder.
Click the plus sign to expand the Plan Guides folder.
Right-click the plan guide you want to delete and select Delete.
In the Delete Object dialog box, ensure that the correct plan guide is selected and then click OK.
In Object Explorer, connect to an instance of Database Engine.
On the Standard bar, click New Query.
Copy and paste the following example into the query window and click Execute.
--Create a procedure on which to define the plan guide.
IF OBJECT_ID(N'Sales.GetSalesOrderByCountry', N'P') IS NOT NULL
DROP PROCEDURE Sales.GetSalesOrderByCountry;
GO
CREATE PROCEDURE Sales.GetSalesOrderByCountry
(@Country nvarchar(60))
AS
BEGIN
SELECT *
FROM Sales.SalesOrderHeader AS h
INNER JOIN Sales.Customer AS c ON h.CustomerID = c.CustomerID
INNER JOIN Sales.SalesTerritory AS t ON c.TerritoryID = t.TerritoryID
WHERE t.CountryRegionCode = @Country;
END
GO
--Create the plan guide.
EXEC sp_create_plan_guide N'Guide3',
N'SELECT *
FROM Sales.SalesOrderHeader AS h
INNER JOIN Sales.Customer AS c ON h.CustomerID = c.CustomerID
INNER JOIN Sales.SalesTerritory AS t ON c.TerritoryID = t.TerritoryID
WHERE t.CountryRegionCode = @Country',
N'OBJECT',
N'Sales.GetSalesOrderByCountry',
NULL,
N'OPTION (OPTIMIZE FOR (@Country = N''US''))';
GO
--Drop the plan guide.
EXEC sp_control_plan_guide N'DROP', N'Guide3';
GO
In Object Explorer, connect to an instance of Database Engine.
On the Standard bar, click New Query.
Copy and paste the following example into the query window and click Execute.
USE AdventureWorks2022;
GO
EXEC sp_control_plan_guide N'DROP ALL';
GO
For more information, see sp_control_plan_guide (Transact-SQL).
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayTraining
Certification
Microsoft Certified: Azure Database Administrator Associate - Certifications
Administer an SQL Server database infrastructure for cloud, on-premises and hybrid relational databases using the Microsoft PaaS relational database offerings.
Documentation
sp_create_plan_guide (Transact-SQL) - SQL Server
sp_create_plan_guide creates a plan guide for associating query hints or actual query plans with queries in a database.
Learn about plan guides, which let you optimize the performance of queries without directly changing the text of the query in SQL Server.
Apply a Fixed Query Plan to a Plan Guide - SQL Server
Learn how to apply a fixed query plan to a plan guide of type OBJECT or SQL in SQL Server. This example creates a plan guide for a simple ad hoc SQL statement.