0

我正在尝试根据变量的值添加 CASE 语句:

WHERE DTH.PointPerson LIKE @userParam
    AND ActivityDate >= @startDateParam
    AND ActivityDate <= @endDateParam

    --new section below
    AND CASE (@orgTeamPK)
    WHEN '%' THEN
     (USR.[OrganizationalTeamId] LIKE @orgTeamPK OR USR.[OrganizationalTeamId] IS NULL)
     ELSE
     (USR.[OrganizationalTeamId] LIKE @orgTeamPK)

但是我的语法似乎关闭了。我如何实现上面想要做的事情?

4

2 回答 2

1

你不需要case stmt。

    WHERE DTH.PointPerson LIKE @userParam
        AND ActivityDate >= @startDateParam
        AND ActivityDate <= @endDateParam
        AND (
            (@orgTeamPK = '%' AND (USR.[OrganizationalTeamId] LIKE @orgTeamPK OR USR.[OrganizationalTeamId] IS NULL) )
            OR
            (@orgTeamPK <> '%' AND (USR.[OrganizationalTeamId] LIKE @orgTeamPK ) )
于 2013-11-04T19:40:10.107 回答
1

正如@Anup 所提到的,您不需要case 语句,但恕我直言,使用 case 使语法更清晰...... where 子句中的每个部分都必须是“PREDICATE”,即它必须评估为布尔值。where 子句中的 case 语句不会计算为布尔值。要修复,请按如下方式更改:

 WHERE DTH.PointPerson LIKE @userParam
   AND ActivityDate >= @startDateParam
   AND ActivityDate <= @endDateParam

   --new section below
   AND USR.OrganizationalTeamId Like 
        CASE (@orgTeamPK) WHEN '%' THEN coalesce(@orgTeamPK, USR.OrganizationalTeamId)
              ELSE @orgTeamPK end
于 2013-11-04T19:41:06.357 回答