Oracle can't know what your intent is with the error information. Knowing the variable name you're storing it as or where exactly it came from isn't necessarily in the best interests of the end user or security.
For example, I can generate your error easily:
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
The error already gives the line number.
Would you prefer the error give the variable name (v_tooshort
)? That's not helpful to a user.
Is the right information the value, "too long"? Or the fact that it's a dummy column from the dual
table? (or an actual column and table)?
Because the error arises from a select into
instead of an insert
statement, it's not like there's a specific database constraint the exception can identify by name.
EDIT (to address issue raised in a comment):
That's not true. You'll get the column name and lengths returned when doing an insert
(as an ORA-12899), but not when doing a select into
even if it's using data from a table:
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
EDIT 2:
You can nest your select into
into its own begin-exception-end block and raise whatever error you like (providing a unique error number and descriptive error text):
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