0

I'm new to Oacle SQL and I'm trying to get my head around what seems to be something easy...

Say I have a select like so:

select * from MRCONTRACT2
WHERE CASE 
        WHEN ("MR_CONTRACT2"."TERM_DATE" < sysdate)
            THEN 'Lapsed Contract'
        WHEN ("MR_CONTRACT2"."EFF_DATE" > sysdate)
            THEN 'Inactive Contract'
        ELSE 'Active Contract'
        END = 'Active Contract'

What does the where clause actually mean, as to me it does not make a full 'if' expression?

4

1 回答 1

3

该查询相当于

select *
  from mrcontract2
 where term_date >= sysdate
   and eff_date <= sysdate

CASE 语句是按顺序计算的,所以如果任何“case”为真,那么它后面的每个 case 都会被忽略。当您检查 case 语句的结果是否与 ELSE 子句相同时,该语句与所有其他条件的相反。

我不太喜欢 WHERE 子句中的 CASE 语句,但它可以用作简化逻辑的一种方式;有一个复杂的 CASE 语句,您想对其进行评估但不想翻译。

于 2013-10-20T12:37:06.387 回答