T-SQL从RDL数据中选择所有XML节点作为行

| 我正在尝试从包含XML列的表中进行SELECT。我想获取特定的节点,并为每个节点创建一行。 XML是直接从Reporting Services数据库获得的,并且包含RDL(报告)结构。我的目标是显示每个报告的所有“ 0”值。 “ 1”个节点的位置是不可预测的(它可以是XML结构中某个元素的一部分)。 以下是当前代码,但是由于某些原因,id无法正常工作:
IF object_id(\'tempdb..#c\') IS NOT NULL
   DROP TABLE #c

select top 50
     path as reportpath
    ,name as reportname
    ,convert(xml, convert(varbinary(max), content)) as reportxml
into
    #c  
from 
    reportserver.dbo.catalog
where 
    content is not null
order by creationdate desc

-----------------------------------------
DECLARE @x XML
SELECT @x = 
( SELECT 
     [reportpath]
    ,[reportname]
    ,[reportxml].query(\'
            for $a in //../Textbox
            return ‹Textbox
            valueX=\"{$a/Value}\"
            /›
        \')
  FROM #c AS reports 
  FOR XML AUTO
)
select @x
-----------------------------------------
SELECT [reportpath]    = T.Item.value(\'../@reportpath\', \'nvarchar(max)\'),
       [reportname]    = T.Item.value(\'../@reportname\', \'nvarchar(max)\'),
       value     = T.Item.value(\'@value\' , \'nvarchar(max)\')
FROM   @x.nodes(\'//reports/Textbox\') AS T(Item)
下面的示例显示包含\“ Value \”的示例\“ Textbox \”:
          ‹RowGrouping›
            ‹Width›2.53968cm‹/Width›
            ‹DynamicRows›
              ‹Grouping Name=\"matrix1_OperationalWeek2\"›
                ‹GroupExpressions›
                  ‹GroupExpression›=Fields!OperationalWeek.Value‹/GroupExpression›
                ‹/GroupExpressions›
              ‹/Grouping›
              ‹ReportItems›
                ‹Textbox Name=\"textbox35\"›
                  ‹rd:DefaultName›textbox35‹/rd:DefaultName›
                  ‹Style›
                    ‹BackgroundColor›White‹/BackgroundColor›
                    ‹PaddingLeft›2pt‹/PaddingLeft›
                    ‹PaddingRight›2pt‹/PaddingRight›
                    ‹PaddingTop›2pt‹/PaddingTop›
                    ‹PaddingBottom›2pt‹/PaddingBottom›
                  ‹/Style›
                  ‹ZIndex›8‹/ZIndex›
                  ‹Value›=Fields!OperationalWeek.Value‹/Value›
                ‹/Textbox›
              ‹/ReportItems›
            ‹/DynamicRows›
          ‹/RowGrouping›
PS:我在stackoverflow代码格式化方面遇到了一些麻烦,因此我用‹和›替换了<和>标记。对于那个很抱歉。     
已邀请:
基于Bret的博客([http://blogs.netconnex.com/2011/05/extracting-ssrs-report-rdl-xml-from.html][1]) 并添加名称空间可以为您带来结果...我希望我可以声称自己理解得足够好来解释,但我主要是通过\“绊脚\”来找到自己的方式。 -================================================
;WITH XMLNAMESPACES (
DEFAULT \'http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition\',
\'http://schemas.microsoft.com/SQLServer/reporting/reportdesigner\' AS rd --ReportDefinition
)
select top 50 
     c.Path as reportpath 
    --, c.name as reportname 
    ,t.value(\'@Name\',\'VARCHAR(100)\') as TextboxName
    ,t.value(\'data(Paragraphs/Paragraph/TextRuns/TextRun/Value)[1]\', \'varchar(max)\') as value 
from  
    reportserver.dbo.catalog c
cross apply 
    (select convert(xml, convert(varbinary(max), content))) as R(reportxml) 
cross apply 
--Get all the Query elements (The \"*:\" ignores any xml namespaces) 
    r.reportxml.nodes(\'//*:Textbox\') n(t)        
where  
    content is not null 
    and c.Type = 2 -- Reports
order by creationdate desc 
    
这个简单的XQuery:
for $a in //Textbox             
 return 
   <Textbox             
      valueX=\"{$a/Value}\"             
   /> 
当应用于提供的XML文档时(添加名称空间定义以使其格式正确):
<RowGrouping xmlns:rd=\"rd\">
  <Width>2.53968cm</Width>
  <DynamicRows>
    <Grouping Name=\"matrix1_OperationalWeek2\">
      <GroupExpressions>
        <GroupExpression>=Fields!OperationalWeek.Value</GroupExpression>
      </GroupExpressions>
    </Grouping>
    <ReportItems>
      <Textbox Name=\"textbox35\">
        <rd:DefaultName>textbox35</rd:DefaultName>
        <Style>
          <BackgroundColor>White</BackgroundColor>
          <PaddingLeft>2pt</PaddingLeft>
          <PaddingRight>2pt</PaddingRight>
          <PaddingTop>2pt</PaddingTop>
          <PaddingBottom>2pt</PaddingBottom>
        </Style>
        <ZIndex>8</ZIndex>
        <Value>=Fields!OperationalWeek.Value</Value>
      </Textbox>
    </ReportItems>
  </DynamicRows>
</RowGrouping>
产生想要的正确结果:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Textbox valueX=\"=Fields!OperationalWeek.Value\"/>
因此,如果无法获得结果,则问题出在其他方面,而不是XQuery代码中。     
我无法测试这是否可行,但它应该可以执行您想要的操作。
select top 50
     path as reportpath
    ,name as reportname
    ,n.t.value(\'Value[1]\', \'varchar(max)\') as value
from 
    reportserver.dbo.catalog
cross apply
    (select convert(xml, convert(varbinary(max), content))) as c(reportxml)
cross apply
    c.reportxml.nodes(\'//Textbox\') n(t)       
where 
    content is not null
order by creationdate desc
    

要回复问题请先登录注册