0

我正在使用 SQL Server 2014 和 Microsoft 开箱即用的 ReportServer 数据库。由于这个错误,我一直在创建一个将拉订阅优先级的脚本。

我使用 SQL、XQuery 和 XPath 编写了以下查询。

;WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition','http://schemas.microsoft.com/SQLServer/reporting/reportdesigner' AS rd )
,s
as (SELECT  
      s.Report_OID
     ,s.SubscriptionID
     ,s.OwnerID
     ,s.Description 
     ,s.LastStatus
     ,s.EventType
     ,CONVERT(XML,CONVERT(VARCHAR(MAX),s.ExtensionSettings)) AS reportXML
    FROM ReportServer.dbo.Subscriptions as s
    WHERE s.ExtensionSettings is not null
   )
--SELECT s.reportxml.value('/ParameterValues[1]','varchar(150)') FROM s

SELECT 
 con.Report_OID
,con.test
FROM (SELECT 
       s.Report_OID
      ,dsn.value('Name[1]','varchar(150)') as test
      FROM s
      cross apply reportxml.nodes('/ParameterValues/ParameterValue')  as r(dsn)
     ) con

我还有 2 个粘贴在下面的示例 xml 文档。

   <ParameterValues>
      <ParameterValue>
        <Name>FILENAME</Name>
        <Field>subscriber_domestic_id</Field>
      </ParameterValue>
      <ParameterValue>
        <Name>PATH</Name>
        <Field>folder</Field>
      </ParameterValue>
      <ParameterValue>
        <Name>RENDER_FORMAT</Name>
        <Value>PDF</Value>
      </ParameterValue>
      <ParameterValue>
        <Name>WRITEMODE</Name>
        <Value>OverWrite</Value>
      </ParameterValue>
      <ParameterValue>
        <Name>FILEEXTN</Name>
        <Value>True</Value>
      </ParameterValue>
      <ParameterValue>
        <Name>USERNAME</Name>
        <Field>login</Field>
      </ParameterValue>
      <ParameterValue>
        <Name>PASSWORD</Name>
        <Field>mypasswordstackOF</Field>
      </ParameterValue>
    </ParameterValues>

The second document here.

<ParameterValues>
  <ParameterValue>
    <Name>TO</Name>
    <Value>jonjones@email.com;billiejean@email.com;prettyfloyd@email.com</Value>
  </ParameterValue>
  <ParameterValue>
    <Name>CC</Name>
    <Value>a@email.com;g@email.com.com;</Value>
  </ParameterValue>
  <ParameterValue>
    <Name>IncludeReport</Name>
    <Value>True</Value>
  </ParameterValue>
  <ParameterValue>
    <Name>RenderFormat</Name>
    <Value>PDF</Value>
  </ParameterValue>
  <ParameterValue>
    <Name>Subject</Name>
    <Value>@ReportName was executed at @ExecutionTime</Value>
  </ParameterValue>
  <ParameterValue>
    <Name>IncludeLink</Name>
    <Value>True</Value>
  </ParameterValue>
  <ParameterValue>
    <Name>Priority</Name>
    <Value>NORMAL</Value>
  </ParameterValue>
</ParameterValues>

从这里您可以看到并非所有文档都有优先级。我当前的查询没有出错,但没有返回任何内容。我相信我正确使用了 nodes() 函数。关于如何正确处理这个问题的任何想法?

4

1 回答 1

0

为了将来的目的,如果有人希望完成相同类型的提取,我将在下面粘贴我的代码:

;WITH
s
as (SELECT  
      s.Report_OID
     ,s.SubscriptionID
     ,s.OwnerID
     ,s.Description 
     ,s.LastStatus
     ,s.EventType
     ,CONVERT(XML,CONVERT(VARCHAR(MAX),s.ExtensionSettings)) AS reportXML
    FROM ReportServer.dbo.Subscriptions as s (NOLOCK)
    WHERE s.ExtensionSettings is not null
   )

SELECT 
 con.Report_OID
,con.Node
,con.PriorityLevel
INTO #Temp
FROM (SELECT 
       s.Report_OID
      ,dsn.value('Name[1]','varchar(150)') as Node
      ,dsn.value('Value[1]','varchar(150)') as PriorityLevel
      FROM s
      cross apply reportxml.nodes('/ParameterValues/ParameterValue')  as r(dsn)
     ) con

SELECT 
 t.Report_OID
,c.Name as 'ReportName'
,t.Node
,t.PriorityLevel
,c.Path
FROM #Temp t
    join ReportServer.dbo.Catalog c (NOLOCK)
        on t.Report_OID = c.ItemID 
WHERE 1=1 
and Node = 'Priority'

享受!

于 2016-09-09T18:44:57.587 回答