SQL Server执行计划XML到表

时间:2015-11-20 07:09:21

标签: sql-server xml

我从SQL Server获取以下XML:

DECLARE @XML AS XML

SELECT @XML = 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 '%WhenForm%'

我的问题是如何将这个XML推送到一个包含节点/元素/值/的表中,以单独的列为准,以便我可以轻松查询?

有点像这样

;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)

这让我疯狂,因为我可以得到零件,但不是整件事。

<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.2" Build="11.0.5343.0">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementText="select distinct prac_id,&#xD;&#xA;cast(PatientID as varchar(7)) PatientID,&#xD;&#xA;WhenForm,&#xD;&#xA;cast(TermCode as varchar(12)) TermCode,&#xD;&#xA;ValueAsString,&#xD;&#xA;TermUnit,&#xD;&#xA;Uploaded_Time UploadedTime,&#xD;&#xA;rank() over(partition by prac_id, PatientId, WhenForm, TermCode order by Uploaded_Time desc) ranking&#xD;&#xA;from BPAC_PatAfTermValue&#xD;&#xA;&#xD;" StatementId="1" StatementCompId="1" StatementType="SELECT" RetrievedFromCache="true" StatementSubTreeCost="0.0260059" StatementEstRows="1" StatementOptmLevel="FULL" QueryHash="0x537C6E8DB411D4C1" QueryPlanHash="0xEF624A887F7525C5" StatementOptmEarlyAbortReason="GoodEnoughPlanFound">
          <StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" NUMERIC_ROUNDABORT="false" />
          <QueryPlan NonParallelPlanReason="NoParallelPlansInDesktopOrExpressEdition" CachedPlanSize="40" CompileTime="14" CompileCPU="14" CompileMemory="208">
            <Warnings>
              <PlanAffectingConvert ConvertIssue="Cardinality Estimate" Expression="CONVERT(varchar(7),[AAA].[dbo].[BPAC_PatAfTermValue].[PatientId],0)" />
              <PlanAffectingConvert ConvertIssue="Cardinality Estimate" Expression="CONVERT(varchar(12),[AAA].[dbo].[BPAC_PatAfTermValue].[TermCode],0)" />
            </Warnings>
            <MemoryGrantInfo SerialRequiredMemory="640" SerialDesiredMemory="704" />
            <OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="207537" EstimatedPagesCached="51884" EstimatedAvailableDegreeOfParallelism="2" />
            <RelOp NodeId="0" PhysicalOp="Sort" LogicalOp="Distinct Sort" EstimateRows="1" EstimateIO="0.0112613" EstimateCPU="0.000100048" AvgRowSize="48" EstimatedTotalSubtreeCost="0.0260059" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
              <OutputList>
                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="ValueAsString" />
                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermUnit" />
                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                <ColumnReference Column="Expr1004" />
                <ColumnReference Column="Expr1005" />
                <ColumnReference Column="Expr1006" />
              </OutputList>
              <MemoryFractions Input="0.5" Output="1" />
              <Sort Distinct="1">
                <OrderBy>
                  <OrderByColumn Ascending="1">
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                  </OrderByColumn>
                  <OrderByColumn Ascending="1">
                    <ColumnReference Column="Expr1005" />
                  </OrderByColumn>
                  <OrderByColumn Ascending="1">
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                  </OrderByColumn>
                  <OrderByColumn Ascending="1">
                    <ColumnReference Column="Expr1006" />
                  </OrderByColumn>
                  <OrderByColumn Ascending="1">
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="ValueAsString" />
                  </OrderByColumn>
                  <OrderByColumn Ascending="1">
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermUnit" />
                  </OrderByColumn>
                  <OrderByColumn Ascending="1">
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                  </OrderByColumn>
                  <OrderByColumn Ascending="1">
                    <ColumnReference Column="Expr1004" />
                  </OrderByColumn>
                </OrderBy>
                <RelOp NodeId="1" PhysicalOp="Compute Scalar" LogicalOp="Compute Scalar" EstimateRows="1" EstimateIO="0" EstimateCPU="1e-007" AvgRowSize="48" EstimatedTotalSubtreeCost="0.0146446" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                  <OutputList>
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="ValueAsString" />
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermUnit" />
                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                    <ColumnReference Column="Expr1004" />
                    <ColumnReference Column="Expr1005" />
                    <ColumnReference Column="Expr1006" />
                  </OutputList>
                  <ComputeScalar>
                    <DefinedValues>
                      <DefinedValue>
                        <ColumnReference Column="Expr1005" />
                        <ScalarOperator ScalarString="CONVERT(varchar(7),[AAA].[dbo].[BPAC_PatAfTermValue].[PatientId],0)">
                          <Convert DataType="varchar" Length="7" Style="0" Implicit="0">
                            <ScalarOperator>
                              <Identifier>
                                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                              </Identifier>
                            </ScalarOperator>
                          </Convert>
                        </ScalarOperator>
                      </DefinedValue>
                      <DefinedValue>
                        <ColumnReference Column="Expr1006" />
                        <ScalarOperator ScalarString="CONVERT(varchar(12),[AAA].[dbo].[BPAC_PatAfTermValue].[TermCode],0)">
                          <Convert DataType="varchar" Length="12" Style="0" Implicit="0">
                            <ScalarOperator>
                              <Identifier>
                                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                              </Identifier>
                            </ScalarOperator>
                          </Convert>
                        </ScalarOperator>
                      </DefinedValue>
                    </DefinedValues>
                    <RelOp NodeId="2" PhysicalOp="Sequence Project" LogicalOp="Compute Scalar" EstimateRows="1" EstimateIO="0" EstimateCPU="6e-008" AvgRowSize="47" EstimatedTotalSubtreeCost="0.0146445" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                      <OutputList>
                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="ValueAsString" />
                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermUnit" />
                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                        <ColumnReference Column="Expr1004" />
                      </OutputList>
                      <SequenceProject>
                        <DefinedValues>
                          <DefinedValue>
                            <ColumnReference Column="Expr1004" />
                            <ScalarOperator ScalarString="rank">
                              <Sequence FunctionName="rank" />
                            </ScalarOperator>
                          </DefinedValue>
                        </DefinedValues>
                        <RelOp NodeId="3" PhysicalOp="Segment" LogicalOp="Segment" EstimateRows="1" EstimateIO="0" EstimateCPU="2e-008" AvgRowSize="47" EstimatedTotalSubtreeCost="0.0146444" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                          <OutputList>
                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="ValueAsString" />
                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermUnit" />
                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                            <ColumnReference Column="Segment1007" />
                            <ColumnReference Column="Segment1008" />
                          </OutputList>
                          <Segment>
                            <GroupBy>
                              <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                              <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                              <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                              <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                              <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                            </GroupBy>
                            <SegmentColumn>
                              <ColumnReference Column="Segment1008" />
                            </SegmentColumn>
                            <RelOp NodeId="4" PhysicalOp="Segment" LogicalOp="Segment" EstimateRows="1" EstimateIO="0" EstimateCPU="2e-008" AvgRowSize="47" EstimatedTotalSubtreeCost="0.0146444" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                              <OutputList>
                                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="ValueAsString" />
                                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermUnit" />
                                <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                                <ColumnReference Column="Segment1007" />
                              </OutputList>
                              <Segment>
                                <GroupBy>
                                  <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                                  <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                                  <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                                  <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                                </GroupBy>
                                <SegmentColumn>
                                  <ColumnReference Column="Segment1007" />
                                </SegmentColumn>
                                <RelOp NodeId="5" PhysicalOp="Sort" LogicalOp="Sort" EstimateRows="1" EstimateIO="0.0112613" EstimateCPU="0.000100039" AvgRowSize="39" EstimatedTotalSubtreeCost="0.0146444" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                                  <OutputList>
                                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="ValueAsString" />
                                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermUnit" />
                                    <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                                  </OutputList>
                                  <MemoryFractions Input="1" Output="0.5" />
                                  <Sort Distinct="0">
                                    <OrderBy>
                                      <OrderByColumn Ascending="1">
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                                      </OrderByColumn>
                                      <OrderByColumn Ascending="1">
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                                      </OrderByColumn>
                                      <OrderByColumn Ascending="1">
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                                      </OrderByColumn>
                                      <OrderByColumn Ascending="1">
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                                      </OrderByColumn>
                                      <OrderByColumn Ascending="0">
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                                      </OrderByColumn>
                                    </OrderBy>
                                    <RelOp NodeId="6" PhysicalOp="Table Scan" LogicalOp="Table Scan" EstimateRows="1" EstimateIO="0.003125" EstimateCPU="0.0001581" AvgRowSize="39" EstimatedTotalSubtreeCost="0.0032831" TableCardinality="0" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                                      <OutputList>
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="ValueAsString" />
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermUnit" />
                                        <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                                      </OutputList>
                                      <TableScan Ordered="0" ForcedIndex="0" ForceScan="0" NoExpandHint="0">
                                        <DefinedValues>
                                          <DefinedValue>
                                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="Prac_Id" />
                                          </DefinedValue>
                                          <DefinedValue>
                                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="PatientId" />
                                          </DefinedValue>
                                          <DefinedValue>
                                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="WhenForm" />
                                          </DefinedValue>
                                          <DefinedValue>
                                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermCode" />
                                          </DefinedValue>
                                          <DefinedValue>
                                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="ValueAsString" />
                                          </DefinedValue>
                                          <DefinedValue>
                                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="TermUnit" />
                                          </DefinedValue>
                                          <DefinedValue>
                                            <ColumnReference Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" Column="uploaded_time" />
                                          </DefinedValue>
                                        </DefinedValues>
                                        <Object Database="[AAA]" Schema="[dbo]" Table="[BPAC_PatAfTermValue]" IndexKind="Heap" />
                                      </TableScan>
                                    </RelOp>
                                  </Sort>
                                </RelOp>
                              </Segment>
                            </RelOp>
                          </Segment>
                        </RelOp>
                      </SequenceProject>
                    </RelOp>
                  </ComputeScalar>
                </RelOp>
              </Sort>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
        <StmtSimple StatementText="&#xA;DECLARE @XML AS XML&#xD;&#xA;SELECT @XML = qplan.query_plan &#xD;&#xA; FROM sys.dm_exec_query_stats AS qstats &#xD;&#xA;  CROSS APPLY sys.dm_exec_query_plan(qstats.plan_handle) AS qplan &#xD;&#xA;  cross apply sys.dm_exec_sql_text(qstats.plan_handle) as qtext &#xD;&#xA;  where text like '%WhenForm%'&#xD;&#xA;--and query_plan_hash = 0xA15453A5C2D43765&#xD;&#xA;&#xD;" StatementId="2" StatementCompId="2" StatementType="SELECT" RetrievedFromCache="true" StatementSubTreeCost="0.000490071" StatementEstRows="7.47583" StatementOptmLevel="FULL" QueryHash="0xAC3C87210F7D6B19" QueryPlanHash="0xF5B7BD0B0868667A" StatementOptmEarlyAbortReason="GoodEnoughPlanFound">
          <StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" NUMERIC_ROUNDABORT="false" />
          <QueryPlan NonParallelPlanReason="NoParallelPlansInDesktopOrExpressEdition" CachedPlanSize="24" CompileTime="51" CompileCPU="51" CompileMemory="280">
            <MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0" />
            <OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="207537" EstimatedPagesCached="51884" EstimatedAvailableDegreeOfParallelism="2" />
            <RelOp NodeId="0" PhysicalOp="Filter" LogicalOp="Filter" EstimateRows="7.47583" EstimateIO="0" EstimateCPU="3.52e-005" AvgRowSize="4085" EstimatedTotalSubtreeCost="0.000490071" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
              <OutputList>
                <ColumnReference Table="[FNGETQUERYPLAN]" Column="query_plan" />
              </OutputList>
              <Filter StartupExpression="0">
                <RelOp NodeId="1" PhysicalOp="Nested Loops" LogicalOp="Inner Join" EstimateRows="40" EstimateIO="0" EstimateCPU="0.0001672" AvgRowSize="5135" EstimatedTotalSubtreeCost="0.000454871" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                  <OutputList>
                    <ColumnReference Table="[FNGETQUERYPLAN]" Column="query_plan" />
                    <ColumnReference Table="[FNGETSQL]" Column="text" />
                  </OutputList>
                  <NestedLoops Optimized="0">
                    <OuterReferences>
                      <ColumnReference Table="[QUERY_STATS]" Column="plan_handle" />
                    </OuterReferences>
                    <RelOp NodeId="2" PhysicalOp="Nested Loops" LogicalOp="Inner Join" EstimateRows="40" EstimateIO="0" EstimateCPU="0.0001672" AvgRowSize="4119" EstimatedTotalSubtreeCost="0.000247514" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                      <OutputList>
                        <ColumnReference Table="[QUERY_STATS]" Column="plan_handle" />
                        <ColumnReference Table="[FNGETQUERYPLAN]" Column="query_plan" />
                      </OutputList>
                      <NestedLoops Optimized="0">
                        <OuterReferences>
                          <ColumnReference Table="[QUERY_STATS]" Column="plan_handle" />
                        </OuterReferences>
                        <RelOp NodeId="3" PhysicalOp="Table-valued function" LogicalOp="Table-valued function" EstimateRows="40" EstimateIO="0" EstimateCPU="4.0157e-005" AvgRowSize="43" EstimatedTotalSubtreeCost="4.0157e-005" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row">
                          <OutputList>
                            <ColumnReference Table="[QUERY_STATS]" Column="plan_handle" />
                          </OutputList>
                          <TableValuedFunction>
                            <DefinedValues>
                              <DefinedValue>
                                <ColumnReference Table="[QUERY_STATS]" Column="plan_handle" />
                              </DefinedValue>
                            </DefinedValues>
                            <Object Table="[QUERY_STATS]" />
                            <ParameterList>
                              <ScalarOperator ScalarString="(0)">
                                <Const ConstValue="(0)" />
                              </ScalarOperator>
                              <ScalarOperator ScalarString="NULL">
                                <Const ConstValue="NULL" />
                              </ScalarOperator>
                            </ParameterList>
                          </TableValuedFunction>
                        </RelOp>
                        <RelOp NodeId="6" PhysicalOp="Table-valued function" LogicalOp="Table-valued function" EstimateRows="1" EstimateIO="0" EstimateCPU="1.157e-006" AvgRowSize="4085" EstimatedTotalSubtreeCost="4.0157e-005" Parallel="0" EstimateRebinds="36" EstimateRewinds="3" EstimatedExecutionMode="Row">
                          <OutputList>
                            <ColumnReference Table="[FNGETQUERYPLAN]" Column="query_plan" />
                          </OutputList>
                          <TableValuedFunction>
                            <DefinedValues>
                              <DefinedValue>
                                <ColumnReference Table="[FNGETQUERYPLAN]" Column="query_plan" />
                              </DefinedValue>
                            </DefinedValues>
                            <Object Table="[FNGETQUERYPLAN]" />
                            <ParameterList>
                              <ScalarOperator ScalarString="QUERY_STATS.[plan_handle]">
                                <Identifier>
                                  <ColumnReference Table="[QUERY_STATS]" Column="plan_handle" />
                                </Identifier>
                              </ScalarOperator>
                            </ParameterList>
                          </TableValuedFunction>
                        </RelOp>
                      </NestedLoops>
                    </RelOp>
                    <RelOp NodeId="8" PhysicalOp="Table-valued function" LogicalOp="Table-valued function" EstimateRows="1" EstimateIO="0" EstimateCPU="1.157e-006" AvgRowSize="1059" EstimatedTotalSubtreeCost="4.0157e-005" Parallel="0" EstimateRebinds="36" EstimateRewinds="3" EstimatedExecutionMode="Row">
                      <OutputList>
                        <ColumnReference Table="[FNGETSQL]" Column="text" />
                      </OutputList>
                      <TableValuedFunction>
                        <DefinedValues>
                          <DefinedValue>
                            <ColumnReference Table="[FNGETSQL]" Column="text" />
                          </DefinedValue>
                        </DefinedValues>
                        <Object Table="[FNGETSQL]" />
                        <ParameterList>
                          <ScalarOperator ScalarString="QUERY_STATS.[plan_handle]">
                            <Identifier>
                              <ColumnReference Table="[QUERY_STATS]" Column="plan_handle" />
                            </Identifier>
                          </ScalarOperator>
                        </ParameterList>
                      </TableValuedFunction>
                    </RelOp>
                  </NestedLoops>
                </RelOp>
                <Predicate>
                  <ScalarOperator ScalarString="FNGETSQL.[text] like N'%WhenForm%'">
                    <Intrinsic FunctionName="like">
                      <ScalarOperator>
                        <Identifier>
                          <ColumnReference Table="[FNGETSQL]" Column="text" />
                        </Identifier>
                      </ScalarOperator>
                      <ScalarOperator>
                        <Const ConstValue="N'%WhenForm%'" />
                      </ScalarOperator>
                    </Intrinsic>
                  </ScalarOperator>
                </Predicate>
              </Filter>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
        <StmtSimple StatementText="&#xA;select @XML" StatementId="3" StatementCompId="3" StatementType="SELECT WITHOUT QUERY" RetrievedFromCache="true" />
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>

