Skip to content

Instantly share code, notes, and snippets.

@peschkaj
Created March 24, 2014 01:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peschkaj/9732614 to your computer and use it in GitHub Desktop.
Save peschkaj/9732614 to your computer and use it in GitHub Desktop.
This will match SQL Server statements to the appropriate portion of the execution plan based on the query_plan_hash
SELECT QueryPlanHash,
CASE WHEN QueryType LIKE '%Stored Procedure%' THEN
QueryPlan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";
sum(//p:StmtSimple/@StatementSubTreeCost)', 'float')
ELSE
QueryPlan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";
sum(//p:StmtSimple[xs:hexBinary(substring(@QueryPlanHash, 3)) = xs:hexBinary(sql:column("QueryPlanHash"))]/@StatementSubTreeCost)', 'float')
END AS cost, *
FROM #procs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment