在SQL Server中的另一个XQuery value()查询中使用XQuery value()查询的结果

时间:2018-03-23 15:48:41

标签: sql-server reporting-services xquery sql-server-2014 xquery-sql

我正在尝试查询xml格式的SSRS .rdl文件。以下是xml相关部分的示例:

<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition">
  <DataSources>
    <DataSource Name="DataSource1">
      <DataSourceReference>DataSourceReference1</DataSourceReference>
    </DataSource>
    <DataSource Name="DataSource2">
      <DataSourceReference>DataSourceReference2</DataSourceReference>
    </DataSource>
  </DataSources>
  <DataSets>
    <DataSet Name="DataSet1">
      <Query>
        <DataSourceName>DataSource1</DataSourceName>
        <CommandText>SELECT a from b</CommandText>
      </Query>
    </DataSet>
    <DataSet Name="DataSet2">
      <Query>
        <DataSourceName>DataSource2</DataSourceName>
        <CommandText>SELECT c from d</CommandText>
      </Query>
    </DataSet>
  </DataSets>
...

到目前为止,这是我的SQL查询:

IF OBJECT_ID('tempdb..#catalogtemp') IS NOT NULL DROP TABLE #catalogtemp 
GO

SELECT Path, CONVERT(XML, CONVERT(VARBINARY(MAX), Content)) XmlColumn
INTO #catalogtemp
FROM Catalog WHERE Type=2;

;WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition' as rdl10)

SELECT Path as ReportPath,
T1.dataset.value('./@Name','nvarchar(max)') DatasetName,
T1.dataset.value('(.//rdl10:CommandText)[1]','nvarchar(max)') as DatasetQuery,
T1.dataset.value('(.//rdl10:DataSourceName)[1]','nvarchar(max)') as DataSourceName
FROM #catalogtemp
CROSS APPLY xmlColumn.nodes('//rdl10:DataSet') T1(dataset)

对于上面的示例xml,它将返回:

ReportPath        DatasetName   DatasetQuery      DataSourceName
/path/to/report   DataSet1      SELECT a from b   DataSource1
/path/to/report   DataSet2      SELECT c from d   DataSource2

我想要做的是添加另一列,查找DataSourceReference节点的DataSource值,Name属性与{DataSourceName的{​​{1}}值相匹配1}}。所以查询的结果如下所示:

DataSet

我知道以下XQuery将返回ReportPath DatasetName DatasetQuery DataSourceName DataSourceReference /path/to/report DataSet1 SELECT a from b DataSource1 DataSourceReference1 /path/to/report DataSet2 SELECT c from d DataSource2 DataSourceReference2 属性为'DataSource1'的DataSourceReference节点的DataSource值:

Name

但是如何编写查询以每次查找正确的T1.dataset.value('(../..//rdl10:DataSource[@Name="DataSource1"]//rdl10:DataSourceReference)[1]','nvarchar(max)')

1 个答案:

答案 0 :(得分:1)

恕我直言,因为For i = 2 To NRScount75 If Range("O" & i) >= 0 And Range("O" & i) <= 10 Then Range("P" & i) = "Within SLA" 'values between 0 to 10 ElseIf Range("O" & i) >= 11 Then Range("P" & i) = "Exceed SLA" 'values greater than 10 Else Range("P" & i) = "Not submitted" 'for negative values End If Next i 标记中没有<DataSources>标记,反之亦然,你需要一个子查询:

<DataSets>
ReportPath | DatasetName | DatasetQuery    | DataSourceName | DSR                 
:--------- | :---------- | :-------------- | :------------- | :-------------------
my report  | DataSet1    | SELECT a from b | DataSource1    | DataSourceReference1
my report  | DataSet2    | SELECT c from d | DataSource2    | DataSourceReference2

dbfiddle here