Case语句错误:在期望条件的上下文中指定的非布尔类型的表达式

时间:2013-07-29 14:57:16

标签: sql-server case

我无法弄清楚此代码的问题是什么。在我在Where子句中添加Case语句之前,查询工作正常,但是当我添加它时,我收到了该错误。如果有人能帮助我,我会非常感激!!

以下是案例陈述

CASE 
    WHEN (GETDATE()< '2013-06-30 00:00:00.000' AND GETDATE() >= '2013-04-01 00:00:00.000') THEN 
        CASE WHEN (TargetStartDt >= '2013-04-01 00:00:00.000' AND TargetStartDt < '2013-06-30 00:00:00.000') THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)')
        END 
    WHEN (GETDATE() < '2013-09-30 00:00:00.000' AND GETDATE() >= '2013-07-01 00:00:00.000') THEN 
        CASE WHEN TargetStartDt < '2013-06-30 00:00:00.000' THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)' OR JobStatus = 'Closed(Not Filled)')
             WHEN (TargetStartDt >= '2013-07-01 00:00:00.000' AND TargetStartDt < '2013-09-30 00:00:00.000') THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)')
        END  
    WHEN GETDATE() < '2013-12-31 00:00:00.000' AND GETDATE()>= '2013-10-01 00:00:00.000' THEN
        CASE WHEN TargetStartDt < '2013-09-30 00:00:00.000' THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)' OR JobStatus = 'Closed(Not Filled)')
             WHEN (TargetStartDt >= '2013-10-01 00:00:00.000' AND TargetStartDt < '2013-12-31 00:00:00.000')  THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)')
        END 
    --ELSE (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)' OR JobStatus = 'Closed(Not Filled)')
END

非常感谢!!

2 个答案:

答案 0 :(得分:2)

CASE是一个返回单值的表达式。它不能用于布尔值或流量控制逻辑。所以你不能说:

THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)'
   OR JobStatus = 'Closed(Not Filled)')

您需要以不同方式构造此子句。我不会尝试对整个查询进行反向工程,但也许不是嵌套CASE它应该是单个查询:

CASE WHEN (date range checks) AND 
  (JobStatus IN ('Approved', 'Closed(Filled)', 'Closed(Not Filled)')) 
THEN ...

答案 1 :(得分:0)

这看起来像你的问题:

THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)' OR JobStatus = 'Closed(Not Filled)')

这种类型的条款发生在几个地方。我不确定你要做什么,但布尔表达式不应该遵循THEN