在与 CASE 一起使用 THEN 语句后返回超过 1 个值

Posted

技术标签:

【中文标题】在与 CASE 一起使用 THEN 语句后返回超过 1 个值【英文标题】:return more than 1 value after THEN statement used with CASE 【发布时间】:2012-10-19 03:26:03 【问题描述】:

以下是我的查询:

UPDATE DSOPI_PERSON_ADDR_RULE ADDR
SET ADDR.DEPT_NAME =
  CASE
    WHEN regexp_like (UPPER(addr.src_address_line1),     'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
    AND ADDR.ORG_NAME IS NOT NULL
    THEN ADDR.SRC_ADDRESS_LINE1
    WHEN regexp_like (UPPER(addr.src_address_line2), 'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
    AND ADDR.ORG_NAME IS NOT NULL
    THEN ADDR.SRC_ADDRESS_LINE2
    WHEN regexp_like (UPPER(addr.src_address_line3), 'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
    AND ADDR.ORG_NAME IS NOT NULL
    THEN ADDR.SRC_ADDRESS_LINE3
    WHEN regexp_like (UPPER(addr.src_address_line4), 'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
    AND ADDR.ORG_NAME IS NOT NULL
    THEN ADDR.SRC_ADDRESS_LINE4
    ELSE NULL
 END;

我想做的是,当 case 条件为真时,我想让相应的地址行为 NULL。有没有办法用这段代码实现它?

【问题讨论】:

【参考方案1】:

您可以选择为每个地址字段添加额外的CASE 语句:

代码是重复的,但这允许您在满足条件时将地址行设置为空。

UPDATE DSOPI_PERSON_ADDR_RULE ADDR
SET ADDR.DEPT_NAME =
   CASE
      WHEN regexp_like (UPPER(addr.src_address_line1),
         'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
         AND ADDR.ORG_NAME IS NOT NULL
         THEN ADDR.SRC_ADDRESS_LINE1
      WHEN regexp_like (UPPER(addr.src_address_line2), 
         'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
         AND ADDR.ORG_NAME IS NOT NULL
         THEN ADDR.SRC_ADDRESS_LINE2
      WHEN regexp_like (UPPER(addr.src_address_line3), 
         'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
         AND ADDR.ORG_NAME IS NOT NULL
         THEN ADDR.SRC_ADDRESS_LINE3
      WHEN regexp_like (UPPER(addr.src_address_line4), 
         'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
         AND ADDR.ORG_NAME IS NOT NULL
         THEN ADDR.SRC_ADDRESS_LINE4
      ELSE NULL
   END
   , ADDR.SRC_Address_Line1 =
        CASE
           WHEN regexp_like (UPPER(addr.src_address_line1),     
              'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
              AND ADDR.ORG_NAME IS NOT NULL
              THEN NULL
           ELSE ADDR.SRC_Address_Line1
        END
   , ADDR.SRC_Address_Line2 =
        CASE
           WHEN regexp_like (UPPER(addr.src_address_line2),     
              'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
              AND ADDR.ORG_NAME IS NOT NULL
              THEN NULL
           ELSE ADDR.SRC_Address_Line2
        END
   , ADDR.SRC_Address_Line3 =
        CASE
           WHEN regexp_like (UPPER(addr.src_address_line3),      
              'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
              AND ADDR.ORG_NAME IS NOT NULL
              THEN NULL
           ELSE ADDR.SRC_Address_Line3
        END
   , ADDR.SRC_Address_Line4 =
        CASE
           WHEN regexp_like (UPPER(addr.src_address_line4),     
              'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
              AND ADDR.ORG_NAME IS NOT NULL
              THEN NULL
           ELSE ADDR.SRC_Address_Line4
        END;

【讨论】:

只是想一想:这种方法执行查询不会花费两倍的时间吗? 您必须在您的环境中进行测试。我通常在 SQL Server 中,并且类似的操作(ORDER BY 中的 CASE 语句)不会在查询中多次计算,所以我希望 Oracle 的行为类似。【参考方案2】:

所以如果第一个 WHEN 条件为真,您想将 src_address_line1 设置为 NULL,等等?在这种情况下,我会这样做:

UPDATE DSOPI_PERSON_ADDR_RULE ADDR

SET 
addr.src_address_line1 = 
CASE WHEN regexp_like (UPPER(addr.src_address_line1), 'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG') 
AND ADDR.ORG_NAME IS NOT NULL 
THEN NULL ELSE addr.src_address_line1 END, 

addr.src_address_line2 = 
CASE WHEN regexp_like (UPPER(addr.src_address_line2), 'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG') 
AND ADDR.ORG_NAME IS NOT NULL 
THEN NULL ELSE addr.src_address_line2 END, 

addr.src_address_line3 = 
CASE WHEN regexp_like (UPPER(addr.src_address_line3), 'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG') 
AND ADDR.ORG_NAME IS NOT NULL 
THEN NULL ELSE addr.src_address_line3 END, 

addr.src_address_line4 = 
CASE WHEN regexp_like (UPPER(addr.src_address_line4), 'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG') 
AND ADDR.ORG_NAME IS NOT NULL 
THEN NULL ELSE addr.src_address_line4 END;  

【讨论】:

以上是关于在与 CASE 一起使用 THEN 语句后返回超过 1 个值的主要内容,如果未能解决你的问题,请参考以下文章

TSQL:SELECT CASE WHEN THEN 子查询:错误:子查询返回超过 1 个值

SQL之case when

case 语句不返回所有值

case语句返回null postgresql时处理空值

SQL语句中case,when,then的用法

oracle中的条件语句