Oracle 无法通过错误信息了解您的意图。知道您将其存储的变量名称或它的确切来源并不一定符合最终用户或安全性的最佳利益。
例如,我可以轻松生成您的错误:
SQL> declare
2 v_tooshort varchar2(3);
3 begin
4 select 'too long' into v_tooshort from dual;
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4
错误已经给出了行号。
您希望错误给出变量名 ( v_tooshort
) 吗?这对用户没有帮助。
正确信息的价值是否“太长”?或者它是表格中的虚拟列dual
?(或实际的列和表)?
因为错误是由 aselect into
而不是insert
语句引起的,所以它不像异常可以通过名称识别特定的数据库约束。
编辑(解决评论中提出的问题):
那不是真的。insert
执行 an (作为 ORA-12899)时,您将获得返回的列名和长度,但在执行时不会,select into
即使它使用表中的数据:
SQL> create table test_length (tooshort varchar2(3));
Table created.
SQL> begin
2 insert into test_length(tooshort) values ('too long');
3 end;
4 /
begin
*
ERROR at line 1:
ORA-12899: value too large for column "MYUSER"."TEST_LENGTH"."TOOSHORT"
(actual: 8, maximum: 3)
ORA-06512: at line 2
SQL> insert into test_length(tooshort) values ('abc');
1 row created.
SQL> commit;
Commit complete.
SQL> declare
2 v_onechar varchar2(1);
3 begin
4 select tooshort into v_onechar from test_length;
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4
编辑2:
您可以将您嵌套select into
到它自己的 begin-exception-end 块中并引发您喜欢的任何错误(提供唯一的错误编号和描述性错误文本):
SQL> ed
Wrote file afiedt.buf
1 declare
2 v_onechar varchar2(1);
3 begin
4 select tooshort into v_onechar from test_length;
5 exception
6 when value_error then
7 RAISE_APPLICATION_ERROR(-20011, 'my variable named v_onechar is too short for the data returned from test_lengt
h.tooshort');
8* end;
SQL> /
declare
*
ERROR at line 1:
ORA-20011: my variable named v_onechar is too short for the data returned from
test_length.tooshort
ORA-06512: at line 7