[英]is there a way to query future SSRS subscription schedules?
In my office, many of us use SSRS to schedule recurring reports. 在我的办公室,我们许多人使用SSRS来安排重复报告。 I want to view the schedule of upcoming report runs, for the next few days or a week, so that we can see whether 20 reports are all going to try to run at the same time. 我想在接下来的几天或一周内查看即将到来的报告运行的时间表,以便我们可以看到20个报告是否都将尝试同时运行。 How can I accomplish that? 我怎么能做到这一点? I've created t-sql reports that show subscription information, but they only show "last run" dates and times. 我创建了显示订阅信息的t-sql报告,但它们只显示“上次运行”的日期和时间。 That's not helpful for predicting tomorrow's bottlenecks. 这对预测明天的瓶颈没有帮助。 The solution should include data-driven subscriptions too. 解决方案也应该包括数据驱动的订阅。
SSRS stores all of its data in the ReportServer database so you'll need an account with read access to this database. SSRS将其所有数据存储在ReportServer数据库中,因此您需要一个对此数据库具有读访问权限的帐户。 This is all relevant for SSRS running in native mode. 这与以纯模式运行的SSRS相关。 I'm also not sure if shared schedules or data-driven subscriptions will be handled by this code, but I'm pretty sure they will be. 我也不确定此代码是否会处理共享计划或数据驱动订阅,但我很确定它们会是。 I just haven't tested them. 我只是没有测试过它们。
PLEASE NOTE: Microsoft does not recommend or support directly querying the ReportServer database. 请注意: Microsoft不建议或支持直接查询ReportServer数据库。 They could change the structure in the next version or update of SSRS and you likely won't get any warning. 他们可以在下一个版本中更改结构或更新SSRS,您可能不会收到任何警告。 The Microsoft recommendation is to always use the SSRS web service when you need to interrogate information about reporting services. Microsoft建议在需要查询有关报告服务的信息时始终使用SSRS Web服务。
These are the tables that are relevant for pulling out the subscription information: 这些是与提取订阅信息相关的表格:
The SQL below pulls out schedule interval information for all reports. 下面的SQL提取所有报告的计划间隔信息。 It doesn't calculate the next run dates but by figuring out the interval that the schedule is supposed to run on you can write another query to generate the actual dates. 它不计算下一个运行日期,但通过计算运行计划的间隔,您可以编写另一个查询来生成实际日期。
This SQL was originally written for a report that just displays a string describing the interval so the final output is probably not what you're after. 此SQL最初是为报表编写的,该报表只显示描述间隔的字符串,因此最终输出可能不是您所追求的。 It should give you a good starting point though since it does figure out all of the interval details. 它应该给你一个很好的起点,因为它确实找出了所有的间隔细节。
--these CTEs are used to match the bitmask fields in the schedule to determine which days & months the schedule is triggered on
WITH wkdays AS (
SELECT 'Sunday' AS label, 1 AS daybit
UNION ALL
SELECT 'Monday', 2
UNION ALL
SELECT 'Tuesday', 4
UNION ALL
SELECT 'Wednesday', 8
UNION ALL
SELECT 'Thursday', 16
UNION ALL
SELECT 'Friday', 32
UNION ALL
SELECT 'Saturday', 64
),
monthdays AS (
SELECT CAST(number AS VARCHAR(2)) AS label,
POWER(CAST(2 AS BIGINT),number-1) AS daybit
FROM master.dbo.spt_values
WHERE type='P' AND number BETWEEN 1 AND 31
),
months AS (
SELECT DATENAME(MM,DATEADD(MM,number-1,0)) AS label,
POWER(CAST(2 AS BIGINT),number-1) AS mnthbit
FROM master.dbo.spt_values
WHERE type='P' AND number BETWEEN 1 AND 12
)
SELECT cat.path,
cat.name,
cat.creationdate,
cat.modifieddate,
subs.Description,
subs.LastStatus,
subs.LastRunTime,
subs.InactiveFlags,
CASE RecurrenceType
WHEN 1 THEN 'Once'
WHEN 2 THEN 'Hourly'
WHEN 3 THEN 'Daily' --by interval
WHEN 4 THEN
CASE
WHEN WeeksInterval>1 THEN 'Weekly'
ELSE 'Daily' --by day of week
END
WHEN 5 THEN 'Monthly' --by calendar day
WHEN 6 THEN 'Monthly' --by day of week
END AS sched_type,
sched.StartDate,
sched.MinutesInterval,
sched.RecurrenceType,
sched.DaysInterval,
sched.WeeksInterval,
sched.MonthlyWeek,
wkdays.label AS wkday,wkdays.daybit AS wkdaybit,
monthdays.label AS mnthday,monthdays.daybit AS mnthdaybit,
months.label AS mnth, months.mnthbit
INTO #t
FROM dbo.Catalog AS cat
LEFT JOIN dbo.ReportSchedule AS repsched ON repsched.ReportID=cat.ItemID
LEFT JOIN dbo.Subscriptions AS subs ON subs.SubscriptionID=repsched.SubscriptionID
LEFT JOIN dbo.Schedule AS sched ON sched.ScheduleID=repsched.ScheduleID
LEFT JOIN wkdays ON wkdays.daybit & sched.DaysOfWeek > 0
LEFT JOIN monthdays ON monthdays.daybit & sched.DaysOfMonth > 0
LEFT JOIN months ON months.mnthbit & sched.[Month] > 0
WHERE cat.ParentID IS NOT NULL --all reports have a ParentID
/* THE PREVIOUS QUERY LEAVES MULTIPLE ROWS FOR SUBSCRIPTIONS THAT HAVE MULTIPLE BITMASK MATCHES *
* THIS QUERY WILL CONCAT ALL OF THOSE FIELDS TOGETHER AND ACCUMULATE THEM IN A TABLE FOR USE LATER. */
CREATE TABLE #c (type VARCHAR(16) COLLATE Latin1_General_CI_AS_KS_WS, name VARCHAR(255) COLLATE Latin1_General_CI_AS_KS_WS, path VARCHAR(255) COLLATE Latin1_General_CI_AS_KS_WS, concatStr VARCHAR(2000) COLLATE Latin1_General_CI_AS_KS_WS);
WITH d AS (
SELECT DISTINCT path,
name,
mnthday AS lbl,
mnthdaybit AS bm
FROM #t
)
INSERT INTO #c (type,path,name,concatStr)
SELECT 'monthday' AS type,
t1.path,t1.name,
STUFF((
SELECT ', ' + CAST(lbl AS VARCHAR(MAX))
FROM d AS t2
WHERE t2.path=t1.path AND t2.name=t1.name
ORDER BY bm
FOR XML PATH(''),TYPE
).value('.','VARCHAR(MAX)'),1,2,'') AS concatStr
FROM d AS t1
GROUP BY t1.path,t1.name;
WITH d AS (
SELECT DISTINCT path,
name,
wkday AS lbl,
wkdaybit AS bm
FROM #t
)
INSERT INTO #c (type,path,name,concatStr)
SELECT 'weekday' AS type,
t1.path,t1.name,
STUFF((
SELECT ', ' + CAST(lbl AS VARCHAR(MAX))
FROM d AS t2
WHERE t2.path=t1.path AND t2.name=t1.name
ORDER BY bm
FOR XML PATH(''),TYPE
).value('.','VARCHAR(MAX)'),1,2,'') AS concatStr
FROM d AS t1
GROUP BY t1.path,t1.name;
WITH d AS (
SELECT DISTINCT path,
name,
mnth AS lbl,
mnthbit AS bm
FROM #t
)
INSERT INTO #c (type,path,name,concatStr)
SELECT 'month' AS type,
t1.path,t1.name,
STUFF((
SELECT ', ' + CAST(lbl AS VARCHAR(MAX))
FROM d AS t2
WHERE t2.path=t1.path AND t2.name=t1.name
ORDER BY bm
FOR XML PATH(''),TYPE
).value('.','VARCHAR(MAX)'),1,2,'') AS concatStr
FROM d AS t1
GROUP BY t1.path,t1.name;
/* PUT EVERYTHING TOGETHER FOR THE REPORT */
SELECT a.path,a.name,a.sched_type,
a.creationdate,a.modifieddate,
a.description AS sched_desc,
a.laststatus AS sched_laststatus,
a.lastruntime AS sched_lastrun,
a.inactiveflags AS sched_inactive,
CASE RecurrenceType
WHEN 1 THEN 'Run once on '
ELSE 'Starting on '
END + CAST(StartDate AS VARCHAR(32)) + ' ' +
CASE RecurrenceType
WHEN 1 THEN ''
WHEN 2 THEN 'repeat every ' + CAST(MinutesInterval AS VARCHAR(255)) + ' minutes.'
WHEN 3 THEN 'repeat every ' + CAST(DaysInterval AS VARCHAR(255)) + ' days.'
WHEN 4 THEN
CASE
WHEN WeeksInterval>1 THEN 'repeat every ' + CAST(WeeksInterval AS VARCHAR(255)) + ' on ' + COALESCE(wkdays.concatStr,'')
ELSE 'repeat every ' + COALESCE(wkdays.concatStr,'')
END
WHEN 5 THEN 'repeat every ' + COALESCE(mnths.concatStr,'') + ' on calendar day(s) ' + COALESCE(mnthdays.concatStr,'')
WHEN 6 THEN 'run on the ' + CASE MonthlyWeek WHEN 1 THEN '1st' WHEN 2 THEN '2nd' WHEN 3 THEN '3rd' WHEN 4 THEN '4th' WHEN 5 THEN 'Last' END + ' week of ' + COALESCE(mnths.concatStr,'') + ' on ' + COALESCE(wkdays.concatStr,'')
END AS sched_pattern
FROM (
SELECT DISTINCT path,name,creationdate,modifieddate,description,laststatus,lastruntime,inactiveflags,sched_type,recurrencetype,startdate,minutesinterval,daysinterval,weeksinterval,monthlyweek
FROM #t
) AS a
LEFT JOIN #c AS mnthdays ON mnthdays.path=a.path AND mnthdays.name=a.name AND mnthdays.type='monthday'
LEFT JOIN #c AS wkdays ON wkdays.path=a.path AND wkdays.name=a.name AND wkdays.type='weekday'
LEFT JOIN #c AS mnths ON mnths.path=a.path AND mnths.name=a.name AND mnths.type='month'
DROP TABLE #t,#c;
Below Query can help you fetch schedule for your reports for the next day, this is built on standard metadata tables from report server database. 查询下方可以帮助您获取第二天报告的计划,这是基于报表服务器数据库的标准元数据表。
Query: 查询:
SELECT CAT.Name
,CAT.[Path] AS ReportPath
--,SUB.LastRunTime
,SCH.NextRunTime
,CONVERT(VARCHAR(10), CONVERT(datetime, SCH.NextRunTime, 1), 101) As RunDate
,right(convert(varchar(32),SCH.NextRunTime,100),8) As RunTime
,SUB.[Description]
,SUB.EventType
,SUB.LastStatus
,SUB.ModifiedDate
,SCH.Name AS ScheduleName
FROM reportserver.dbo.Subscriptions AS SUB
INNER JOIN reportserver.dbo.Users AS USR
ON SUB.OwnerID = USR.UserID
INNER JOIN reportserver.dbo.[Catalog] AS CAT
ON SUB.Report_OID = CAT.ItemID
INNER JOIN reportserver.dbo.ReportSchedule AS RS
ON SUB.Report_OID = RS.ReportID
AND SUB.SubscriptionID = RS.SubscriptionID
INNER JOIN reportserver.dbo.Schedule AS SCH
ON RS.ScheduleID = SCH.ScheduleID
Where CONVERT(VARCHAR(10), CONVERT(datetime, SCH.NextRunTime, 1), 101) = CONVERT(VARCHAR(10), CONVERT(datetime, getDate()+1, 1), 101)
ORDER BY USR.UserName
,CAT.[Path];
This procedure will give list of all values related to Report subscription. 此过程将提供与报告订阅相关的所有值的列表。
here you will find startdate. 在这里你会发现startdate。 and on the basis of that you can complete your task. 并在此基础上,您可以完成您的任务。
Create PROCEDURE [dbo].[GetSubscriptionData]
AS
BEGIN
SET NOCOUNT ON;
WITH
[Sub_Parameters] AS
(
SELECT [SubscriptionID], [Parameters] = CONVERT(XML,a.[Parameters])
FROM [Subscriptions] a
),
[MySubscriptions] AS
(
SELECT DISTINCT [SubscriptionID], [ParameterName] = QUOTENAME(p.value('(Name)[1]', 'nvarchar(max)')), [ParameterValue] = p.value('(Value)[1]', 'nvarchar(max)')
FROM [Sub_Parameters] a
CROSS APPLY [Parameters].nodes('/ParameterValues/ParameterValue') t(p)
),
[SubscriptionsAnalysis] AS
(
SELECT a.[SubscriptionID], a.[ParameterName], [ParameterValue] =
(
SELECT STUFF((SELECT [ParameterValue] + ', ' as [text()]
FROM [MySubscriptions]
WHERE [SubscriptionID] = a.[SubscriptionID] AND [ParameterName] = a.[ParameterName]
FOR XML PATH('') ),1, 0, '') +''
)
FROM [MySubscriptions] a
GROUP BY a.[SubscriptionID],a.[ParameterName]
)
SELECT
DISTINCT (a.[SubscriptionID]),
c.[UserName] AS Owner,
b.Name as ReportName,
Convert(XML,a.[ExtensionSettings]).value('(//ParameterValue/Value[../Name="RENDER_FORMAT"])[1]','nvarchar(50)') as ReportExtension,
b.Path,
a.[Locale],
a.[InactiveFlags],
d.[UserName] AS Modified_by,
a.[ModifiedDate],
a.[Description],
a.[LastStatus],
a.[EventType],
a.[LastRunTime],
a.[DeliveryExtension],
a.[Version],
sch.StartDate,
--e.[ParameterName],
--LEFT(e.[ParameterValue],LEN(e.[ParameterValue])-1) as [ParameterValue],
SUBSTRING(b.PATH,2,LEN(b.PATH)-(CHARINDEX('/',REVERSE(b.PATH))+1)) AS ProjectName
FROM
[Subscriptions] a
INNER JOIN [Catalog] AS b ON a.[Report_OID] = b.[ItemID]
Inner Join ReportSchedule as RS on rs.SubscriptionID = a.SubscriptionID
INNER JOIN Schedule AS Sch ON Sch.ScheduleID = rs.ScheduleID
LEFT OUTER JOIN [Users] AS c ON a.[OwnerID] = c.[UserID]
LEFT OUTER JOIN [Users] AS d ON a.MODIFIEDBYID = d.Userid
LEFT OUTER JOIN [SubscriptionsAnalysis] AS e ON a.SubscriptionID = e.SubscriptionID;
END
I reviewed a bunch of different solutions to this problem. 我回顾了这个问题的一系列不同解决方案。 And I finally found one that work best. 我终于找到了一个效果最好的。
My solutions starts with the query in answer from Mike D. did not like his query at first because because it looked complicated and returned too much data to review-- But I chose to amend his query because it provides info beyond just subscriptions (Cache schedules/ Snapshots). 我的解决方案从Mike D.的回答开始,起初不喜欢他的查询,因为它看起来很复杂,并且返回了太多的数据来审查 - 但我选择修改他的查询,因为它提供的信息不仅仅是订阅(缓存计划) /快照)。
I modified his query as follows: 我修改了他的查询如下:
Exclude results of server content that do not use any scheduling 排除不使用任何计划的服务器内容的结果
Improved the formatting to taste 改进了格式化的品味
Added useful fields that give more information about how the Schedule is being used. 添加了有用的字段,提供有关如何使用计划的更多信息。
Resolved collation conflict error message: Cannot resolve the collation conflict between temp table and sys.objects -- 已解决的排序规则冲突错误消息: 无法解决临时表和sys.objects之间的排序规则冲突 -
I might update this later once I've used other features such as "data-driven" subscription instead of "timed" Or I might update this if I make improvements 一旦我使用了其他功能,例如“数据驱动”订阅而不是“定时”,我可能会稍后更新。如果我做了改进,我可能会更新
Sample Data 样本数据
NOTE: One thing I do not understand is why there are 2 TimedSubscriptions (row 7-8) because I looked in the portal and I see only there (row 8 with the shared schedule). 注意:我不明白的一件事是为什么有2个TimedSubscriptions(第7-8行),因为我查看了门户网站,我只看到那里(第8行共享时间表)。 Anyone know why? 谁知道为什么?
+-----------------------------------+------------+-----------------------+-------------------+-------------------+--------------------------------------------------------------------------+---------------------+------------------+------------------+-----------------------------------------------------------------------+--------------+--------------------------------------+--------------------------------------+------------------+-------------------+------------------+------------+---------+--------------------------------------+---------------------------------------------------------------------+------------------+----------------+---------------------------------------------------------+----------------------------------------------------------------------+
| name | sched_type | ScheduleType | Usage | SubscriptionType | Elements_List | DeliveryExtension | UserOwnerSubs | UserCreatedSched | EmailRecipients | RenderFormat | ScheduleName | SubscriptionID | creationdate | modifieddate | modifieddate2 | StartDate | EndDate | sched_desc | sched_laststatus | sched_lastrun | sched_inactive | path | sched_pattern |
+-----------------------------------+------------+-----------------------+-------------------+-------------------+--------------------------------------------------------------------------+---------------------+------------------+------------------+-----------------------------------------------------------------------+--------------+--------------------------------------+--------------------------------------+------------------+-------------------+------------------+------------+---------+--------------------------------------+---------------------------------------------------------------------+------------------+----------------+---------------------------------------------------------+----------------------------------------------------------------------+
| PO Samples for Testing | Daily | TimedSubscription | TimedSubscription | TimedSubscription | TO CC BCC ReplyTo IncludeReport RenderFormat Subject Comment IncludeLink | Report Server Email | GEORGES\bl0040ep | GEORGES\bl0040ep | (x24) TO: brenda.metcalf@DOMAIN; | MHTML | bb53dfb1-6819-4a99-94bd-28ec3dcf3ecb | AF285C92-31E5-4BC4-9D88-284E0A3ED17B | 2/8/19 10:38 AM | 2/8/19 10:38 AM | 2/12/19 10:44 AM | 2/8/2019 | NULL | SUN Caryville PO Samples for Testing | Mail sent to brenda.metcalf@georgesinc.com; | 2/10/19 6:00 PM | 0 | /fsqa/PO Samples for Testing | Starting on Feb 8 2019 6:00PM repeat every Sunday Ending on Never |
| | | | | | Priority | | | | Shannon.Driggers@DOMAIN; Joseph.Davenport@DOMAIN; Lisa.Fude@DOMAIN; | | | | | | | | | | Shannon.Driggers@georgesinc.com; Joseph.Davenport@georgesinc.com; | | | | |
| | | | | | | | | | Amanda.Bourff@DOMAIN; Pam.Overton@DOMAIN; Brent.Lester@DOMAIN; | | | | | | | | | | Lisa.Fude@georgesinc.com; Amanda.Bourff@georgesinc.com; | | | | |
| | | | | | | | | | Jerry.McKnight@DOMAIN; Jacob.Phillips@DOMAIN; Ricky.Cole@DOMAIN; | | | | | | | | | | Pam.Overton@georgesinc.com | | | | |
| | | | | | | | | | Jeremy.Morris@DOMAIN; Bryan.Claiborne@DOMAIN; Harold.Webb@DOMAIN; | | | | | | | | | | | | | | |
| | | | | | | | | | Georgia.Roberts@DOMAIN; Chris.Malone@DOMAIN; Louis.Bargy@DOMAIN; | | | | | | | | | | | | | | |
| | | | | | | | | | Josh.Bills@DOMAIN; Larry.Reid@DOMAIN; Chris.Thompson@DOMAIN; | | | | | | | | | | | | | | |
| | | | | | | | | | Trenton.Marshall@DOMAIN; Willie.Baker@DOMAIN; Jack.Badon@DOMAIN; | | | | | | | | | | | | | | |
| | | | | | | | | | Susan.Delaney@DOMAIN CC: Suzanne.Beauchamp@DOMAIN | | | | | | | | | | | | | | |
+-----------------------------------+------------+-----------------------+-------------------+-------------------+--------------------------------------------------------------------------+---------------------+------------------+------------------+-----------------------------------------------------------------------+--------------+--------------------------------------+--------------------------------------+------------------+-------------------+------------------+------------+---------+--------------------------------------+---------------------------------------------------------------------+------------------+----------------+---------------------------------------------------------+----------------------------------------------------------------------+
| Sigma SO Loaded with Inventory | Daily | ReportHistorySchedule | ...ReportSnapshot | NULL | NULL | NULL | NULL | GEORGES\bl0040ep | NULL | NULL | 98a284df-3d03-445e-88d9-a44ed2d5c33a | NULL | 7/2/18 2:38 PM | 7/2/18 2:38 PM | NULL | 7/2/2018 | NULL | NULL | NULL | NULL | NULL | /Inventory/Sigma SO Loaded with Inventory (Cassville) | Starting on Jul 2 2018 6:20AM repeat every 1 days. Ending on Never |
| (Cassville) | | | | | | | | | | | | | | | | | | | | | | | |
+-----------------------------------+------------+-----------------------+-------------------+-------------------+--------------------------------------------------------------------------+---------------------+------------------+------------------+-----------------------------------------------------------------------+--------------+--------------------------------------+--------------------------------------+------------------+-------------------+------------------+------------+---------+--------------------------------------+---------------------------------------------------------------------+------------------+----------------+---------------------------------------------------------+----------------------------------------------------------------------+
| Customer Aging Report By Customer | Hourly | SharedSchedule | RefreshCache | RefreshCache | NULL | NULL | GEORGES\bl0040ep | GEORGES\bl0040ep | NULL | NULL | Hourly: Even Hours After 6AM | CF87DDDE-0F7F-416E-B403-E161AD0B14C1 | 10/25/18 1:55 PM | 12/13/18 12:46 PM | 11/29/18 3:55 PM | 11/30/2018 | NULL | Hourly: Even Hours After 6AM (625) | Cache refresh succeeded. | 2/12/19 10:00 AM | 0 | /Accounts Receivables/Customer Aging Report By Customer | Starting on Nov 30 2018 6:00AM |
| | | | | | | | | | | | | | | | | | | | | | | | repeat every 120 minutes. Ending on Never |
+-----------------------------------+------------+-----------------------+-------------------+-------------------+--------------------------------------------------------------------------+---------------------+------------------+------------------+-----------------------------------------------------------------------+--------------+--------------------------------------+--------------------------------------+------------------+-------------------+------------------+------------+---------+--------------------------------------+---------------------------------------------------------------------+------------------+----------------+---------------------------------------------------------+----------------------------------------------------------------------+
The Query (UPDATED: 2/12/2019) 查询(更新时间:2/12/2019)
This query is essentially the same but I have added more data element for analyzing the subscriptions. 此查询基本相同,但我添加了更多数据元素来分析订阅。 One issue resolved is [EmailRecipients] cutting and truncating the full list of recipients: value(N'(/ParameterValues/ParameterValue[Name="CC"]/Value)[1]', 'varchar(250)') changed to value(N'(/ParameterValues/ParameterValue[Name="CC"]/Value)[1]', 'varchar(max)') . 解决的一个问题是[EmailRecipients]剪切和截断收件人的完整列表: 值(N'(/ ParameterValues / ParameterValue [Name =“CC”] / Value)[1]','varchar(250)')更改为值(N'(/ ParameterValues / ParameterValue [Name =“CC”] / Value)[1]','varchar(max)') 。
subs.DeliveryExtension
subs.ExtensionSettings
CAST(CAST(subs.ExtensionSettings AS XML).query('data(ParameterValues/ParameterValue/Name)') as nvarchar(500)) AS Elements_List
'TO: ' + CAST(subs.ExtensionSettings AS xml).value(N'(/ParameterValues/ParameterValue[Name="TO"]/Value)[1]', 'varchar(250)') + ' CC: ' + ISNULL(CAST(subs.ExtensionSettings AS xml).value(N'(/ParameterValues/ParameterValue[Name="CC"]/Value)[1]', 'varchar(250)'), ' ') as EmailRecipients
CAST(subs.ExtensionSettings AS xml).value(N'(/ParameterValues/ParameterValue[Name="RenderFormat"]/Value)[1]', 'varchar(250)') as RenderFormat
subs.SubscriptionID
UserCreatedSched.UserName as [UserCreatedSched]
SQL: SQL:
----------------------------------------------------------------------------------------------------------------------------------------------------------
-- Get all schedule usage information: Subscription/ Snapshot/ Caching
-- queryGetScheduleDetails
-- https://stackoverflow.com/questions/25943877/is-there-a-way-to-query-future-ssrs-subscription-schedules/25944797#25944797
-- these CTEs are used to match the bitmask fields in the schedule to determine which days & months the schedule is triggered on
----------------------------------------------------------------------------------------------------------------------------------------------------------
IF @QueryCalled = 'queryGetScheduleDetails'
BEGIN
WITH wkdays AS (
SELECT
'Sunday' AS label, 1 AS daybit
UNION ALL
SELECT 'Monday', 2
UNION ALL
SELECT 'Tuesday', 4
UNION ALL
SELECT 'Wednesday', 8
UNION ALL
SELECT 'Thursday', 16
UNION ALL
SELECT 'Friday', 32
UNION ALL
SELECT 'Saturday', 64
)
,monthdays AS (
SELECT
CAST(number AS VARCHAR(2)) AS label
,POWER(CAST(2 AS BIGINT),number-1) AS daybit
FROM master.dbo.spt_values
WHERE type='P' AND number BETWEEN 1 AND 31
)
,months AS (
SELECT
DATENAME(MM,DATEADD(MM,number-1,0)) AS label
,POWER(CAST(2 AS BIGINT),number-1) AS mnthbit
FROM master.dbo.spt_values
WHERE type='P' AND number BETWEEN 1 AND 12
)
SELECT
cat.path
, cat.name
, cat.creationdate
, cat.modifieddate
, subs.ModifiedDate as ModifiedDate2
, subs.Description
, UserOwnerSubs.UserName as [UserOwnerSubs]
, subs.LastStatus
, subs.LastRunTime
, subs.InactiveFlags
, subs.EventType as [SubscriptionType]
, subs.DeliveryExtension
, subs.ExtensionSettings
, CAST(CAST(subs.ExtensionSettings AS XML).query('data(ParameterValues/ParameterValue/Name)') as nvarchar(max)) AS Elements_List
-- <RECIPIENTS function>
,
'TO: '
+ CAST(subs.ExtensionSettings AS xml).value(N'(/ParameterValues/ParameterValue[Name="TO"]/Value)[1]', 'varchar(max)')
+ ' CC: '
+ ISNULL(CAST(subs.ExtensionSettings AS xml).value(N'(/ParameterValues/ParameterValue[Name="CC"]/Value)[1]', 'varchar(max)'), ' ')
as EmailRecipients
-- </RECIPIENTS function>
, CAST(subs.ExtensionSettings AS xml).value(N'(/ParameterValues/ParameterValue[Name="RenderFormat"]/Value)[1]', 'varchar(max)')
as RenderFormat
, subs.SubscriptionID
, sched.Name as [ScheduleName]
, UserCreatedSched.UserName as [UserCreatedSched]
, sched.EventType as [ScheduleType]
--
, CASE RecurrenceType
WHEN 1 THEN 'Once'
WHEN 2 THEN 'Hourly'
WHEN 3 THEN 'Daily' --by interval
WHEN 4 THEN
CASE
WHEN WeeksInterval>1 THEN 'Weekly'
ELSE 'Daily' --by day of week
END
WHEN 5 THEN 'Monthly' --by calendar day
WHEN 6 THEN 'Monthly' --by day of week
END AS [sched_type]
, sched.StartDate
, sched.EndDate
, sched.MinutesInterval
, sched.RecurrenceType
, sched.DaysInterval
, sched.WeeksInterval
, sched.MonthlyWeek
, wkdays.label AS [wkday]
, wkdays.daybit AS [wkdaybit]
, monthdays.label AS [mnthday]
, monthdays.daybit AS [mnthdaybit]
, months.label AS [mnth]
, months.mnthbit
INTO #t
FROM
dbo.Catalog AS cat
LEFT JOIN dbo.ReportSchedule AS repsched ON repsched.ReportID=cat.ItemID
LEFT JOIN dbo.Subscriptions AS subs ON subs.SubscriptionID=repsched.SubscriptionID
LEFT JOIN dbo.Schedule AS sched ON sched.ScheduleID=repsched.ScheduleID
LEFT JOIN wkdays ON wkdays.daybit & sched.DaysOfWeek > 0
LEFT JOIN monthdays ON monthdays.daybit & sched.DaysOfMonth > 0
LEFT JOIN months ON months.mnthbit & sched.[Month] > 0
LEFT JOIN dbo.Users UserOwnerSubs ON subs.OwnerId = UserOwnerSubs.UserID
LEFT JOIN dbo.Users UserCreatedSched ON sched.CreatedByID = UserCreatedSched.UserID
WHERE cat.ParentID IS NOT NULL --all reports have a ParentID
/* THE PREVIOUS QUERY LEAVES MULTIPLE ROWS FOR SUBSCRIPTIONS THAT HAVE MULTIPLE BITMASK MATCHES *
* THIS QUERY WILL CONCAT ALL OF THOSE FIELDS TOGETHER AND ACCUMULATE THEM IN A TABLE FOR USE LATER. */
CREATE TABLE #c (type VARCHAR(16) COLLATE Latin1_General_CI_AS_KS_WS, name VARCHAR(255) COLLATE Latin1_General_CI_AS_KS_WS, path VARCHAR(255) COLLATE Latin1_General_CI_AS_KS_WS, concatStr VARCHAR(2000) COLLATE Latin1_General_CI_AS_KS_WS);
WITH d AS (
SELECT DISTINCT
path
, name
, mnthday AS lbl
, mnthdaybit AS bm
FROM #t
)
INSERT INTO #c (type,path,name,concatStr)
SELECT
'monthday' AS type
, t1.path,t1.name
, STUFF((
SELECT ', ' + CAST(lbl AS VARCHAR(MAX))
FROM d AS t2
WHERE t2.path=t1.path AND t2.name=t1.name
ORDER BY bm
FOR XML PATH(''),TYPE
).value('.','VARCHAR(MAX)'),1,2,'')
AS concatStr
FROM d AS t1
GROUP BY t1.path,t1.name;
WITH d AS (
SELECT DISTINCT path,
name,
wkday AS lbl,
wkdaybit AS bm
FROM #t
)
INSERT INTO #c (type,path,name,concatStr)
SELECT
'weekday' AS type
, t1.path,t1.name
, STUFF(
(
SELECT ', ' + CAST(lbl AS VARCHAR(MAX))
FROM d AS t2
WHERE t2.path=t1.path AND t2.name=t1.name
ORDER BY bm
FOR XML PATH(''),TYPE
).value('.','VARCHAR(MAX)'),1,2,'')
AS concatStr
FROM d AS t1
GROUP BY t1.path,t1.name;
WITH d AS (
SELECT DISTINCT
path
, name
, mnth AS lbl
, mnthbit AS bm
FROM #t
)
INSERT INTO #c (type,path,name,concatStr)
SELECT
'month' AS type
, t1.path,t1.name
, STUFF(
(
SELECT ', ' + CAST(lbl AS VARCHAR(MAX))
FROM d AS t2
WHERE t2.path=t1.path AND t2.name=t1.name
ORDER BY bm
FOR XML PATH(''),TYPE
).value('.','VARCHAR(MAX)'),1,2,'')
AS concatStr
FROM d AS t1
GROUP BY t1.path,t1.name;
/* PUT EVERYTHING TOGETHER FOR THE REPORT */
SELECT
a.name
, a.sched_type
, ScheduleType
, CASE
WHEN a.description IS NOT NULL THEN SubscriptionType
WHEN a.ScheduleType='ReportHistorySchedule' THEN '...ReportSnapshot'
ELSE '...ReportCache'
END AS [Usage]
, SubscriptionType
, a.Elements_List
, a.DeliveryExtension
, a.UserOwnerSubs
, a.UserCreatedSched
, a.EmailRecipients
, a.RenderFormat
-- , ExtensionSettings
, ScheduleName
, SubscriptionID
, a.creationdate
, a.modifieddate
, a.modifieddate2
, CAST(a.StartDate as date) as StartDate
, CAST(a.EndDate as date) as EndDate
, a.description AS sched_desc
, a.laststatus AS sched_laststatus
, a.lastruntime AS sched_lastrun
, a.inactiveflags AS sched_inactive
, a.path
, CASE RecurrenceType
WHEN 1 THEN 'Run once on '
ELSE 'Starting on '
END
+ CAST(StartDate AS VARCHAR(32)) + ' ' +
CASE RecurrenceType
WHEN 1 THEN ''
WHEN 2 THEN 'repeat every ' + CAST(MinutesInterval AS VARCHAR(255)) + ' minutes.'
WHEN 3 THEN 'repeat every ' + CAST(DaysInterval AS VARCHAR(255)) + ' days.'
WHEN 4 THEN
CASE
WHEN WeeksInterval>1 THEN 'repeat every ' + CAST(WeeksInterval AS VARCHAR(255)) + ' on ' + COALESCE(wkdays.concatStr,'')
ELSE 'repeat every ' + COALESCE(wkdays.concatStr,'')
END
WHEN 5 THEN 'repeat every ' + COALESCE(mnths.concatStr,'') + ' on calendar day(s) ' + COALESCE(mnthdays.concatStr,'')
WHEN 6 THEN 'run on the ' +
CASE
MonthlyWeek WHEN 1 THEN '1st' WHEN 2 THEN '2nd' WHEN 3 THEN '3rd' WHEN 4 THEN '4th' WHEN 5 THEN 'Last'
END
+ ' week of ' + COALESCE(mnths.concatStr,'') + ' on ' + COALESCE(wkdays.concatStr,'')
END
+ ' Ending on ' + ISNULL(CAST(EndDate AS VARCHAR(32)), 'Never')
AS sched_pattern
FROM
(
SELECT DISTINCT path,name,creationdate,modifieddate,modifieddate2,SubscriptionType,RenderFormat, /*ExtensionSettings,*/ ScheduleName, UserOwnerSubs,ScheduleType,SubscriptionID,description, UserCreatedSched,laststatus,lastruntime,StartDate,EndDate,inactiveflags,sched_type,recurrencetype,minutesinterval,daysinterval,weeksinterval,monthlyweek
,cast(Elements_List as nvarchar(500)) as Elements_List, DeliveryExtension
, '(x'
+ CAST((LEN(EmailRecipients) - LEN(REPLACE(EmailRecipients,'@',''))) / LEN('@') as nvarchar(10))
+ ') '
+ REPLACE(EmailRecipients,'@GEORGESINC.COM','@DOMAIN')
as EmailRecipients
FROM #t
) AS a
LEFT JOIN #c AS mnthdays ON mnthdays.path COLLATE DATABASE_DEFAULT =a.path COLLATE DATABASE_DEFAULT
AND mnthdays.name COLLATE DATABASE_DEFAULT =a.name COLLATE DATABASE_DEFAULT
AND mnthdays.type COLLATE DATABASE_DEFAULT ='monthday' COLLATE DATABASE_DEFAULT
LEFT JOIN #c AS wkdays ON wkdays.path COLLATE DATABASE_DEFAULT =a.path COLLATE DATABASE_DEFAULT
AND wkdays.name COLLATE DATABASE_DEFAULT=a.name COLLATE DATABASE_DEFAULT
AND wkdays.type COLLATE DATABASE_DEFAULT ='weekday' COLLATE DATABASE_DEFAULT
LEFT JOIN #c AS mnths ON mnths.path COLLATE DATABASE_DEFAULT =a.path COLLATE DATABASE_DEFAULT
AND mnths.name COLLATE DATABASE_DEFAULT =a.name COLLATE DATABASE_DEFAULT
AND mnths.type COLLATE DATABASE_DEFAULT ='month' COLLATE DATABASE_DEFAULT
WHERE
a.sched_type IS NOT NULL
AND (@ReportName = 'All Reports' OR a.Name like @ReportName)
DROP TABLE #t,#c;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.