Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15694

Plan Guide Forced Parameterization For A Query Is Not In Plan Cache

$
0
0

First, sorry for the long post.

I've unsuccessfully attempted to force parameterization on a SQL query using plan guides. Even running the example in BOL doesn't work for me.

Is there someone here that can help?

Thank you.

Below are the details:

= Server = 

Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64)

= Original query  =

SELECT pi.ProductID, SUM(pi.Quantity) AS Total
FROM Production.ProductModel AS pm
    INNER JOIN Production.ProductInventory AS pi
        ON pm.ProductModelID = pi.ProductID
WHERE pi.ProductID = 1
GROUP BY pi.ProductID, pi.Quantity HAVING SUM(pi.Quantity) > 50;
GO

= Plan Guide Setup SQL =

USE AdventureWorks2008R2
GO
DECLARE @query nvarchar(max), @templatetextOUT nvarchar(max), @parametersOUT nvarchar(max)
SET @query = N'SELECT pi.ProductID, SUM(pi.Quantity) AS Total
FROM Production.ProductModel AS pm
    INNER JOIN Production.ProductInventory AS pi
        ON pm.ProductModelID = pi.ProductID
WHERE pi.ProductID = 101
GROUP BY pi.ProductID, pi.Quantity HAVING SUM(pi.Quantity) > 50;'
EXEC sp_get_query_template @querytext = @query, @templatetext = @templatetextOUT OUTPUT, @parametersOUT = @parametersOUT OUTPUT
SELECT @templatetextOUT, @parametersOUT

EXEC sp_create_plan_guide
    @name=N'TemplateGuide1',
    @stmt=@templatetextOUT,
    @type=N'TEMPLATE',
    @module_or_batch=NULL,
    @params=@parametersOUT,
    @hints=N'OPTION(PARAMETERIZATION FORCED)';

= Plan Cache After Clearing Cache And Rerunning Original Query =

cacheobjtype: Compiled Plan

objtype: Adhoc

text: (same as original query)



Viewing all articles
Browse latest Browse all 15694

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>