2 个答案:

答案 0 :(得分:1)

假设你有一个这样的表:

CREATE TABLE XmlExecutionPlans 
(
    ID INT IDENTITY(1,1) 
       CONSTRAINT PK_XmlExecutionPlans PRIMARY KEY CLUSTERED, 
    ExecutionPlan XML
)

您应该能够轻松插入此XML:

INSERT INTO dbo.XmlExecutionPlans(ExecutionPlan)
VALUES (@Xml)

答案 1 :(得分:1)

由于这是高度嵌套的数据,因此没有简单的方法可以将它放在平面表中。当然,您可以编写一个包含许多CROSS APPLY Something.nodes('xyz')的长查询,并完成整个过程。

但你可能会尝试一些事情:

DECLARE @DocHandle INT;
EXEC sp_xml_preparedocument @DocHandle OUTPUT, YourXML;
SELECT * FROM OPENXML(@DocHandle,'/');
EXEC sp_xml_removedocument @DocHandle;

这不会创建一个平面表,但是 - 至少 - 一个非常简单的方法来读取&#34;正常&#34;表

一个提示:Attribut值限制为128个字节。如果没有手动更正,您发布的XML就不会被转换......但是 - 如果这已经是XML了,我希望可以在没有转换的情况下使用它...

结果如下:

enter image description here