1. 程式人生 > >SSRS 如何根據查詢語句Query找到使用該查詢的報表Report

SSRS 如何根據查詢語句Query找到使用該查詢的報表Report

set mman emp parent query microsoft mode sam -type

生成環境中,經常會捕獲到一些消耗CPU和內存資源較多的Query,有一些來自某個APP,有一些來Client,還有一些來自報表服務器。通常報表服務器連接過來的都是通過配置好的共享DataSource, 所以很難判斷是誰。 下面介紹一下我是如何根據Query快速找到Report Owner:

第一步: 將SSRS服務器所有Report的定義信息取出,並轉換成可讀的XML類型:

SELECT
  [Path]
  ,CASE [Type]
  WHEN 2 THEN ‘Report‘
  WHEN 5 THEN ‘Data Source‘
  END AS TypeName


  ,CAST(CAST(content AS varbinary(max)) AS xml) as command
  , [Description] into #temp1
  FROM PBIReportServer.dbo.[Catalog] CTG
 WHERE
  [Type] IN (2, 5)

第二步:將XML字段中Dataset中CommandText取出:


  SELECT *, command.value(‘
  declare namespace ns="http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition";


  (/ns:Report/ns:DataSets/ns:DataSet/ns:Query/ns:CommandText)[1] ‘, ‘varchar(max)‘) as Result into #temp2
  FROM #temp1

  SELECT *
  from #temp2
  where Result like ‘%query statement%‘

第三步:根據上面找到的Report Path,找到 Report Owner:

SELECT
ItemID -- Unique Identifier
, [Path] --Path including object name


, [Name] --Just the objectd name
, ParentID --The ItemID of the folder in which it resides
, CASE [Type] --Type, an int which can be converted using this case statement.
WHEN 1 THEN ‘Folder‘
WHEN 2 THEN ‘Report‘
WHEN 3 THEN ‘File‘
WHEN 4 THEN ‘Linked Report‘
WHEN 5 THEN ‘Data Source‘
WHEN 6 THEN ‘Report Model - Rare‘
WHEN 7 THEN ‘Report Part - Rare‘
WHEN 8 THEN ‘Shared Data Set - Rare‘
WHEN 9 THEN ‘Image‘
ELSE CAST(Type as varchar(100))
END AS TypeName
--, content
, LinkSourceID --If a linked report then this is the ItemID of the actual report.
, [Description] --This is the same information as can be found in the GUI
, [Hidden] --Is the object hidden on the screen or not
, CreatedBy.UserName CreatedBy
, CreationDate
, ModifiedBy.UserName ModifiedBy

FROM
[PBIReportServer].dbo.[Catalog] CTG
INNER JOIN
[PBIReportServer].dbo.Users CreatedBy ON CTG.CreatedByID = CreatedBy.UserID
INNER JOIN
[PBIReportServer].dbo.Users ModifiedBy ON CTG.ModifiedByID = ModifiedBy.UserID
where path in (
‘/Contoso/Report1‘,
‘/Contoso/Report2‘)

SSRS 如何根據查詢語句Query找到使用該查詢的報表Report