【发布时间】:2011-03-21 15:09:09
【问题描述】:
SSRS 参数很麻烦。通过允许用户访问许多不同的参数并使它们成为可选参数,我希望能够针对许多不同的需求重复使用报告。
所以,如果我从以下代码开始:
Select * from mytable myt
where myt.date between '1/1/2010' and '12/31/2010'
and year(myt.date) = '2010'
and myt.partnumber = 'XYZ-123'
我希望这些参数是可选的,所以我的第一次尝试是让参数默认为 null,例如:
and (myt.partnumber = (@PartNumber) or (@PartNumber) is null)
这有问题,因为如果有问题的数据库字段可以为空,那么您将删除记录,因为 null 不等于 null。
然后我使用了这样的代码:
DECLARE @BeginDate AS DATETIME
DECLARE @EndDate AS DATETIME
DECLARE @PartNumber AS VARCHAR(25)
SET @Year = '..All'
SET @BeginDate = '1/1/2005'
SET @EndDate = '12/31/2010'
SET @PartNumber = '..All'
SET @Year = '..All'
Select * from mytable myt
where (myt.date between (@BeginDate) and (@EndDate))
and (year(myt.date) = (@Year) or (@Year) = '..All' )
and (myt.partnumber = (@PartNumber) or (@PartNumber) = '..All')
这不起作用,因为 Year(myt.date) 是整数,而 @Year 不是。
所以,这是我的问题。
- 我怎样才能让我的日期成为可选的?将它们简单地默认为实际范围之外的日期以便我返回所有值的最佳方法是什么?
- 处理 null 或 '..All' 选项以使我的查询尽可能具有可读性并允许我的用户为大多数数据类型使用可选参数的最佳方法是什么?我宁愿不为 使用 null
【问题讨论】:
标签: tsql reporting-services ssrs-2008 reportingservices-2005