Sunday, October 2, 2011

When was the last time a report ran in MSRM?

I was recently asked if it was possible to determine the last time a MSCRM report was run. To get this information you will need read access to the report server db and read access to the Reports table of your CRM instance. You can then run the this query
--NOTE!!! This query is designed to run on SSRS 2008 R2
--Replace #REPORT SERVER DB# with the name of the your SSRS DB 
--Replace #CRM DB NAME# with the name of the your CRM DB
USE #REPORT SERVER DB#

;WITH theExecution AS (
  SELECT
    C.Name as reportid,
    E.TimeEnd,
    ROW_NUMBER() OVER (PARTITION BY C.Name ORDER BY E.TimeEnd DESC) rowNumber
  FROM Executionlog E  
  INNER JOIN Catalog C on C.ItemID = E.ReportID AND C.Type = 2 AND C.Path LIKE '#CRM DB NAME#%'  
  AND E.Format = 'RPL'
), numberTimesRun AS (
  SELECT
  te.reportid,
  COUNT(*) NumberTimesRan
  FROM theExecution TE
  GROUP BY te.reportid
)
SELECT
R.ReportId,
R.Name,
R.FileName,
R.ModifiedOn LastModifiedOn,
TE.TimeEnd LastTimeGenerated,
NTR.NumberTimesRan
FROM [RGA_MSCRM].[dbo].Report R
  LEFT OUTER JOIN theExecution TE ON TE.reportid = r.ReportId AND TE.rowNumber = 1
  LEFT OUTER JOIN numberTimesRun NTR ON NTR.reportid = r.ReportId
WHERE
R.ispersonal = 0 --Exclude Personal reports 
ORDER BY R.Name  
Running this query will return a result set that looks like the below report but with more data.

ReportId ReportName FileNameLastModifiedOn LastTimeGenerated NumberTimesRan
7AD95C7E-737A-DF11-AB37-00145EED82FA Account Distribution Account Distribution.rdl2011-07-24 02:11:35.000 NULL 80
AAA85E84-737A-DF11-AB37-00145EED82FA Account Distribution Detail Account Distribution Detail.rdl 2011-07-24 02:11:35.000 NULL 80
82D95C7E-737A-DF11-AB37-00145EED82FA Account Overview Account_Overview.rdl 2011-09-30 20:06:08.000 2011-09-30 15:15:07.290 221
...

On thing to keep in mind when you report is that by default, SSRS log entries are kept 60 days. Entries that exceed this date are removed at 2:00 A.M. every day. On a mature installation, only 60 days of information will be available at any given time. You can read more about the SSRS execution log at http://msdn.microsoft.com/en-us/library/ms159110.aspx.

No comments:

Post a Comment