【问题标题】:06502. 00000 - "PL/SQL: numeric or value error%s"06502. 00000 - “PL/SQL:数字或数值错误%s”
【发布时间】:2021-06-17 07:10:51
【问题描述】:

您好,我有一项任务要解决。我写了这段代码

CREATE OR REPLACE function hr_funct_task4(p_emp_id number)
  return varchar
  as
    v_emp_last_name varchar2(25);
    v_count         number;
    no_emp_id       exception;
    null_emp_id     exception;
  begin

    if p_emp_id is null
    then
      raise no_emp_id;
    else
      select count(*)
        into v_count
          from jobs j
            inner join  employees e
            on e.job_id = j.job_id
        where j.min_salary > 10000
          and e.employee_id = p_emp_id;

      if v_count <> 0
      then
        select e.last_name
          into v_emp_last_name
          from jobs j
            inner join  employees e
              on e.job_id = j.job_id
          where j.min_salary > 10000
            and e.employee_id = p_emp_id;
      else
        v_emp_last_name := 'No emp_id has salsry > 10000';
      end if;
    end if;

    return v_emp_last_name;

  exception
    when no_emp_id then raise_application_error(-20001, 'No emp found! Please, input another emp_id.');
    when null_emp_id then raise_application_error(-20001, 'Input emp_id is null. Please, input not null value.');
  end;

调用函数:

begin
  for i in
  (
    select hr_funct_task4(e.employee_id) as last_name
      from employees e
  )
  loop
    dbms_output.put_line(i.last_name);
  end loop;
end;

我收到了正确的值,但也收到了错误 06502。00000 - "PL/SQL: numeric or value error%s", ORA-06512: on line 4.

你能告诉我我做错了什么吗?谢谢

【问题讨论】:

  • 通常应该有一个错误堆栈显示行号。你是从 SQL Developer 运行它吗?我的猜测是v_emp_last_name varchar2(25) 不够大。将其声明为employees.last_name%type 会更安全。
  • @Wiliam - 是的,我正在使用 sql developer。我试过你的建议,但我仍然收到同样的错误
  • @Wiliam - 它显示 ORA-06512: 在第 4 行。
  • 尝试将return varchar也转换为return employees.last_name%type
  • 此错误代码可以在旧的Oracle's 9i documentation 中找到:发生了算术、数字、字符串、转换或约束错误。例如,如果尝试将值 NULL 分配给声明为 NOT NULL 的变量,或者尝试将大于 99 的整数分配给声明为 NUMBER(2) 的变量,则会发生此错误。 您需要在整个数据流中检查这种情况(每个步骤中哪个变量/列的类型以及进入它们的值)

标签: oracle plsql error-handling stored-functions ora-06512


【解决方案1】:

你声明

v_emp_last_name varchar2(25);

后来修改为

v_emp_last_name employees.last_name%type;

(我猜last_name 列不超过 25 个字符,是吗?)


那么,这就是你要做的:

 else
    v_emp_last_name := 'No emp_id has salsry > 10000';

你猜怎么着?

SQL> select length('No emp_id has salsry > 10000') from dual;

LENGTH('NOEMP_IDHASSALSRY>10000')
---------------------------------
                               28         --> 28 won't fit into 25

SQL>

【讨论】:

    猜你喜欢
    • 2014-04-25
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2021-12-26
    • 2014-12-30
    相关资源
    最近更新 更多