10

我试图在忽略命名空间的同时查询 XML,因为结果集有多个命名空间。我已经到了 DataSets 节点,但我不知道如何取出多个 DataSourceName/CommandType/CommandText。理想情况下,我想要:

DataSetName   DataSourceName   CommandType      CommandText
SQLDS         SQLDS            StoredProcedure  ReportProc_aaaaa
SQLDS         SQLDS            StoredProcedure  ReportProc_lalala

非常感谢帮助。

DECLARE @xmltable TABLE (myxml XML)
INSERT INTO @xmltable 
SELECT   
'<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <DataSources>
    <DataSource Name="SQLDS">
      <rd:DataSourceID>32e83b35-434d-4808-b685-ada14accd0e7</rd:DataSourceID>
      <DataSourceReference>SQLDS</DataSourceReference>
    </DataSource>
  </DataSources>
  <DataSets>
    <DataSet Name="SQLDS">
      <Query>
        <DataSourceName>SQLDS</DataSourceName>
        <CommandType>StoredProcedure</CommandType>
        <CommandText>ReportProc_ServerPerformanceGroup</CommandText>
      </Query>
    </DataSet>
    <DataSet Name="GroupDetails">
      <Query>
        <DataSourceName>SQLDS</DataSourceName>
        <CommandType>StoredProcedure</CommandType>
        <CommandText>ReportProc_lalala</CommandText>
      </Query>
    </DataSet>
  </DataSets>
</Report>'

SELECT myxml.value('(/*:Report/*:DataSets)[1]','varchar(100)') FROM @xmltable
4

2 回答 2

20

使用nodes() 方法(xml 数据类型)将 yoru XML 分解为行,并使用value() 方法(xml 数据类型)从 XML 中获取特定值。

select T1.N.value('@Name', 'nvarchar(128)') as DataSetName,
       T2.N.value('(*:DataSourceName/text())[1]', 'nvarchar(128)') as DataSourceName,
       T2.N.value('(*:CommandType/text())[1]', 'nvarchar(128)') as CommandType,
       T2.N.value('(*:CommandText/text())[1]', 'nvarchar(max)') as CommandText
from @xmltable as T
  cross apply T.myxml.nodes('/*:Report/*:DataSets/*:DataSet') as T1(N)
  cross apply T1.N.nodes('*:Query') as T2(N)

SQL小提琴

于 2013-06-27T06:17:00.170 回答
0

怎么样(未经测试)....

select
    T.c.value(N'DataSourceName', N'nvarchar(100)') as DataSourceName,
    T.c.value(N'CommandType',N'nvarchar(100)') as CommandType,
    T.c.value(N'CommandText', N'nvarchar(100)') as CommandText
  from
    @myxml.nodes(N'/Report/DataSets/DataSet/Query') T(c)
于 2013-06-27T05:06:34.077 回答