PLS-00215: String length constraints must be in range (1 .. 32767) - oracle

I have a oracle package and there is a function in it which is having out parameters.
the package name lets say ppp and function name lets say fff.
function is below and i am unable to execute the function with out parameters.
function-
FUNCTION fff (P_FID NUMBER,DUCTBANKLABEL VARCHAR2, SERVICEDUCTVALID OUT NUMBER ) RETURN VARCHAR2
----End of R3 - Obs#195 (1 Oct 2018)
AS
CNT NUMBER;
INSTDATE VARCHAR2(100);
DUCTSIZE NUMBER;
.
.
.
.
many more.......;
BEGIN
.
.
.function does its thing and returns the value
END;
Now am trying to call this function with a pl/sql block like this-
set serveroutput on;
declare
aa NUMBER:=129685933;
bb VARCHAR2:='1705297E TO P5547635';
cc NUMBER;
ANS VARCHAR2;
BEGIN
ANS:=ppp.fff(aa,bb,cc);
DBMS_OUTPUT.put_line(ANS);
END;
But getting the below error-
Error report:
ORA-06550: line 3, column 4:
PLS-00215: String length constraints must be in range (1 .. 32767)
ORA-06550: line 5, column 5:
PLS-00215: String length constraints must be in range (1 .. 32767)
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Please help on how can i execute this function

You need to specify VARCHAR2 variable lengths in your anonymous PL/SQL block.
DECLARE
aa NUMBER := 129685933;
bb VARCHAR2 (30) := '1705297E TO P5547635';
cc NUMBER;
ANS VARCHAR2 (200);
BEGIN
ANS := ppp.fff (aa, bb, cc);
DBMS_OUTPUT.put_line (ANS);
END;

varchar2 variables need to have a length as the error indicates.
Line 3
bb VARCHAR2:='1705297E TO P5547635';
and line 5
ANS VARCHAR2;
are both missing lengths. Assuming you want both to allow, say, 100 characters
bb VARCHAR2(100):='1705297E TO P5547635';
ANS VARCHAR2(100);

Related

ORA-06502 with ORA-06512

I have a procedure in which I'm trying to write a source code (1290 lines) to dbms_output like this:
dbms_output.put_line(DBMS_METADATA.GET_DDL('FUNCTION', 'name', 'owner')); --MYPROC, line 6
I'm getting :
ORA-06502: PL/SQL: numeric or value error
ORA-06512: in "MYPROC", line 6
.
This error occures in toad.
I can execute in editor tab of toad:
SELECT DBMS_METADATA.GET_DDL('FUNCTION', 'name', 'owner') FROM DUAL;
I mean I'm getting the source code in 'Data grid'.
Same happens when I try to store the code in a CLOB variable:
src CLOB;
...
src := DBMS_METADATA.GET_DDL('FUNCTION', 'name', 'owner') ; --MYPROC, line 6
Any clue?
From the documentation for dbms_output:
The maximum line size is 32767 bytes.
That means that you can't pass more than that in a single put_line call. You are currently passing your whole CLOB, which at 1290 lines is likely to exceed that limit. And the error you get when you do that is "ORA-06502: PL/SQL: numeric or value error", as you are seeing.
You can split your CLOB into smaller chunks, and as it is already multiple lines it makes sense to make each chunk a single line from the DDL. You can do that by looking for newline characters, extracting all the text up to the next one, and printing that. You need a few variables to keep track of where you are. Something like this should work for you:
declare
src clob;
src_length pls_integer;
pos pls_integer := 1;
buffer varchar2(32767);
amount pls_integer := 32767;
begin
src := dbms_metadata.get_ddl('FUNCTION', 'TEST_FUNCTION_1', user);
src_length := dbms_lob.getlength(src);
while pos < src_length loop
-- read to next newline if there is one, rest of CLOB if not
if dbms_lob.instr(src, chr(10), pos) > 0 then
-- see how many charcaters there are until next newline
amount := dbms_lob.instr(src, chr(10), pos) - pos;
-- if there are any, read them into the buffer; otherwise clear it
if amount > 0 then
dbms_lob.read(src, amount, pos, buffer);
else
buffer := null;
end if;
pos := pos + amount + 1; -- skip newline character
else
-- no newline so read everything that is left
amount := 32767;
dbms_lob.read(src, amount, pos, buffer);
pos := pos + amount;
end if;
dbms_output.put_line(buffer);
end loop;
end;
/
It won't work if you have a single line (without or without a newline at the end) that is more than 32k, which hopefully won't be an issue with DDL. (You could sort of handle it, but doing so would inject additional newlines, which wouldn't be good either.)
What you are saying can't be true. DBMS_OUTPUT.PUT_LINE can't be used at SQL level, it belongs to PL/SQL.
What is MYPROC and what does it contain at line #6?
Which "editor" is "I can execute in editor"?
Numeric or value error is usually related to the fact that you're trying to store "large" values into a "small" variable:
SQL> declare
2 l_var varchar2(2);
3 begin
4 l_var := 'ABC';
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
SQL>
which is what - I presume - you did.
Another cause is wrongly declared variable, e.g.
SQL> declare
2 l_var number;
3 begin
4 l_var := 'A';
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 4
SQL>
I'll try to guess what you might be doing:
SQL> set serveroutput on
SQL> DECLARE
2 src CLOB;
3 BEGIN
4 src := DBMS_METADATA.GET_DDL ('PACKAGE', 'MY_PKG', 'SCOTT');
5 DBMS_OUTPUT.put_line ('len = ' || DBMS_LOB.getlength (src));
6 END;
7 /
len = 67239
PL/SQL procedure successfully completed.
SQL>
As you can see, it works OK for me. Package isn't that small (see its length), so - can't really tell what you did wrong. I'd suggest you to do exactly as I did - copy/paste code I posted above (those 7 lines), fix information (function, its name, owner) and post the result by editing the original question, not as a comment.

PLS-00201: identifier must be declared in Procedure

I have a PL/SQL Procedure code, which runs when it is / , but doesn't runs when it's executed. The error message I get is
SQL> EXECUTE MAXINUM;
BEGIN MAXINUM; END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'MAXINUM' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
The code which I'm working on is :
DECLARE
N NUMBER;
M NUMBER;
O NUMBER;
P NUMBER;
X NUMBER;
PROCEDURE MAXINUM(N IN NUMBER, M IN NUMBER, O IN NUMBER, P IN NUMBER, X OUT NUMBER) IS
BEGIN
IF N>M AND N>O AND N>P THEN
X:=N;
ELSIF M>N AND M>O AND M>P THEN
X:=M;
ELSIF O>N AND O>M AND O>P THEN
X:=O;
ELSIF P>N AND P>M AND P>O THEN
X:=P;
END IF;
END;
BEGIN
N:=&NUMBER;
M:=&NUMBER;
O:=&NUMBER;
P:=&NUMBER;
MAXINUM(N,M,O,P,X);
DBMS_OUTPUT.PUT_LINE('HIGHEST NUMBER = '||X);
END;
/
When it gave the 'identifier error', I tried dropping this procedure I got the error:
SQL> DROP PROCEDURE MAXINUM;
DROP PROCEDURE MAXINUM
*
ERROR at line 1:
ORA-04043: object MAXINUM does not exist
I have so far read this, this, this solutions and other solutions some what related to this error.
You have written an anonymous block with a local procedure MAXINUM(). This procedure can be called within that block but does not exist outside that block. Consequently you cannot call it independently.
If you want to use the procedure elsewhere you need to create it as a first class database object:
create or replace procedure MAXINUM
(N IN NUMBER, M IN NUMBER, O IN NUMBER, P IN NUMBER, X OUT NUMBER)
is
BEGIN
IF N>M AND N>O AND N>P THEN
X:=N;
ELSIF M>N AND M>O AND M>P THEN
X:=M;
ELSIF O>N AND O>M AND O>P THEN
X:=O;
ELSIF P>N AND P>M AND P>O THEN
X:=P;
END IF;
END;
/
Now you can call it in your code, like this:
DECLARE
N NUMBER;
M NUMBER;
O NUMBER;
P NUMBER;
X NUMBER;
BEGIN
N:=&NUMBER;
M:=&NUMBER;
O:=&NUMBER;
P:=&NUMBER;
MAXINUM(N,M,O,P,X);
DBMS_OUTPUT.PUT_LINE('HIGHEST NUMBER = '||X);
END;
/
Points to note:
what happens if a parameter is null?
what happens if two arguments have the same value?
the convention would be to declare this as a function and return the highest value instead of setting an OUT parameter.
Incidentally I assume you're doing this as an exercise, as it is a re-implementation of an existing Oracle built-in function, greatest().

Creating a anonymous block to display 1 to 5 and getting the below error

ORA-06550: line 2, column 11:
PLS-00103: Encountered the symbol "=" when expecting one of the following:
constant exception <an identifier>
<a double-quoted delimited-identifier> table long double ref
char time timestamp interval date binary national character
nchar
The symbol "<an identifier>" was substituted for "=" to continue.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Here is the code:
SET SERVEROUTPUT ON;
clear scr;
DECLARE v_counter := 0;
BEGIN
LOOP
v_counter:= v_counter+1;
IF v_counter=3 THEN CONTINUE; END IF;
EXIT WHEN v_counter=5;
END LOOP;
DBMS_OUTPUT.PUT_LINE('v_counter='||v_counter);
END;
Creating a anonymous block to display 1 to 5 and getting the below error
Your code will never display 1 to 5. As #David said in the comments -
You haven't declared the data type for the variable.
The DBMS_OUTPUT is outside the LOOP, thus it will print only the last value held by the variable.
That IF-ELSE construct seems unnecessary if you just want to print 1 to 5.
You could achieve the same in just one FOR LOOP -
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
2 FOR i IN 1..5
3 LOOP
4 DBMS_OUTPUT.PUT_LINE('v_counter='||i);
5 END LOOP;
6 END;
7 /
v_counter=1
v_counter=2
v_counter=3
v_counter=4
v_counter=5
PL/SQL procedure successfully completed.
SQL>
The same could be done in SQL -
SQL> select 'v_counter ='||level counter from dual connect by level <=5;
COUNTER
---------------------------------------------------
v_counter =1
v_counter =2
v_counter =3
v_counter =4
v_counter =5
SQL>
The problem is the line reading
DECLARE v_counter := 0;
You forgot to specify the variable's datatype.
It should read
DECLARE v_counter number := 0;
Of course, instead of number you might choose pls_integer if you're more confortable with it.

expression 'string' cannot be used as an assignment target -SQL PLUS

I wrote the following procedure which was meant to be anonymous and remove all the vowels from a string, but when I call it I get an error: I've followed the advice given in a similar post, but it didn't help:Oracle PLS-00363: expression '' cannot be used as an assignment target
SQL> CREATE OR REPLACE PROCEDURE disemvowel (string IN OUT NVARCHAR2)
2 IS
3 BEGIN
4 DBMS_OUTPUT.PUT_LINE(translate(string,'euioa',''));
5 END disemvowel;
6 /
Procedure created.
So good so far, but now I call it:
SQL> BEGIN
2 disemvowel('hahahahaha');
3 END;
4 /
The Error message says:
disemvowel('hahahahaha');
*
ERROR at line 2:
ORA-06550: line 2, column 12:
PLS-00363: expression 'hahahahaha' cannot be used as an assignment target
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored
Your procedure has IN OUT parameter. So while calling the procedure you should supply a variable to it, so that it can hold the value that the procedure gives back. You cannot supply a value directly, as it cannot be modified by the procedure.
DECLARE
param NVARCHAR2 (20) := 'hahahahaha';
BEGIN
disemvowel (param);
END;
/
Generate new VARCHAR2 type variable to assign your IN (input) string.
PROCEDURE sp_name(
ps_list IN VARCHAR2,
...
write here other IN's and OUT's
...
)
AS
ps_list_copy VARCHAR2 (32000);
BEGIN
ps_list_copy := ps_list;
...
do your works with ps_list_copy
...
...
EXCEPTION WHEN OTHERS THEN
....
END sp_name;

Oracle - Number to varchar

I have a table containing a column of type Number
create table tmp (
/*other fields*/
some_field Number
)
and in a PL SQL script, I want to convert that field to a varchar. However, i don't know its length, so I get an exception
Exception message is ORA-06502:
PL/SQL: numeric or value error:
character string buffer too small
v_some_field varchar(21);
/*...*/
v_some_field := TO_CHAR(some_field,'999999999999999999999');
How should i declare the v_some_field buffer? Setting it to varchar(32767) seems quite brute, is there any alternative?
you're getting an error not because the number is too large but because the result of your to_char is 22 characters long (21x"9"+one character for the sign):
SQL> DECLARE
2 some_field NUMBER := 123;
3 v_some_field VARCHAR(21);
4 BEGIN
5 v_some_field := TO_CHAR(some_field, '999999999999999999999');
6 END;
7 /
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 6
SQL> DECLARE
2 some_field NUMBER := 123;
3 v_some_field VARCHAR(22);
4 BEGIN
5 v_some_field := TO_CHAR(some_field, '999999999999999999999');
6 END;
7 /
PL/SQL procedure successfully completed
You could determine the maximum length of your converted varchar2 by converting a negative value with integral and fractional digits:
set serveroutput on
declare
n number;
begin
n := -4/3;
dbms_output.put_line(length(to_char(n)));
end;
/
Output is 41 for me.

Resources