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.