可选提示的T-SQL问题

时间:2016-06-02 09:55:13

标签: sql-server tsql

我在尝试将以下MPAN和TimePeriodStart作为必需参数时遇到问题,其余部分是可选的。但是,当我运行提示时,没有结果通过。我在这里做错了吗?

alter procedure task2assigned 
(
@MPAN varchar(13),
@TimePeriodStart datetime = null,
@TimePeriodEnd datetime = null,
@BSCValidation varchar(1) = null,
@ReadType varchar(1) = null
)

A

SELECT * FROM TABLES

以下是整个查询,问题在于参数:

where a.J0003 = (@MPAN)
and d.J0016 >= DateAdd(mm,-36,@TimePeriodStart)
and (@TimePeriodEnd is null or d.J0016 <= DateAdd(mm,-36,@TimePeriodEnd))
and (@BSCValidation = null or d.J0022 = @BSCValidation)
and (d.J0171 is null or d.J0171 = @ReadType)

1 个答案:

答案 0 :(得分:0)

为什么你突然把 @BSCValidation = null ... null无法使用=符号进行比较....无论如何,脚本可能会帮助你

编辑1 :正如您在评论中提到的那样..

alter procedure task2assigned
(
@MPAN varchar(13),
@TimePeriodStart datetime = DATEADD(year,3,GETDATE()),
@TimePeriodEnd datetime = GETDATE(),
@BSCValidation varchar(1) = null,
@ReadType varchar(1) = null
)

AS
BEGIN

   SELECT * FROM TABLES
   WHERE a.J0003 = @MPAN
   AND (@TimePeriodStart IS NULL OR d.J0016 >= DateAdd(mm,-36,@TimePeriodStart))
   AND (@TimePeriodEnd IS NULL OR d.J0016 <= DateAdd(mm,-36,@TimePeriodEnd))
   AND (@BSCValidation IS NULL OR d.J0022 = @BSCValidation)
   AND (@ReadType IS NULL  OR ISNULL(d.J0171,@ReadType) = @ReadType)

END