创建一个更新员工个人信息的存储过程

时间:2016-11-27 20:54:40

标签: sql oracle stored-procedures plsql

我已完成以下操作来创建一个存储过程,用于更新EMPLOYEE表中员工的个人信息:

CREATE OR REPLACE PROCEDURE UPDATE_EMPLOYEE 
(
  THE_EMPLOYEEID IN NUMBER,
  NEW_LASTNAME IN VARCHAR2,
  NEW_FIRSTNAME IN VARCHAR2,
  NEW_TITLE IN VARCHAR2,
  NEW_REPORTSTO IN NUMBER,
  NEW_BIRTHDATE IN DATE,
  NEW_HIREDATE IN DATE,
  NEW_ADDRESS IN VARCHAR2,
  NEW_CITY IN VARCHAR2,
  NEW_STATE IN VARCHAR2,
  NEW_COUNTRY VARCHAR2,
  NEW_POSTALCODE VARCHAR2,
  NEW_PHONE VARCHAR2,
  NEW_FAX VARCHAR2,
  NEW_EMAIL VARCHAR2
)
AS 
BEGIN
  UPDATE EMPLOYEE
  SET LASTNAME = 
        CASE NEW_LASTNAME 
        WHEN NULL THEN 
          LASTNAME
        ELSE NEW_LASTNAME
        END,
      FIRSTNAME =
        CASE NEW_FIRSTNAME 
        WHEN NULL THEN 
          FIRSTNAME
        ELSE NEW_FIRSTNAME
        END,
      TITLE = NEW_TITLE,
      REPORTSTO = NEW_REPORTSTO,
      BIRTHDATE = NEW_BIRTHDATE,
      HIREDATE = NEW_HIREDATE,
      ADDRESS = NEW_ADDRESS,
      CITY = NEW_CITY,
      STATE = NEW_STATE,
      COUNTRY = NEW_COUNTRY,
      POSTALCODE = NEW_POSTALCODE,
      PHONE = NEW_PHONE,
      FAX = NEW_FAX,
      EMAIL = NEW_EMAIL
    WHERE EMPLOYEEID = THE_EMPLOYEEID;
END UPDATE_EMPLOYEE;

表EMPLOYEE中的EMPLOYEEID,LASTNAME和FIRSTNAME不可为空。

我收到此消息:

Connecting to the database ChinookDB.
ORA-01407: cannot update ("CHINOOK"."EMPLOYEE"."LASTNAME") to NULL
ORA-06512: at "CHINOOK.UPDATE_EMPLOYEE", line 21
ORA-06512: at line 34
Process exited.
Disconnecting from the database ChinookDB.

有人可以告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

如果传入new_firstname值,我假设您要忽略new_lastnameNULL参数。如果是这样,您的case语句错误

CASE NEW_LASTNAME 
        WHEN NULL THEN 
          LASTNAME
        ELSE NEW_LASTNAME
        END,

将始终返回new_lastname。包括NULL在内的任何值都不等于NULL,因此when null将始终评估为false。你可以做点什么

CASE WHEN new_lastname IS NULL
     THEN lastname
     ELSE new_lastname
 END

或更简单

COALESCE( new_lastname, lastname )

当然,new_firstname表达式存在同样的问题。