从SSRS报告2008发送电子邮件

时间:2018-11-14 14:30:30

标签: email reporting-services ssrs-2008 send

我在SSRS 2008报告中具有以下供应商数据。

ID    Name    Details    Email
1     abc      d1       rob.b@gmail.com
2     def      d2       pat@gmail.com
3     ghi      d3       golf@gmail.com
4     dft      d4       rob.b@gmail.com 

我需要将相关详细信息发送到每个电子邮件ID。例如,在上面的数据中,我需要将ID 1和ID 4的整个记录​​发送到rob.b@gmail.com。 有什么方法可以只将相关详细信息发送到报告中提供的电子邮件。有人可以建议。

1 个答案:

答案 0 :(得分:0)

-如果您具有SQL Server的企业版,则可以使用数据驱动订阅在SSRS中执行此操作。

如果您没有企业版,则仍可以使用计划的存储过程来模拟数据驱动的订阅,从而更加省力。

无论哪种方式,您都将拥有一个与参数一起使用的报告以标识单个记录。然后,单独的数据驱动部分将具有不同的查询,该查询获取报告所需的ID列表以及将其发送到的电子邮件地址。然后,数据驱动部件将为您在报告中创建的每个ID参数生成一个单独的报告,然后将其发送到该地址。

数据驱动订阅属性: Data Driven Subscription

不幸的是,这两种方法都无法解决一个问题。

MSDN - Data Driven Subscriptions

MSSQLTips.com - simulate-reporting-services-data-driven-subscriptions

这是我用来生成多封电子邮件的SP:

ALTER PROCEDURE [dbo].[RECORD_REVIEW_GENERATION]

AS
DECLARE @SUBSCRIPTION_ID AS VARCHAR(100) = (SELECT SETTING_VALUE FROM WORKING_STORAGE.dbo.SETTINGS WHERE SETTING_NAME = 'REVIEW_SUBSCRIPTION')
DECLARE @SUBJECT VARCHAR(200) 
DECLARE @COMMENT VARCHAR(500) 
DECLARE @REVIEW_ID VARCHAR(50) 
DECLARE @EXTENSION_SETTINGS XML
DECLARE @PARAMETERS XML
DECLARE @RECORD_ID AS INT 
DECLARE @MAX_RECORD_ID AS INT 
DECLARE @END_DATE AS DATE = GETDATE() - DATEPART(WEEKDAY, GETDATE())    --PREVIOUS SUNDAY
DECLARE @START_DATE AS DATE = DATEADD(DAY, -6, @END_DATE)   --PREVIOUS MONDAY
DECLARE @DATE VARCHAR(500) = CONVERT(VARCHAR(8), @END_DATE, 112)



SELECT @EXTENSION_SETTINGS = ExtensionSettings, @PARAMETERS = [Parameters]
FROM ReportServer.dbo.Subscriptions
WHERE SubscriptionID = @SUBSCRIPTION_ID;



IF OBJECT_ID('tempdb..#REVIEWS') IS NOT NULL DROP TABLE #REVIEWS 

CREATE TABLE #REVIEWS(ReviewID VARCHAR(50), ProviderName VARCHAR(250), SiteIDNo VARCHAR(20), 
                        ReviewDate VARCHAR(12), UserName VARCHAR(250), VisitPurpose VARCHAR(250), 
                        NoOfPhysicians INT, NoOfRecords INT, RECORD_ID INT IDENTITY(1,1)
                    )
INSERT INTO #REVIEWS(ReviewID, ProviderName, SiteIDNo, ReviewDate, UserName, VisitPurpose, NoOfPhysicians, NoOfRecords)
SELECT ReviewID, ProviderName, SiteIDNo, ReviewDate, UserFirstName + ' ' + UserLastName AS UserName, VisitPurpose, NoOfPhysicians, NoOfRecords 
FROM KHSSQLODSPRD.QI_SITE_REVIEW.dbo.RecordHeader 
WHERE ExportDate BETWEEN @START_DATE AND @END_DATE
ORDER BY ReviewDate


SET @RECORD_ID = 1
SET @MAX_RECORD_ID = (SELECT ISNULL(MAX(RECORD_ID), 0) FROM #REVIEWS)


    WHILE @RECORD_ID <= @MAX_RECORD_ID 
    BEGIN

        SELECT @REVIEW_ID = ReviewID, 
                @SUBJECT = 'Review - ' + ProviderName, 
                @COMMENT = CAST(@RECORD_ID AS VARCHAR(10)) + ' of ' + CAST(@MAX_RECORD_ID AS VARCHAR(10)) 
                            + ' Review Surveys for the week ending ' + RTRIM(CONVERT(VARCHAR(12), @END_DATE, 110)) + '.' 
                            + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10) + ProviderName + ' [' + SiteIDNo + '] - Reviewed ' + RTRIM(CONVERT(VARCHAR(12), ReviewDate, 110)) 
                            + '. Review ID ' + ReviewID  
                            + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10) + VisitPurpose + ' visit for ' + CAST(CAST(NoOfPhysicians AS INT) AS VARCHAR(20)) + ' physicians with ' 
                            +  CAST(CAST(NoOfRecords AS INT) AS VARCHAR(20)) + ' records by ' + UserName + '.'
        FROM #REVIEWS 
        WHERE RECORD_ID = @RECORD_ID


        SET @REVIEW_ID = LOWER(@REVIEW_ID)  

        --SET EXTENSION OPTIONS
        SET @EXTENSION_SETTINGS.modify('replace value of (/ParameterValues/ParameterValue[Name="Subject"]/Value/text())[1] with sql:variable("@SUBJECT")');
        SET @EXTENSION_SETTINGS.modify('replace value of (/ParameterValues/ParameterValue[Name="Comment"]/Value/text())[1] with sql:variable("@COMMENT")');

        --SET REPORT PARAMETERS
        SET @PARAMETERS.modify('replace value of (/ParameterValues/ParameterValue[Name="SITE_ID"]/Value/text())[1] with sql:variable("@REVIEW_ID")');


        --UPDATE SUBSCRIPTION PARAMETERS 
        UPDATE dbo.Subscriptions
        SET ExtensionSettings = CAST(@EXTENSION_SETTINGS AS VARCHAR(8000)), 
            [Parameters]      = CAST(@PARAMETERS AS VARCHAR(8000))
        WHERE SubscriptionID = @SUBSCRIPTION_ID


        EXEC dbo.AddEvent @EventType = 'TimedSubscription', @EventData = @SUBSCRIPTION_ID;


        --WAIT WHILE REPORT GENERATES 
        WAITFOR DELAY '00:00:03.000';

        --IF REPORT IS STILL EXECUTING, WAIT SOME MORE
        WHILE EXISTS (SELECT TOP 1 'X' FROM dbo.Event WHERE EventData = @SUBSCRIPTION_ID ) OR EXISTS (SELECT TOP 1 'X' FROM dbo.Notifications WHERE SubscriptionID = @SUBSCRIPTION_ID)
                        BEGIN
                            WAITFOR DELAY '00:00:01.000';
                            PRINT 'Waiting for subscription to finish'
                        END


        SET @RECORD_ID = @RECORD_ID + 1

    END  --END OF LOOP 

--      SET @FLAG = 1

您将以与注释更新相同的方式添加“收件人”地址。 @Review_ID作为参数传递给报表。