Dynamically create filename in SSRS 2008

后端 未结 5 2079
南旧
南旧 2021-02-20 03:14

I have been asked a couple times to change the file name dynamically in SSRS 2008. Example: ReportName_201101.RDL. The 201101 represents the execution date. Can this be accom

5条回答
  •  别跟我提以往
    2021-02-20 03:30

    Another work around, is to rename the report before it runs automatically. This is a diamond in the rough. This may only work for reports that are subscriptions and not ones that users link back to. Create a table on the ReportServer database that contains a listing of all reports that you want to rename before they execute. Table Report_Rename_Listing RenameID int ItemID uniqueidentifier OriginalReportName nvarchar(350) DateType nvarchar(75) Format int DatePlusMinus real Create a stored procedure on the same server that goes out and changes all the reports in the above table.

    Create Procedure [dbo].[RenameReports]
    AS
    SET NOCOUNT OFF ; 
      Update dbo.Catalog 
        Set Name = ISNULL(( Select OriginalReportName + '_' + 
                   dbo.func_SetupRenameOfReports(DateType, Format, DatePlusMinus)
          From dbo.DDC_Report_Rename r
          Where r.ItemID = c.ItemID), Name)  
      From dbo.Catalog c
    return (0)
    

    Create a scalar function on same server that figures out just how you want to rename the report.

    Create Function [dbo].[func_SetupRenameOfReports]
    ( @DateType nvarchar(75), @Format int, @PlusMinus real  )
    RETURNS nvarchar(75)
    AS
    BEGIN
      Declare @FirstMonth datetime, @LastMonth datetime
      Declare @OutputFormat nvarchar(75)
    
      Set @FirstMonth = CONVERT(datetime, Convert(varchar(2), DateAdd(mm, @PlusMinus, GetDate()), 103) + '/1/' + CONVERT(varchar(4), DateAdd(mm, @PlusMinus, GetDate()), 102))
      Set @LastMonth = DATEADD(dd, -1, DateAdd(mm, 1, @FirstMonth))
    
      Set @OutputFormat = 
        Case When @DateType = 'CurrentDate' Then Convert(varchar(75), DateAdd(dd, @PlusMinus, GetDate()), @Format)
             When @DateType = 'CurrentDayName' Then CONVERT(varchar(75), DateName(dw, DateAdd(dd, @PlusMinus, GetDate())))
             When @DateType = 'CurrentMonthName' Then CONVERT(varchar(75), DateName(mm, DateAdd(mm, @PlusMinus, GetDate())))
             When @DateType = 'CurrentYear' Then CONVERT(varchar(75), DateAdd(yy, @PlusMinus, GetDate()))
             When @DateType = 'CurrentBeginEndMonth' Then CONVERT(varchar(10), @FirstMonth, @Format) + '-' + CONVERT(varchar(10), @LastMonth, @Format)
        End
    
      If @OutputFormat IS null 
        Begin 
          Set @OutputFormat = ''
        End
     Return @OutputFormat
    
    END
    

    Then setup this up to have the stored procedure automatically run daily on your server. I have it run just after midnight every day.

提交回复
热议问题