亲宝软件园·资讯

展开

ORA-06512数字或值错误字符串缓冲区太小异常详解

=PNZ=BeijingL 人气:0

ORA-06512 网上最容易查到的解释为

Cause

This error is caused by the stack being unwound by unhandled exceptions in your PLSQL code.

The options to resolve this Oracle error are:

  • Fix the condition that is causing the unhandled error.
  • Write an exception handler for this unhandled error.
  • Contact your DBA for help.

The ORA-06512 error message indicates the line number of the unhandled error in the PLSQL code. This is quite useful when troubleshooting.

举个栗子来了解此异常

创建存储过程TestProc,参数v_number是一个长度为2的数字, 存储过程中将100赋值给v_number,执行存储过程后提示异常,ORA-06502表示发生的错误;ORA-06512表示发生的行数,本例中 因为100的是3位数字, v_number只能处理2位数字

SQL> CREATE OR REPLACE PROCEDURE TestProc AS
  2    v_number number(2);
  3  BEGIN
  4    v_number := 100;
  5  END;
  6  /
 
Procedure created
 
SQL> execute TestProc();
 
begin TestProc(); end;
 
ORA-06502: PL/SQL: 数字或值错误 : number precision too large
ORA-06512: 在 "BOSS643.TESTPROC", line 4
ORA-06512: 在 line 2

 针对上述问题,可以重新定义数值长度解决这个问题, v_number定义为3

SQL> CREATE OR REPLACE PROCEDURE TestProc AS
  2    v_number number(3);
  3  BEGIN
  4    v_number := 100;
  5  END;
  6  /
 
Procedure created
 
SQL> execute TestProc();
 
PL/SQL procedure successfully completed
 
SQL> 

如果将数值型修改成其他类型后也是同样的, 例如字符串,v_str设置为处理长度为10的字符串, 当给v_str赋值大于长度10的字符串后, 提示数字或者值错误,符串缓冲区太小

SQL> CREATE OR REPLACE PROCEDURE TestProc2 AS
  2    v_str varchar2(10);
  3  BEGIN
  4    v_str := 'This is a test string';
  5  END;
  6  /
 
Procedure created
 
SQL> execute TestProc2();
 
begin TestProc2(); end;
 
ORA-06502: PL/SQL: 数字或值错误 : character string buffer too small
ORA-06512: 在 "BOSS643.TESTPROC2", line 4
ORA-06512: 在 line 2

重新定义varchar2长度

SQL> CREATE OR REPLACE PROCEDURE TestProc2 AS
  2    v_str varchar2(512);
  3  BEGIN
  4    v_str := 'This is a test string';
  5  END;
  6  /
 
Procedure created
 
 
SQL> execute TestProc2();
 
PL/SQL procedure successfully completed
 
SQL> 

当然你也可以自定义异常来处理,例如当出现异常后提示“数值越界” ,此方法将异常捕获提示更加明确,个人认为ORA-06502异常已经十分清楚了, 具体还看存储过程对应的需求是否有明确提示需求

SQL> CREATE OR REPLACE PROCEDURE TestProc AS
  2    v_number number(2);
  3  BEGIN
  4    v_number := 100;
  5  EXCEPTION
  6    WHEN OTHERS THEN
  7      RAISE_APPLICATION_ERROR(-20001, '数值v_number越界');
  8  END;
  9  /
 
Procedure created
 
SQL> exec TestProc();
 
begin TestProc(); end;
 
ORA-20001: 数值v_number越界
ORA-06512: 在 "BOSS643.TESTPROC", line 7
ORA-06512: 在 line 2

相关资源:

Oracle Database Documentation - Oracle Database

PL/SQL User's Guide and Reference -- Contents

总结

加载全部内容

相关教程
猜你喜欢
用户评论