如何在SQL Query中为特定报告在SSRS报告中创建新快照

时间:2017-07-12 19:55:36

标签: sql reporting-services

通过用户界面单击新快照按钮可以轻松为报表服务器上的报表创建快照,但我需要仅使用SQL查询创建新快照。这意味着什么是SQL查询,我可以在不使用用户界面的情况下为报表服务器上的特定报表创建新快照?我

1 个答案:

答案 0 :(得分:1)

不鼓励直接访问ReportServer数据库。生成SSRS报告快照的首选方法是使用C#调用CreateReportHistorySnapshot API方法。但是,如果必须使用SQL,请查看AddEvent ReportServer存储过程。

exec [ReportServer].dbo.AddEvent @EventType='ReportExecutionUpdateSchedule', @EventData='<InsertReportIDHere>'

可以找到更多信息herehere。下面是一个示例SQL脚本,它可以每天为给定的报告生成新的快照。

declare @Path varchar(425)
set @Path = '/SSRS Testing and Training/Test_snapshot' -- the name of my linked report which renders from a snapshot

declare @EventData uniqueidentifier
select @EventData = (select ItemID from Catalog where Path = @Path) 

-- make a new snapshot in History table
exec ReportServer.dbo.AddEvent 'ReportHistorySchedule', @EventData 

-- !!!! wait until Reporting Services figures out that it has an event to process (it actually takes 5sec)
waitfor delay '00:00:10' 

-- take a snapshot ID from a newly created row in History table
declare @SnapshotDataID uniqueidentifier
select @SnapshotDataID = (select SnapshotDataID from history WHERE ReportID = @EventData)

-- set a date for a new Snapshot in Catalog table
-- use getdate() instead (select SnapshotDate from history WHERE ReportID = @EventData) because otherwise you'll get a UTC date for "last run date" in Report Manager which can confuse your users 
declare @SnapshotDate datetime
select @SnapshotDate = getdate() 

-- run a RS stored procedure which updates SnapshotDataID in Catalog table and some other necessary things
exec UpdateSnapshot @Path,@SnapshotDataID,@SnapshotDate