where子句中的多个选择

时间:2017-03-20 18:56:05

标签: sql-server

我尝试了IIF,IF Else和Case似乎没有用。如果@ShippedDate中有值,则单独查询该值或查询其他值。

 WHERE 
    (CASE WHEN @ShippedDate  != NULL THEN date_shipped = @ShippedDate 
     ELSE   
    [order].order_date BETWEEN @StartDate AND @EndDate
    AND program_types.id IN ( @ProgTypes )
    AND employee.id IN (@RepNames)
    END
    ORDER BY OrderID

1 个答案:

答案 0 :(得分:0)

非常确定这样的东西就是你要找的东西。但是,我不认为你实际上是在这里完成的。我注意到你使用标量值IN。如果这些值包含以逗号分隔的列表,则仍然无效。

WHERE 
(
    date_shipped = @ShippedDate --This will not be true if @ShippedDate is null
    OR
    (
        @ShippedDate is null
        AND
        [order].order_date BETWEEN @StartDate AND @EndDate
    )
)
AND program_types.id IN ( @ProgTypes )
AND employee.id IN (@RepNames)
ORDER BY OrderID