oerr ora 06502
06502, 00000, "PL/SQL: numeric or value error%s"
// *Cause: An arithmetic, numeric, string, conversion, or constraint error
// occurred. For example, this error occurs if an attempt is made to
// assign the value NULL to a variable declared NOT NULL, or if an
// attempt is made to assign an integer larger than 99 to a variable
// declared NUMBER(2).
// *Action: Change the data, how it is manipulated, or how it is declared so
// that values do not violate constraints.
今天遇到一個錯誤提示:ORA-06502:PL/SQL :numberic or value error: character string buffer too small,一般對應的中文信息為:ORA-06502: PL/SQL: 數字或值錯誤 :字符串緩沖區太小。仔細檢查調試過程中才發現是開發人員定義了一個變量,但是在腳本里面賦予了該變量超過其長度的值。結果就報這個錯誤。我習慣總結每一個遇到的錯誤信息,既有利於學習、總結知識,也方便以后遇到此類問題能夠及時給出解決方法。
如果執行oerr ora 06502命令,沒有提及詳細原因(Cause)以及解決方法(Action)。這個估計是出現這類錯誤的場景太多了的緣故。
$ oerr ora 06502
06502, 00000, "PL/SQL: numeric or value error%s"
// *Cause:
// *Action:
在官方文檔http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/datatypes.htm,我看到了關於ORA-06502的錯誤的一些出現場景。非常有意思。有興趣的最好直接閱讀源文檔。
1: 賦值或插入超過長度的值。
Assigning or Inserting Too-Long Values
If the value that you assign to a character variable is longer than the maximum size of the variable, an error occurs. For example:
1: DECLARE
2:
3: c VARCHAR2(3 CHAR);
4:
5: BEGIN
6:
7: c := 'abc ';
8:
9: END;
10:
11: /
12:
13: Result:
14:
15: DECLARE
16:
17: *
18:
19: ERROR at line 1:
20:
21: ORA-06502: PL/SQL: numeric or value error: character string buffer too small
22:
23: ORA-06512: at line 4
24:
2: 違反了SIMPLE_INTEGER Subtype約束
PLS_INTEGER and its subtypes can be implicitly converted to these data types:
·
· CHAR
·
· VARCHAR2
·
· NUMBER
·
· LONG
All of the preceding data types except LONG, and all PLS_INTEGER subtypes, can be implicitly converted to PLS_INTEGER.
A PLS_INTEGER value can be implicitly converted to a PLS_INTEGER subtype only if the value does not violate a constraint of the subtype. For example, casting the PLS_INTEGER value NULL to the SIMPLE_INTEGER subtype raises an exception, as Example 3-5 shows.
Example 3-5 Violating Constraint of SIMPLE_INTEGER Subtype
1: DECLARE
2:
3: a SIMPLE_INTEGER := 1;
4:
5: b PLS_INTEGER := NULL;
6:
7: BEGIN
8:
9: a := b;
10:
11: END;
12:
13: /
14:
15: Result:
16:
17: DECLARE
18:
19: *
20:
21: ERROR at line 1:
22:
23: ORA-06502: PL/SQL: numeric or value error
24:
25: ORA-06512: at line 5
26:
3: User-Defined Constrained Subtype Detects Out-of-Range Values
Example 3-7 User-Defined Constrained Subtype Detects Out-of-Range Values
1: DECLARE
2:
3: SUBTYPE Balance IS NUMBER(8,2);
4:
5: checking_account Balance;
6:
7: savings_account Balance;
8:
9: BEGIN
10:
11: checking_account := 2000.00;
12:
13: savings_account := 1000000.00;
14:
15: END;
16:
17: /
18:
19: Result:
20:
21: DECLARE
22:
23: *
24:
25: ERROR at line 1:
26:
27: ORA-06502: PL/SQL: numeric or value error: number precision too large
28:
29: ORA-06512: at line 9
30:
4: Implicit Conversion Between Constrained Subtypes with Same Base Type
A constrained subtype can be implicitly converted to its base type, but the base type can be implicitly converted to the constrained subtype only if the value does not violate a constraint of the subtype (see Example 3-5).
A constrained subtype can be implicitly converted to another constrained subtype with the same base type only if the source value does not violate a constraint of the target subtype.
Example 3-8 Implicit Conversion Between Constrained Subtypes with Same Base Type
1: DECLARE
2:
3: SUBTYPE Digit IS PLS_INTEGER RANGE 0..9;
4:
5: SUBTYPE Double_digit IS PLS_INTEGER RANGE 10..99;
6:
7: SUBTYPE Under_100 IS PLS_INTEGER RANGE 0..99;
8:
9: d Digit := 4;
10:
11: dd Double_digit := 35;
12:
13: u Under_100;
14:
15: BEGIN
16:
17: u := d; -- Succeeds; Under_100 range includes Digit range
18:
19: u := dd; -- Succeeds; Under_100 range includes Double_digit range
20:
21: dd := d; -- Raises error; Double_digit range does not include Digit range
22:
23: END;
24:
25: /
26:
27: Result:
28:
29: DECLARE
30:
31: *
32:
33: ERROR at line 1:
34:
35: ORA-06502: PL/SQL: numeric or value error
36:
37: ORA-06512: at line 12
38:
5: Implicit Conversion Between Subtypes with Base Types in Same Family
Example 3-9 Implicit Conversion Between Subtypes with Base Types in Same Family
1: DECLARE
2:
3: SUBTYPE Word IS CHAR(6);
4:
5: SUBTYPE Text IS VARCHAR2(15);
6:
7: verb Word := 'run';
8:
9: sentence1 Text;
10:
11: sentence2 Text := 'Hurry!';
12:
13: sentence3 Text := 'See Tom run.';
14:
15: BEGIN
16:
17: sentence1 := verb; -- 3-character value, 15-character limit
18:
19: verb := sentence2; -- 5-character value, 6-character limit
20:
21: verb := sentence3; -- 12-character value, 6-character limit
22:
23: END;
24:
25: /
26:
27: Result:
28:
29: DECLARE
30:
31: *
32:
33: ERROR at line 1:
34:
35: ORA-06502: PL/SQL: numeric or value error: character string buffer too small
36:
37: ORA-06512: at line 13
http://www.cnblogs.com/kerrycode/p/3796600.html
網上經常有人問Oracle varchar2最大支持長度為多少?其實這個叫法不太准確,varchar2分別在oracle的sql和pl/sql中都有使用,oracle 在sql參考手冊和pl/sql參考手冊中指出:oracle sql varchar2的最大支持長度為4000個字節(bytes);而 oracle plsql varchar2最大支持長度為32767個字節。這就是有朋友問,在pl/sql中定義了32767個(字符/字節),為什么在表的字段中不能定義大於4000個字節的原因了。
下面分別給出varchar2在oracle sql和plsql中最大長度的示例。
oracle sql中varchar2最大支持長度示例–最大長度為4000
- drop table idb_varchar2;
- create table idb_varchar2
- (id number,
- name varchar2(4000 char));
- insert into idb_varchar2 values(1,lpad('中',32767,'中'));
- insert into idb_varchar2 values(2,lpad('a',32767,'b'));
- commit;
- select id,lengthb(name),length(name) from idb_varchar2;
- drop table idb_varchar2;
- create table idb_varchar2
- (id number,
- name varchar2(4000 char));
- insert into idb_varchar2 values(1,lpad('中',32767,'中'));
- insert into idb_varchar2 values(2,lpad('a',32767,'b'));
- commit;
- select id,lengthb(name),length(name) from idb_varchar2;
輸出結果:
dw@dw>drop table idb_varchar2; 表已刪除。 dw@dw>create table idb_varchar2 2 (id number, 3 name varchar2(4000 char)); 表已創建。 dw@dw>insert into idb_varchar2 values(1,lpad('中',32767,'中')); 已創建 1 行。 dw@dw>insert into idb_varchar2 values(2,lpad('a',32767,'b')); 已創建 1 行。 dw@dw>commit; 提交完成。 dw@dw>select id,lengthb(name),length(name) from idb_varchar2; ID LENGTHB(NAME) LENGTH(NAME) ---------- ------------- ------------ 1 4000 2000 2 4000 4000 已選擇2行。 |
oracle sql中varchar2最大支持長度示例–設計長度為4001
- drop table idb_varchar2;
- create table idb_varchar2
- (id number,
- name varchar2(4001));
- drop table idb_varchar2;
- create table idb_varchar2
- (id number,
- name varchar2(4001));
結果:
dw@dw>drop table idb_varchar2; 表已刪除。 dw@dw>create table idb_varchar2 2 (id number, 3 name varchar2(4001)); name varchar2(4001)) * 第 3 行出現錯誤: ORA-00910: 指定的長度對於數據類型而言過長 |
超過4001會報錯。
oracle plsql中varchar2最大支持長度示例
- set serveroutput on
- declare
- v_var varchar2(32767 byte);
- v_char varchar2(32767 char);
- begin
- v_var := lpad('a',32767,'a');
- dbms_output.put_line(length(v_var));
- v_char := lpad('中',32767,'中');
- dbms_output.put_line(lengthb(v_var));
- v_var := lpad('中',32768,'中');
- end;
- /
- --定義如果超過32768會報錯
- declare
- v_var varchar2(32768);
- begin
- null;
- end;
- /
- set serveroutput on
- declare
- v_var varchar2(32767 byte);
- v_char varchar2(32767 char);
- begin
- v_var := lpad('a',32767,'a');
- dbms_output.put_line(length(v_var));
- v_char := lpad('中',32767,'中');
- dbms_output.put_line(lengthb(v_var));
- v_var := lpad('中',32768,'中');
- end;
- /
- --定義如果超過32768會報錯
- declare
- v_var varchar2(32768);
- begin
- null;
- end;
- /
輸出結果:
dw@dw>set serveroutput on dw@dw>declare 2 v_var varchar2(32767 byte); 3 v_char varchar2(32767 char); 4 begin 5 v_var := lpad('a',32767,'a'); 6 dbms_output.put_line(length(v_var)); 7 v_char := lpad('中',32767,'中'); 8 dbms_output.put_line(lengthb(v_var)); 9 v_var := lpad('中',32768,'中'); 10 end; 11 / 32767 32767 declare * 第 1 行出現錯誤: ORA-06502: PL/SQL: 數字或值錯誤 : 字符串緩沖區太小 ORA-06512: 在 line 9 dw@dw> dw@dw>declare 2 v_var varchar2(32768); 3 begin 4 null; 5 end; 6 / v_var varchar2(32768); * 第 2 行出現錯誤: ORA-06550: 第 2 行, 第 18 列: PLS-00215: 字符串長度限制在范圍 (1...32767)
http://www.linuxidc.com/Linux/2012-03/56006.htm
create table test3 (v2 varchar2(4001)),錯誤,原因:ORA-00910: specified length too long for its datatype
http://blog.chinaunix.net/uid-7240278-id-3209954.html