My ultimate goal is automatic extraction of all referenced columns from a cached execution plan. This will help us keep a track of all the columns used by our scheduled set of SSRS reports.
The XML data of interest looks like this:
<ColumnReference Database="[AdventureWorksDW2012]" Schema="[dbo]" Table="[DimCustomer]" Alias="[dC]" Column="HouseOwnerFlag" />
and I would like to store Database, Schema, Table, Alias and Column values in a table.
However, for a proof of concept, I have taken a simple query and copied the following part of the full execution plan into a TSQL code below:
DECLARE @myDoc xml;
SET @myDoc = '<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.2" Build="11.0.3381.0" TEST="1">
<BatchSequence>
<Batch>
<Statements>
<StmtSimple StatementText="SELECT ... 
" StatementId="1" StatementCompId="1" ThereIsMoreHere="..." >
<StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="true" ThereIsMoreHere="..." />
</StmtSimple>
</Statements>
</Batch>
</BatchSequence>
</ShowPlanXML>';
SELECT StatementId = @myDoc.value('(/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple/@StatementId)[1]', 'int');
SELECT StatementId = @myDoc.value('(/ShowPlanXML/@TEST)[1]', 'int');
Both SELECT statements are returning NULL. What is wrong here? I feel I am slowly going blind. This is executed against an SQL Server 2012 SP1 Developers edition.
The ultimate goal of selecting all the columns used in an SQL Server's execution plan solved:
USE AdventureWorksDW2012
DBCC FREEPROCCACHE
SELECT dC.Gender, dc.HouseOwnerFlag,
SUM(fIS.SalesAmount) AS SalesAmount
FROM
dbo.DimCustomer dC INNER JOIN
dbo.FactInternetSales fIS ON fIS.CustomerKey = dC.CustomerKey
GROUP BY dC.Gender, dc.HouseOwnerFlag
ORDER BY dC.Gender, dc.HouseOwnerFlag
/*
query_hash query_plan_hash
0x752B3F80E2DB426A 0xA15453A5C2D43765
*/
DECLARE @MyQ AS XML;
-- SELECT qstats.query_hash, query_plan_hash, qplan.query_plan AS [Query Plan],qtext.text
SELECT @MyQ = qplan.query_plan
FROM sys.dm_exec_query_stats AS qstats
CROSS APPLY sys.dm_exec_query_plan(qstats.plan_handle) AS qplan
cross apply sys.dm_exec_sql_text(qstats.plan_handle) as qtext
where text like '% fIS %'
and query_plan_hash = 0xA15453A5C2D43765
SeLeCt @MyQ
;WITH xmlnamespaces (default 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT DISTINCT
[Database] = x.value('(@Database)[1]', 'varchar(128)'),
[Schema] = x.value('(@Schema)[1]', 'varchar(128)'),
[Table] = x.value('(@Table)[1]', 'varchar(128)'),
[Alias] = x.value('(@Alias)[1]', 'varchar(128)'),
[Column] = x.value('(@Column)[1]', 'varchar(128)')
FROM @MyQ.nodes('//ColumnReference') x1(x)
Leads to the following output:
Database Schema Table Alias Column
------------------------- ------ ---------------- ----- ----------------
NULL NULL NULL NULL Expr1004
[AdventureWorksDW2012] [dbo] [DimCustomer] [dC] CustomerKey
[AdventureWorksDW2012] [dbo] [DimCustomer] [dC] Gender
[AdventureWorksDW2012] [dbo] [DimCustomer] [dC] HouseOwnerFlag
[AdventureWorksDW2012] [dbo] [FactInternetSal [fIS] CustomerKey
[AdventureWorksDW2012] [dbo] [FactInternetSal [fIS] SalesAmount
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With