我有一个带有两个日期/时间参数的报告服务(SQL 2008)报告 - begindate和enddate.我需要将enddate限制在相同的月份和年份.这似乎应该是一件容易的事情,但我无法弄明白.
目前,我正在检查传递给存储过程的参数,如果两个日期时间参数不在同一个月和一年,则会引发错误.我正在寻找一种更优雅的方式来实现这一目标.
您可以在参数表达式中检查EndDate值,如果不正确,请将其设置为StartDate + 1 Month.
就像是:
= IIF(DateDiff(DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) = 0, Parameters!EndDate.Value, AddDate(DateInterval.Month, 1, Parameters!StartDate.Value))
如果您只想要通知用户,可以放置一些带有适当格式(红色大字体)的隐藏文本框和有关日期参数不正确范围的消息.在隐藏表达式集中
= (DateDiff(DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) <> 0)
此外,您可以将这两个操作与自定义代码组合:
Public DateMessage As String
Public Function ValidateDate(StartDate As DateTime, EndDate As DateTime) As DateTime
Dim ResultDate As DateTime
If (DateDiff(DateInterval.Month, StartDate, EndDate) <> 0) Then
ResultDate = AddDate(DateInterval.Month, 1, StartDate)
DateMessage = String.Format("End Date parameter value {0}
was out of range and was changed to {1}", EndDate, ResultDate)
Else
ResultDate = EndDate
End If
End Function
然后,在参数值表达式中:
= Code.ValidateDate(Parameters!StartDate.Value, Parameters!EndDate.Value)
在tbDateParameterMessage文本框的Value属性中:
= Code.DateMessage
并在隐藏属性表达式中:
= String.IsNullOrEmpty(Code.DateMessage)
编辑 但是如果要停止报告运行,请使用此自定义代码:
Public Function CheckDate(SDate as Date, EDate as Date) as Integer
Dim msg as String
msg = ""
If (SDate > EDate) Then
msg="Start Date should not be later than End Date"
End If
If msg <> "" Then
MsgBox(msg, 16, "Parameter Validation Error")
Err.Raise(6,Report) 'Raise an overflow
End If
End Function
它取自SQLServerCentral论坛.