sql中的where子句

时间:2012-04-05 12:15:33

标签: sql sql-server

我的sql查询如下

IF @StatusId = 10
    BEGIN
        SELECT 
            *
        FROM 
        Risk AS R
        INNER JOIN Statuses AS St ON R.Status_Id=St.Status_Id
        WHERE
        R.MitigationOwner = COALESCE(@MitigationOwner,R.MitigationOwner)
        AND R.RiskFactor = COALESCE(@RiskFactor,R.RiskFactor)
        AND R.RiskArea = COALESCE(@RiskArea,R.RiskArea)
        AND R.AddedWhen BETWEEN 
        COALESCE(CONVERT(DATETIME, @StartDate+'00:00:00',120),R.AddedWhen) AND 
        COALESCE(CONVERT(DATETIME,@EndDate+'23:59:59',120),R.AddedWhen)
    END 

当我只传递状态Id并且所有其他变量都为null时,不会显示包含NULL MitigationOwner或ModifiedDate的记录。 这个查询有什么问题?

3 个答案:

答案 0 :(得分:4)

使用表格:

...
(R.MitigationOwner = @MitigationOwner OR  @MitigationOwner IS NULL)
...

这在SQL Server中进行了优化。 COALESCE不是。

编辑:这与Paul Williams的回答相同,但他的回答允许明确的“NULL = NULL”匹配。 m ylogic更简单,因为NULL永远不等于NULL。

答案 1 :(得分:1)

我相信通过ModifiedDate你的意思是R.AddedWhen

试试这个:

SELECT 
            *
        FROM 
        Risk AS R
        INNER JOIN Statuses AS St ON R.Status_Id=St.Status_Id
        WHERE
        (R.MitigationOwner = COALESCE(@MitigationOwner,R.MitigationOwner) OR R.MitigationOwner IS NULL)
        AND R.RiskFactor = COALESCE(@RiskFactor,R.RiskFactor)
        AND R.RiskArea = COALESCE(@RiskArea,R.RiskArea)
        AND (R.AddedWhen BETWEEN 
        COALESCE(CONVERT(DATETIME, @StartDate+'00:00:00',120),R.AddedWhen) AND 
        COALESCE(CONVERT(DATETIME,@EndDate+'23:59:59',120),R.AddedWhen) OR R.AddedWhen IS NULL)

答案 2 :(得分:1)

如果R.MitigationOwner可以为null,那么您的比较子句:

WHERE
R.MitigationOwner = COALESCE(@MitigationOwner,R.MitigationOwner) 

必须重写以处理NULL值:

WHERE
((R.MitigationOwner IS NULL AND @MitigationOwner IS NULL)
 OR (R.MitigationOwner = @MitigationOwner))

请参阅this article on Wikipedia about NULL