简体   繁体   English

SQL查询中的SSRS报告参数处理

[英]SSRS Report Parameter handling in SQL Query

I have a SSRS report where I pass start date and end date to filter my data. 我有一个SSRS报告,可以在其中传递开始日期和结束日期以过滤数据。 I need to get those records having data between passed date values.I have query as below.. 我需要获取那些在传递的日期值之间有数据的记录。我有如下查询..

SELECT * FROM Sample s WHERE s.RecStartDate >= ISNULL(@StartDate,s.RecStartDate)
AND s.RecEndDate <= ISNULL(@EndDate,s.RecEndDate)

Here I have to handle 'NULL' and data is from two different fields. 在这里,我必须处理“ NULL”,并且数据来自两个不同的字段。 But the problem is, in my SSRS report default value for those two dates is 'TODAY'. 但是问题是,在我的SSRS报告中,这两个日期的默认值为“今天”。 When I choose @StartDate as 'TODAY' and @EndDate as 10/12/2010, it will never work as per above condition. 当我选择@StartDate作为'TODAY'和@EndDate作为10/12/2010时,它将无法按照上述条件工作。 Please help me solve this. 请帮我解决这个问题。

Thanks, Su. 谢谢你苏

Create the following procedure and use this stored procedure in your report's dataset. 创建以下过程,并在报表的数据集中使用此存储过程。

CREATE PROCEDURE dbo.SomeReportProc
@StartDate DATETIME,
@EndDate   DATETIME
AS
BEGIN
  SET NOCOUNT ON;

   DECLARE @Sql NVARCHAR(MAX);

 SET @Sql = N'SELECT * FROM Sample s WHERE 1 = 1 '
            + CASE WHEN @StartDate IS NOT NULL 
                  THEN N' AND s.RecStartDate >= @StartDate ' ELSE N'' END
            + CASE WHEN @EndDate IS NOT NULL 
                  THEN N' AND s.RecEndDate <= @EndDate '     ELSE N'' END

   EXECUTE sp_executesql @Sql
                        ,N'@StartDate DATETIME, @EndDate DATETIME'
                        ,@StartDate
                        ,@EndDate

END

Note 注意

Also make sure you select the parameter type to be datetime so when the users enter the date they use that nice GUI for selecting dates rather than inserting dates in different formats . 还要确保将参数类型选择为datetime以便当用户输入日期时,他们使用该漂亮的GUI来选择日期,而不是以不同的格式插入日期。

Datetime Parameter ON report 日期时间参数开启报告

在此处输入图片说明

Datetime Parameter Properties 日期时间参数属性

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM