Error: ORA-06502: PL/SQL: numeric or value error: host bind array too small - oracle

I want to understand when this oracle error occurs 'Error: ORA-06502: PL/SQL: numeric or value error: host bind array too small'
I have a plsql block and it has a normal annonymus block which has DBMS_output.put_line and also has DBMS_output.put in a for loop and in the code its calls another package in for loop itself and the out variables like success( as S) and errormessage (as err_msg) of package are displayed using DBMS_output.put and the out put gets printed for few records but suddenly a above error occurs and blocks gets completed don't how this happemed. Can any one explain why such error occurs.

The error occurs on the client-side, not on the server-side, so it's an error with specific versions of sqlplus. It happens when sqlplus utility tries to read the dbms_output buffer from the server-side to the client-side buffer, and the buffer contains a string that is more than 255 characters long. So error happens when:
you are using sqlplus with version less than 10.2 and the version of Database server is 10.2 or higher. For example:
client is 10.1.0.5 and server is 12.2.9
client is 10.1.0.5 and server is 10.2.0.5
client is 10.1.0.5 and server is 11.2.0.1
the serveroutput on option is set
you have put a line with more than 255 characters into the dbms_output buffer.
Please, note that if your server is 10.1.0.5 or less (9.2.0.8) you would get different error: ORA-20000: ORU-10028: line length overflow, limit of 255 chars per line.
Demo
Put 256 characters and we get the error:
SET SERVEROUTPUT ON
begin
dbms_output.put_line(a => rpad('1',256,'1'));
end;
/
ERROR:
ORA-06502: PL/SQL: numeric or value error: host bind array too small
ORA-06512: at line 1
Or you can do it in a slightly different way:
SET SERVEROUTPUT ON
begin
dbms_output.put(a => rpad('1',200,'1'));
dbms_output.put(a => rpad('1',200,'1'));
dbms_output.put_line('');
end;
/
ERROR:
ORA-06502: PL/SQL: numeric or value error: host bind array too small
ORA-06512: at line 1
Put 255 characters and it' ok we get the output:
SET SERVEROUTPUT ON
begin
dbms_output.put_line(a => rpad('1',255,'1'));
end;
/
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111
How to solve
Option 1:
Oracle fixed the error in the 10.2 release, so if you upgrade your server-side and the client-side components to at least 10.2.0.5, the error will be fixed.
Option 2:
If you absolutely have to use 10.1 version or older versions of sqlplus (and version of Database is 10.2 or higher) and you cannot rewrite your pl/sql code that generates long characters string, then you can work around this bug using dbms_output.get_lines (or dbms_output.get_line) procedures:
SET SERVEROUTPUT OFF
begin
-- enable the server-side buffer. When SET SERVEROUTPUT ON sqlplus does it automatically.
-- Now we do it manually.
dbms_output.enable(1000000);
dbms_output.put_line(a => rpad('1',300,'1'));
end;
/
PL/SQL procedure successfully completed.
Then use this block just to get the content of the buffer:
variable l_cursor REFCURSOR
set autoprint on
declare
-- Then use this block to get the contents of the buffer.
l number:=10000;
l_array DBMSOUTPUT_LINESARRAY;
begin
dbms_output.get_lines(l_array,l);
open :l_cursor for select * from table(l_array);
end;
/
PL/SQL procedure successfully completed.
COLUMN_VALUE
--------------------------------------------------------------------------------
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111

usually this error will occur when you either try to set a variable with too big a value. for instance.
v_number number(2);
BEGIN
v_number := 100;
END;
or when you try to save text into a number variable
v_number number(2);
BEGIN
v_number := 'a';
END;
see here for more info: https://www.techonthenet.com/oracle/errors/ora06502.php

Related

plsql using in datagrip ide

When I am running PL/SQL scripts in Datagrip I am getting task compiled but I cannot see the output how to get an output in datagrip,
and when I am running dynamic block it's not taking input value in datagrip.
declare
v1 emp.empno%type := '&empno';
v2 emp.ename%type;
begin
select empno,ename into v1,v2 from emp where empno = v1;
DBMS_OUTPUT.PUT_LINE(v1 || ' ' || v2);
end;
it shows this error
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
Does anyone have any solution?
Enable DBMS_OUTPUT button is placed on the right toolbar of the Services window
I don't use DataGrip.
Looks like you should enable dbms_output. How? Try with [Ctrl + F8] (as a keyboard shortcut), or push the appropriate button (tooltip says "Enable SYS.DBMS_OUTPUT").
As of the second part of your question, documentation says
Generally, only the question mark ? is treated as a parameter in SQL statements, which means that you might need to do this:
v1 emp.empno%type := ?;
Because, code you posted works OK in e.g. SQL Developer or SQL*Plus:
Enabling DBMS_OUTPUT:
SQL> set serveroutput on
Code execution:
SQL> declare
2 v1 emp.empno%type := '&empno';
3 v2 emp.ename%type;
4 begin
5 select empno,ename into v1,v2 from emp where empno = v1;
6 DBMS_OUTPUT.PUT_LINE(v1 || ' ' || v2);
7 end;
8 /
Enter value for empno: 7369
7369 SMITH --> this is the result
PL/SQL procedure successfully completed.
SQL>
Finally, ORA-06502: it happens when you pass something that isn't a number (while number is expected); for example, I'll pass ABC:
SQL> /
Enter value for empno: ABC
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 2
SQL>
See? The same error you got. Maybe it all has to do something with improper usage of a substitution variable, i.e. '&empno' vs. ?

Get the first value from Oracle cursor - Calling From Java Code

I have a oracle cursor which I have created to facilitate concurrency. This is my cursor.
create or replace FUNCTION get_unlocked_records RETURN table_to_test%ROWTYPE IS
CURSOR c IS SELECT * FROM table_to_test where status_code = 5 FOR UPDATE SKIP LOCKED;
record_to_get table_to_test%ROWTYPE;
BEGIN
OPEN c;
FETCH c INTO record_to_get;
CLOSE c;
RETURN record_to_get;
END;
When I do the testing in 2 separate sql sessions using these commands,it gives the following errors.
declare
record_to_gets table_to_test%ROWTYPE;
begin
exec :record_to_gets := get_unlocked_records;
dbms_output.put_line(record_to_gets);
end;
Error
Error starting at line : 32 in command -
declare
record_to_gets table_to_test%ROWTYPE;
begin
exec :record_to_gets := get_unlocked_records;
dbms_output.put_line(record_to_gets);
end;
Error report -
ORA-06550: line 4, column 7:
PLS-00103: Encountered the symbol "" when expecting one of the following:
:= . ( # % ;
The symbol ";" was substituted for "" to continue.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
What is the error that I am doing here ?
Since my ultimate goal is to call the function and get the result in java, how to call this function to get the first record in java ?
Thanks in advance.
EXEC[UTE] is a SQL*Plus command and prepending variable with a colon is done in SQL*Plus, but in PL/SQL EXECUTE IMMEDIATE might be used whereas that's not needed in your case, only using such an assignment without prepending the local variable is enough :
DECLARE
record_to_gets table_to_test%ROWTYPE;
BEGIN
record_to_gets := get_unlocked_records;
DBMS_OUTPUT.PUT_LINE(record_to_gets.col1);
DBMS_OUTPUT.PUT_LINE(record_to_gets.col2)
END;
/

ORA-06561 while executing PL/SQL procedure

I have this procedure:
create or replace
PROCEDURE P_P2
IS
v_str varchar2(1000);
v_file_name varchar2(1000);
BEGIN
P_P1(v_str, 'EXPORT_CSV',v_file_name);
v_str := 'select * from H----)';
v_file_name := 'H_'||to_char(sysdate,'DD-MM-YYYY')||'.csv';
END;
I am getting error "ORA-06561: given statement is not supported by package DBMS_SQL" when I execute it:
Error starting at line : 165 in command -
exec P_P1
Error report -
ORA-06561: given statement is not supported by package DBMS_SQL
ORA-06512: at "SYS.DBMS_SQL", line 1120
ORA-06512: at "BIDB.P_P1", line 40
ORA-06512: at "BIDB.P_P2", line 7
ORA-06512: at line 1
06561. 00000 - "given statement is not supported by package DBMS_SQL"
*Cause: Attempting to parse an unsupported statement using procedure
PARSE provided by package DBMS_SQL.
*Action: Only statements which begin with SELECT, DELETE, INSERT, UPDATE,
LOCK, BEGIN, DECLARE or << (PL/SQL label delimiter) are supported.
I cannot see why. What am I doing wrong?
You haven't shown what P_P1 is doing, but from what you have shown, your P_P2 procedure may just be calling it too early; you have a call to P_P1 which uses v_str as a parameter, but it's null at that point - you set it after the call that uses it.
That means that somewhere in P_P1 you are probably ending up doing the equivalent of:
dbms_sql.parse(c, null, dbms_sql.native);
which throws the error you are seeing. You're doing the same with v_filename, which will presumably cause other issues.
So for a start, swap those lines around so the procedure is called last:
create or replace
PROCEDURE P_P2
IS
v_str varchar2(1000);
v_file_name varchar2(1000);
BEGIN
v_str := 'select * from H----)';
v_file_name := 'H_'||to_char(sysdate,'DD-MM-YYYY')||'.csv';
-- call this AFTER populating the variables
P_P1(v_str, 'EXPORT_CSV',v_file_name);
END;
/
You may have other problems of course - the table name looks odd, both because of the dashes and the closing parenthesis; but you may have just hidden the real name oddly. You seem to have changed the procedure names too (though inconsistently, as your exec seems to be calling the wrong one...).

Compilation errors on stored procedure

I am typically a SQL Server developer however am now working with a system that uses Oracle. I have created a new procedure and am getting a runtime error. Here is the procedure:
CREATE OR REPLACE PROCEDURE CHK_LASTAPPTIME
(
LASTAPPTIME OUT VARCHAR2
)
IS
v_appappid varchar2(20) null;
v_lastapptime number null;
BEGIN
select max(APPID) into v_appappid from applicationtable;
select trunc(v_lastapptime = (((sysdate - capturedate) * 24)) * 60) from applicationtable where APPID = v_appappid;
LASTAPPTIME := to_char(v_lastapptime);
END CHK_LASTAPPTIME;
Here is the error that I am getting:
SQL> var x varchar2;
SQL> exec CHK_LASTAPPTIME(:x);
BEGIN CHK_LASTAPPTIME(:x); END;
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "CAPDEV.CHK_LASTAPPTIME", line 19
ORA-06512: at line 1
Your proecedure doesn't have 19 lines, so the message is odd, or you've shown different code to what you're actually running. Since what you've shown doesn't compile I'm assuming you've tried to cut it down for the question, and I'll also assume your actual procedure is valid and does what you want.
The problem is in your run-time variable declaration. You haven't specified a size for x, so it uses a default. On my DB, in SQL*Plus that seems to allow three characters but errors with four or more:
var x varchar2;
exec :x := 'a';
PL/SQL procedure successfully completed.
exec :x := 'ab';
PL/SQL procedure successfully completed.
exec :x := 'abc';
PL/SQL procedure successfully completed.
exec :x := 'abcd';
BEGIN :x := 'abcd'; END;
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 1
The limit might be related to your character set, perhaps. SQL Developer allows more by default.
Anyway, specify the size for your variable:
var x varchar2(30);
exec CHK_LASTAPPTIME(:x);
You seem to be generating a number in your procedure, and then returning that as a string. Using the same data type would make this a bit simpler.

Why do I get an error as I try to call a procedure?

I created a procedure named greet as :
create procedure greet(message in char(50))
as
begin
dbms_output.put_line('Greet Message : ' || message);
end;
The procedure compiled successfully but when I try to call it as :
execute greet('Hey ! This is a self created procedure :)');
I get an error :
execute greet('Hey ! This is a self created procedure :)')
Error report:
ORA-06550: line 1, column 7:
PLS-00905: object SUHAIL.GREET is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
What error is it ? Why do I get it ?
Note : 'suhail' is name of the current user connected to oracle server
I don't believe that your procedure compiled successfully. When I try to compile it on my system, I get syntax errors
SQL> create procedure greet(message in char(50))
2 as
3 begin
4 dbms_output.put_line('Greet Message : ' || message);
5 end;
6 /
Warning: Procedure created with compilation errors.
SQL> sho err
Errors for PROCEDURE GREET:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/32 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= ) , default varying character large
The symbol ":=" was substituted for "(" to continue.
If I resolve the syntax errors (you cannot specify a length for an input parameter), it works
SQL> ed
Wrote file afiedt.buf
1 create or replace procedure greet(message in char)
2 as
3 begin
4 dbms_output.put_line('Greet Message : ' || message);
5* end;
SQL> /
Procedure created.
SQL> set serveroutput on;
SQL> execute greet('Hey ! This is a self created procedure :)');
Greet Message : Hey ! This is a self created procedure :)
PL/SQL procedure successfully completed.
I would be shocked if you really wanted the input parameter to be declared as CHAR. Almost always, you should use VARCHAR2 for character strings. It is exceptionally rare to come across a case where you really want the blank-padding semantics of a CHAR.
this is working dude;
create or replace
procedure greet(message in char)
as
begin
dbms_output.put_line('Greet Message : ' || message);
end;
see main property of char datatype is is the length of input data is less than the size you specified it'll add blank spaces.this case is not happened for varchar2.
in procedure above mentioned char property is violated so it's almost treat like varchar2. so if you remove size of input parameter it will work and also char support maximum length of input.

Resources