在CASEN使用THEN语句后返回超过1的值

时间:2012-10-19 03:26:04

标签: sql oracle set 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;

我想做的是,当案例条件为真时,我想将各自的地址行设为NULL。有没有办法用这个代码实现它?

2 个答案:

答案 0 :(得分:1)

所以如果第一个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;  

答案 1 :(得分:1)

您拥有的一个选项是为每个地址字段添加其他CASE语句:

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

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;