从QueryPlan xml中提取ColumnReference信息

时间:2018-10-16 10:50:04

标签: sql xml tsql

我有一个很大的xml,不过它只是一个查询计划。我需要从中提取列信息。

在该xml中,有一个类似下面的标签,

<ColumnReference Table="[TestTable]" Schema="[dbo]" Database="[Mydatabase]" Column="FirstName" Alias="a"/>

我想提取表格,模式,列信息。 ColumnReference埋在多个不同的标签内。但是无论层次结构如何,我都需要所有ColumnReference标签

从Columnreference中选择表,模式,列

xml的一部分,例如:

<RelOp EstimatedExecutionMode="Row" EstimateRewinds="0" EstimateRebinds="0" Parallel="1" EstimatedTotalSubtreeCost="92.6757" AvgRowSize="349" EstimateCPU="0.0439447" EstimateIO="0" EstimateRows="179.753" LogicalOp="Repartition Streams" PhysicalOp="Parallelism" NodeId="3">
<OutputList>
<ColumnReference Table="[REALESTATECONTRACT2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="ODS_REGIONCODE" Alias="[REC2]"/>
<ColumnReference Table="[REALESTATECONTRACT2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PRIMARYLOCATIONTRIIDTX" Alias="[REC2]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PATHTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTIDSY" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTFLOORTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTBUILDINGTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="NAMETX" Alias="[SP]"/>
<ColumnReference Table="[BUILDING1]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="STATEPROVTX" Alias="[BLD]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="COUNTRYTX" Alias="[BLD2]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="CITYTX" Alias="[BLD2]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="ACTIVESTARTDA" Alias="[BLD2]"/>
</OutputList>
<Parallelism PartitioningType="Hash">
<PartitionColumns>
<ColumnReference Table="[REALESTATECONTRACT2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PRIMARYLOCATIONTRIIDTX" Alias="[REC2]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="NAMETX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PATHTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTIDSY" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTFLOORTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTBUILDINGTX" Alias="[SP]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="CITYTX" Alias="[BLD2]"/>
<ColumnReference Table="[BUILDING1]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="STATEPROVTX" Alias="[BLD]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="COUNTRYTX" Alias="[BLD2]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="ACTIVESTARTDA" Alias="[BLD2]"/>
</PartitionColumns>

1 个答案:

答案 0 :(得分:0)

可能会帮助您:

DECLARE @xml XML=
N'<RelOp EstimatedExecutionMode="Row" EstimateRewinds="0" EstimateRebinds="0" Parallel="1" EstimatedTotalSubtreeCost="92.6757" AvgRowSize="349" EstimateCPU="0.0439447" EstimateIO="0" EstimateRows="179.753" LogicalOp="Repartition Streams" PhysicalOp="Parallelism" NodeId="3">
<OutputList>
<ColumnReference Table="[REALESTATECONTRACT2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="ODS_REGIONCODE" Alias="[REC2]"/>
<ColumnReference Table="[REALESTATECONTRACT2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PRIMARYLOCATIONTRIIDTX" Alias="[REC2]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PATHTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTIDSY" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTFLOORTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTBUILDINGTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="NAMETX" Alias="[SP]"/>
<ColumnReference Table="[BUILDING1]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="STATEPROVTX" Alias="[BLD]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="COUNTRYTX" Alias="[BLD2]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="CITYTX" Alias="[BLD2]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="ACTIVESTARTDA" Alias="[BLD2]"/>
</OutputList>
<Parallelism PartitioningType="Hash">
<PartitionColumns>
<ColumnReference Table="[REALESTATECONTRACT2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PRIMARYLOCATIONTRIIDTX" Alias="[REC2]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="NAMETX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PATHTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTIDSY" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTFLOORTX" Alias="[SP]"/>
<ColumnReference Table="[SPACE]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="PARENTBUILDINGTX" Alias="[SP]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="CITYTX" Alias="[BLD2]"/>
<ColumnReference Table="[BUILDING1]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="STATEPROVTX" Alias="[BLD]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="COUNTRYTX" Alias="[BLD2]"/>
<ColumnReference Table="[BUILDING2]" Schema="[Tririga]" Database="[TRI_INTL_CERT]" Column="ACTIVESTARTDA" Alias="[BLD2]"/>
</PartitionColumns>
</Parallelism>
</RelOp>';

-我使用两个变量,您可以根据需要进行调整

DECLARE @tableName VARCHAR(100)='[BUILDING2]';
DECLARE @ColumnName VARCHAR(100)='COUNTRYTX'

-这将使用Xpath / XQuery来触发//深度搜索<ColumnReference>),但是它将仅返回行,谓词为满足。

SELECT a.query('.')
FROM @xml.nodes(N'//ColumnReference[@Table=sql:variable("@tableName") 
                                    and @Column=sql:variable("@ColumnName")]') A(a);

更新

此查询将返回列中的行:

DECLARE @tableName VARCHAR(100)='[BUILDING2]';
DECLARE @ColumnName VARCHAR(100)='COUNTRYTX'

SELECT a.value('@Table','nvarchar(max)') AS TableName
      ,a.value('@Schema','nvarchar(max)') AS SchemaName
      ,a.value('@Database','nvarchar(max)') AS DatabaseName
      ,a.value('@Column','nvarchar(max)') AS ColumnName
      ,a.value('@Alias','nvarchar(max)') AS Alias
FROM @xml.nodes(N'//ColumnReference[@Table=sql:variable("@tableName") 
                                    and @Column=sql:variable("@ColumnName")]') A(a)