Oracle SQL - 仅更新过程非空值

时间:2015-02-26 19:45:17

标签: sql oracle-sqldeveloper

我有一个简单的更新程序。因此,我希望更新字段,而不是空值。我该怎么办?

  PROCEDURE UpdateCustomerInfo(
      CustomerID                 IN NUMBER,
      CustomerType                 IN VARCHAR2,
      CustomerName        IN VARCHAR2,
      CustomerGender          IN VARCHAR2,
      CustomerBirthday        IN DATE)
  AS
  BEGIN
    UPDATE CUSTOMER_INFO
    SET CUSTOMER_TYPE =CustomerType,
     CUSTOMER_NAME =CustomerName,
     CUSTOMER_GENDER =CustomerGender,
     CUSTOMER_BIRTHDAY =CustomerBirthday
    WHERE CUSTOMER_ID = CustomerID;
    COMMIT;

  END CUSTOMER_INFO

任何人都可以帮助我?

由于

1 个答案:

答案 0 :(得分:4)

UPDATE CUSTOMER_INFO
   SET CUSTOMER_TYPE     = NVL(CustomerType, CUSTOMER_TYPE),
       CUSTOMER_NAME     = NVL(CustomerName, CUSTOMER_NAME),
       CUSTOMER_GENDER   = NVL(CustomerGender, CUSTOMER_GENDER),
       CUSTOMER_BIRTHDAY = NVL(CustomerBirthday, CUSTOMER_BIRTHDAY)
 WHERE CUSTOMER_ID = CustomerID;

应该这样做。如果第一个参数是NVLNULL函数将返回第二个参数。因此,如果CustomerType参数为NULL,则CUSTOMER_TYPE列将更新为具有已有的值。您仍然在进行更新(例如生成重做),但您并未更改数据。