首页 > 解决方案 > 该过程引发错误“无法在“CUSTOMER”中插入空值。“CUST_ID”虽然值不为空

问题描述

似乎我的程序不接受我试图插入到客户表中的值。但为什么?列有什么问题cust_id

BEGIN

do_new_customer('650707-1111', 'Tito', 'Ortiz', 'qwerTY');

do_new_customer('560126-1148', 'Margreth', 'Andersson', 'olle85');

do_new_customer('840317-1457', 'Mary', 'Smith', 'asdfgh');

do_new_customer('861124-4478', 'Vincent', 'Ortiz', 'qwe123');

COMMIT;

END;

这是我的程序:

create or REPLACE procedure do_new_customer
    (p_cust_id in varchar2, 
     p_first_name in varchar2, 
     P_last_name in varchar2, 
     P_passwd in varchar2)
as     
     v_cust_id number(6);    
begin     
    insert into Customer (cust_id, first_name, last_name, passwd)    
    values (v_cust_id, P_First_name, P_Last_name, P_passwd);    

    Commit;
end;

标签: sqlplsql

解决方案


该值为空:我想你忘记了将 p_cust_id 转换为数字并将其存储在 v_cust_id 中的步骤

create or REPLACE procedure do_new_customer
(p_cust_id in varchar2, p_first_name in varchar2, P_last_name in varchar2, P_passwd in varchar2)
as   
  --YOU DIDN'T SET THIS TO A VALUE SO IT'S NULL  
  v_cust_id number(6);   

begin     
  insert into Customer( cust_id, first_name, last_name, passwd)    

  --STILL NULL WHEN YOU CAME TO USE IT
  values(v_cust_id, P_First_name,P_Last_name,P_passwd);    
Commit;

end;

尝试 putSET v_cust_id = p_cust_id::int;或类似操作将 varchar 转换为大数字,并给 v_cust_num 一个值?

或者如 Tim 所说,直接将其作为 p_cust_id 传递,让 Postgres 算出转换。

或者使 p_cust_id 匹配表中列的类型,并一起摆脱 v_cust_id


推荐阅读