Stored procedure ERROR 1520: File No:ORA-06502: PL/SQL: numeric or value error when calling it from oracle_cx python driver - oracle

I am calling a stored procedure from Python program.
There is an issue that happens on certain values.
For example it fails when i send values 2020,2017 but it succeeds wen I send 2010,2050 etc. What can be the issue?
DB Version: NLSRTL Version 11.2.0.4.0 - Production
Oracle_cx: latest
Procedure parameter that causes the issue : P_FILE_YEAR IN NUMBER
Error Message: ERROR 1520: File No:ORA-06502: PL/SQL: numeric or value error: number precision too large
It fails in the following exception:
BEGIN
SELECT MAX(mlal_file_no) + 1
INTO ln_file_no
FROM doc_mail_allocation
WHERE mlal_file_year = P_FILE_YEAR
AND std_file_type_code = 2 ;
EXCEPTION
WHEN no_data_found THEN
ln_file_no := 1 ;
WHEN OTHERS THEN
p_error_code := 1 ;
p_error_msg := 'ERROR 1520: File No:'||SQLERRM||' AND flal_file_year = '||P_FILE_YEAR;
RETURN ;
END ;

Looks like this:
Sample data:
SQL> select * From doc_mail_allocation;
MLAL_FILE_YEAR STD_FILE_TYPE_CODE MLAL_FILE_NO
-------------- ------------------ ------------
2010 2 100
2020 2 999
Your code:
SQL> declare
2 ln_file_no number(3);
3 begin
4 SELECT MAX(mlal_file_no) + 1
5 INTO ln_file_no
6 FROM doc_mail_allocation
7 WHERE mlal_file_year = &P_FILE_YEAR
8 AND std_file_type_code = 2 ;
9 dbms_output.put_line('result = ' || ln_file_no);
10 end;
11 /
Enter value for p_file_year: 2010
result = 101
PL/SQL procedure successfully completed.
SQL> /
Enter value for p_file_year: 2020
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: number precision too large
ORA-06512: at line 4
SQL>
See the difference & error cause? If you declared a variable as "too small" (number(3)), for year 2020 MAX(mlal_file_no) + 1 equals 1000 and it can't fit into such a variable.
Solution? Enlarge ln_file_no, or check why this happens (if it shouldn't) - then there's error in data.

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. ?

i need some help about plsql functions in stored procedure

this is my code for procedure. i have created two tables as Stud_Marks and Result with some data in it...and then i have created this procedure. at run time it shows error for "object SYS.FIND_CLASS is invalid".
SQL> create or replace procedure find_class(roll IN number) IS
2 tm number (10);
3 begin
4 select total_marks into tm from stud_marks where name IN(select name from result where roll=roll_no);
5 if(tm<=1500 and tm>=990)then
6 update result set class='distin' where roll_no=roll;
7 elseif(tm>900 and tm<989)then
8 update result set class='first' where roll_no=roll;
9 elseif(tm<=899 and tm>=825)then
10 update result set class='second' where roll_no=roll;
11 end if;
12 exception when no_data_found then
13 dbms_output.put_line('roll no is not matched with the entry');
14 end;
15 /
[enter image description here][1]
Warning: Procedure created with compilation errors
.
SQL> declare
2 r number(10);
3 begin
4 r:=&roll_no;
5 find_class(r);
6 end;
7 /
Enter value for roll_no: 1
old 4: r:=&roll_no;
new 4: r:=1;
find_class(r);
*
ERROR at line 5:
ORA-06550: line 5, column 1:
PLS-00905: object SYS.FIND_CLASS is invalid
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored
then i am getting an error for find_class(r)..why???
Before calling the procedure, you must make sure it is valid. Yours isn't so Oracle raises an error.
Once you created it, you got a message saying
Warning: Procedure created with compilation errors
You should have checked what's wrong by running command
show err
I reformatted code you posted - see how it is easier to read:
create or replace procedure find_class(roll IN number) IS
tm number (10);
begin
select total_marks
into tm
from stud_marks
where name IN (select name
from result
where roll = roll_no
);
if (tm <= 1500 and tm >= 990)then
update result set
class = 'distin'
where roll_no = roll;
elsif (tm > 900 and tm < 989) then --> ELSIF, not ELSEIF
update result set
class = 'first'
where roll_no = roll;
elsif (tm <= 899 and tm >= 825) then --> ELSIF, not ELSEIF
update result set
class = 'second'
where roll_no = roll;
end if;
exception when no_data_found then
dbms_output.put_line('roll no is not matched with the entry');
end;
Apparently, you used ELSEIF while it should be ELSIF. I don't know whether that's the only error as I don't have your tables - you'll find that out. Don't forget to show err if necessary!
So, once it is created without errors, you can call it and see what it does.
P.S. Forgot to mention: why do you do it connected as SYS? That user owns the database and should not be used for development or educational purposes. You should use one of predefined users (such as Scott or HR) or create your own user. Leave SYS alone.

How to rollback and exit in sqlplus whenever SQL WARNINGS occurs in Oracle PL/SQL? [duplicate]

I am deploying pl/sql code using several sql files that are called with ##file. If a package got a compilation error the script continues to the end.
Is there a way to stop on every compilation error?
I tried WHENEVER SQLERROR EXIT SQL.SQLCODE but the script still continues.
No native sql*plus way I'm aware of. Only workarounds. Like this:
20:42:50 TEST#oars_sandbox> get scr
1 whenever sqlerror exit rollback
2 create or replace procedure my_failed_proc as
3 i number;
4 begin
5 select 1 into i from me_iz_not_exist;
6 end;
7 /
8 #check my_failed_proc
9 create or replace procedure my_correct_proc as
10 i number;
11 begin
12 select 1 into i from dual;
13 end;
14 /
15* #check my_correct_proc
20:42:57 16 .
20:42:59 TEST#oars_sandbox> get check
1 declare
2 l_status varchar2(100);
3 begin
4 select status into l_status
5 from all_objects where object_name = upper('&1');
6 if l_status = 'INVALID' then
7 raise_application_error(-20000, 'Object &1 is invalid!');
8 end if;
9* end;
20:43:02 10 .
20:43:04 TEST#oars_sandbox> #scr
Warning: Procedure created with compilation errors.
Elapsed: 00:00:00.05
declare
*
ERROR at line 1:
ORA-20000: Object my_failed_proc is invalid!
ORA-06512: at line 8
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production

PL/SQL : i have a function but there is an error : "in a procedure,RETURN can not contain an expression"

Here is my code:
CREATE OR REPLACE FUNCTION customer_city_function(city_in IN VARCHAR2)
RETURN NUMBER
AS
number_cus NUMBER := 0;
CURSOR cus_cur IS
SELECT COUNT(*)
FROM customer
WHERE customer_city = city_in;
BEGIN
IF city_in IS NOT NULL THEN
OPEN cus_cur;
FETCH cus_cur INTO number_cus;
CLOSE cus_cur;
END IF;
RETURN number_cus;
END;
/
and here is warnings:
Error starting at line : 1 in command -
CREATE OR REPLACE FUNCTION customer_city_function(city_in IN VARCHAR2)
RETURN NUMBER
AS
number_cus NUMBER := 0
Error report -
SQL Command: functıon CUSTOMER_CITY_FUNCTION
Failed: Warning: executing is completed with a warning
Error starting at line : 5 in command -
CURSOR cur_cur IS
Error report -
Unknown Command
Error starting at line : 6 in command -
SELECT COUNT(*)
FROM costumer
WHERE customer_city=city_in
Error at Command Line : 8 Column : 25
Error report -
SQL Error: ORA-00904: "CITY_IN": undefined variable
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error starting at line : 9 in command -
BEGIN
IF city_in IS NOT NULL
THEN
OPEN cus_cur;
FETCH cus_cur INTO number_cus;
CLOSE cus_cur;
END IF;
RETURN (number_cus);
END;
Error report -
ORA-06550: row 2, column 6:
PLS-00201: 'CITY_IN' variable should been defined
ORA-06550: row 2, column 3:
PL/SQL: Statement ignored
ORA-06550: row 8, column 1:
PLS-00372: in a procedure, RETURN can not contain an expression
ORA-06550: row 8, column 1:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Where is my mistake? I can't find it, it doesn't make any sense.
(I translated this warning message from my language. I hope I did it right.)
I have just tried it in Command Window and it works. Why doensn't it work in Oracle SQL Developer sql worksheet?
There is nothing wrong with your posted code. The issue might be with your client or the way you are compiling the code.
As you have mentioned PL/SQL Developer in the tags, it might be possible that you have some extra characters in the SQL Worksheet and you are compiling the function as a script, thus the compiler finds it erroneous.
Here is a demo in SQL*Plus, and there is no error:
SQL> CREATE OR REPLACE FUNCTION customer_city_function(i_deptno IN number)
2 RETURN NUMBER
3 AS
4 number_cus NUMBER := 0;
5 CURSOR cus_cur IS
6 SELECT COUNT(*)
7 FROM emp
8 WHERE deptno=i_deptno;
9 BEGIN
10 IF i_deptno IS NOT NULL
11 THEN
12 OPEN cus_cur;
13 FETCH cus_cur INTO number_cus;
14 CLOSE cus_cur;
15 END IF;
16 RETURN number_cus;
17 END;
18 /
Function created.
SQL> sho err
No errors.
SQL> SELECT customer_city_function(10) FROM DUAL;
CUSTOMER_CITY_FUNCTION(10)
--------------------------
3
SQL>
The only difference in my code is that I have used EMP table instead of CUSTOMERS table and the input parameter is DEPTNO instead of CITY_IN. Rest everything is same and function compiles and executes without any errors.

halt on compilation error in a sqlplus script

I am deploying pl/sql code using several sql files that are called with ##file. If a package got a compilation error the script continues to the end.
Is there a way to stop on every compilation error?
I tried WHENEVER SQLERROR EXIT SQL.SQLCODE but the script still continues.
No native sql*plus way I'm aware of. Only workarounds. Like this:
20:42:50 TEST#oars_sandbox> get scr
1 whenever sqlerror exit rollback
2 create or replace procedure my_failed_proc as
3 i number;
4 begin
5 select 1 into i from me_iz_not_exist;
6 end;
7 /
8 #check my_failed_proc
9 create or replace procedure my_correct_proc as
10 i number;
11 begin
12 select 1 into i from dual;
13 end;
14 /
15* #check my_correct_proc
20:42:57 16 .
20:42:59 TEST#oars_sandbox> get check
1 declare
2 l_status varchar2(100);
3 begin
4 select status into l_status
5 from all_objects where object_name = upper('&1');
6 if l_status = 'INVALID' then
7 raise_application_error(-20000, 'Object &1 is invalid!');
8 end if;
9* end;
20:43:02 10 .
20:43:04 TEST#oars_sandbox> #scr
Warning: Procedure created with compilation errors.
Elapsed: 00:00:00.05
declare
*
ERROR at line 1:
ORA-20000: Object my_failed_proc is invalid!
ORA-06512: at line 8
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production

Resources