为什么OR运算符不在CASE语句中工作?

时间:2017-10-05 07:39:30

标签: sql sql-server sql-server-2008 tsql stored-procedures

我一直试图在CASE语句中添加 OR 运算符,但它不起作用。

;WITH CTE
     AS (
     SELECT Parent_Id,
            Child_ID,
            C1_PID,
            C2_PID
     FROM #TM
     UNION
     SELECT Parent_Id,
            NULL Child_ID,
            NULL C1_PID,
            NULL C2_PID
     FROM #TM),
     CTE1
     AS (
     SELECT Parent_Id,
            Child_ID,
            C1_PID,
            C2_PID,
            ROW_NUMBER() OVER(PARTITION BY Parent_Id ORDER BY Parent_Id) RN
     FROM CTE)
     SELECT CASE
                WHEN RN > 1
                THEN CONVERT(VARCHAR(MAX), '')
                ELSE CONVERT(VARCHAR(MAX), Parent_Id)
            END AS Parent_Id,
            ISNULL(Child_ID, '') Child_ID,
            ISNULL(C1_PID, '') C1_PID,
            ISNULL(C2_PID, '') C2_PID
     FROM CTE1;

我想检查是否有任何IsAdmin或IsChief工程师,或者他们是1然后组织其他@OrganizationID

3 个答案:

答案 0 :(得分:2)

几乎只是改变你的案例陈述:

CASE
WHEN @IsAdmin = 1  OR @IsChiefEngineer = 1 
THEN W.Organization_ID 
ELSE @OrganizationID 
END

答案 1 :(得分:1)

您可以按如下方式编写:

Where W.Organization_ID = CASE 
    WHEN @IsAdmin = 1 OR @IsChiefEngineer = 1 
        THEN W.Organization_ID 
    ELSE @OrganizationID END

答案 2 :(得分:0)

您应该测试OR运算符“内部”的每个参数

    CASE 
       WHEN @IsAdmin = 1 OR @IsChiefEngineer = 1 
       THEN W.Organization_ID 
       ELSE @OrganizationID 
    END