create procedure about_emp(p_empno in number,p_ename out varchar2)
is
begin
select ename into p_ename from emp
where empno=p_empno;
exception
when no_data_found then
dbms_output.put_line('your id not available');
when value_error then
dbms_output.put_line('enter exact data');
end;
execution
variable x varchar2(10);
exec about_emp(4520,:x);
X
michel
exec about_emp(1111,:x);
you id not available
exec abot_emp('a',:x);
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 1
but normally in store procedure are display like
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "SCOTT.about_emp", line 4
ORA-06512: at line 1
please find the error and give me the solution
Well, either you have declared X variable too small to hold the value of empno.ename, so make it larger. If you also want to see the output from dbms_output in sqlplus, you need to enable: "set serveroutput on"
Procedure about_emp expects number data type as first formal parameter and varchar as second formal parameter.
create procedure about_emp(p_empno in number,p_ename out varchar2)
But while executing you are supplying varchar data type for the first formal parameter (where number is expected)
exec abot_emp('a',:x);
Oracle can't do implicit conversion of a to a number, so that is why you are getting error ERROR at line 1: ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: at line 1
You should call this procedure supplying first actual parameter as number and second parameter as varchar (or compatible (or convertable) data types )
As per exec about_emp(1111,:x); you id not available
This is just output from trapped exception
exception
when no_data_found then
dbms_output.put_line('your id not available');
It mean's that SELECT returned empty result set, there is no such data in the table.
UPDATE OP asked how to handle errors in PL/SQL
1st option - You need to associate error with the definition
PRAGMA EXCEPTION_INIT(invalid_implicit_conversion, -6789);
and then trap error in EXCEPTION section.
EXCEPTION
WHEN invalid_implicit_conversion
THEN ...
2nd option -
EXCEPTION
WHEN OTHERS // this will trap any exception
IF SQLCODE = -6789 THEN ...
Related
CREATE OR REPLACE PROCEDURE ex9a(n NUMBER ,c CHAR) IS
pi NUMBER(7,4):=3.14;
v_record Areas%rowtype;
BEGIN
IF c='R' THEN
DBMS_OUTPUT.PUT_LINE('CHOICE : R');
v_record.Input_Value:= n;
v_record.Circle_Area:=pi*n*n;
v_record.Square_Area:=null;
v_record.Sphere_Area:=2*pi*n;
v_record.Sphere_Volume:=(3/4)*r*r;
v_record.Cube_Volume:=null;
END IF;``
END;
/
I am getting procedure created with compilation errors.I want to compute area of a circle and insert it into the table .
When I give SHOW ERRORS , it lists the errors as
1) plsql statement
2) 'R' must be declared .
(Even after I gave 'then'. I forgot the line number)
It would help if you would list the errors, and the lines on which they occur.
However, one obvious issue is that your IF statement has no THEN. It should be:
IF c = 'R' THEN
I am trying to create and assign variables using following code to create object types in plsql (11g) but facing some errors:
begin
execute immediate 'drop type picu_obj force';
execute immediate 'drop type picu_obj_tab force';
execute immediate 'create type picu_obj as object(Customer_ID varchar2(32767),Customer_Name varchar2(32767),Server_Name varchar2(32767),Time_stamp varchar2(32767))';
execute immediate 'create type picu_obj_tab is table of picu_obj;';
picu_var picu_obj_tab;
picu_var := picu_obj_tab(picu_obj('101','xyz','pro-ssr-qr','12:13'));
end;
The above code gives following errors:
ERROR at line 6:
ORA-06550: line 6, column 10:
PLS-00103: Encountered the symbol "PICU_OBJ_TAB" when expecting one of the
following:
:= . ( # % ;
The symbol ":=" was substituted for "PICU_OBJ_TAB" to continue.
Please suggest what I am doing wrong here.
There are two problems with this code:
First: In Oracle 11g you can not use varchar2(32767) the maximum length is 4000 for a varchar there. So even if the code did run, it wouldn't create the types.
Secondly: the PL/SQL code is validated/compiled when you run it. But as you use dynamic SQL to create the types, the PL/SQL compiler can't see those types when it tries to compile the lines:
picu_var picu_obj_tab;
picu_var := picu_obj_tab(picu_obj('101','xyz','pro-ssr-qr','12:13'));
and that's the error you are seeing.
You have to create the types before you run PL/SQL code that uses them.
i am trying to compile this function
CREATE OR REPLACE FUNCTION sql_error_msg (err_num PLS_INTEGER)
RETURN VARCHAR2 IS
BEGIN
RETURN sqlerrm(-err_num);
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END sql_error_msg
/
but i am getting this error:
FUNCTION SQL_ERROR_MSG
On line: 8
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
;
The symbol ";" was substituted for "end-of-file" to continue.
Just add ; at the end of the function.
END sql_error_msg;
/
I was trying to execute PL/SQL that "creates or replaces" a function, using <jdbc:initialize-database> tag. I was given the PLS-00103 and it turned out PL/SQL is not currently supported this way.
You have to execute the script in a different way.
I am going to try directly with JDBC as per http://www.coderanch.com/t/298532/JDBC/databases/run-SQL-Script-file-JDBC
and work out a solutiom from there.
When running the following code in Oracle 10g (pl/sql)
DECLARE
A NUMBER;
B NUMBER;
BEGIN:
A:=&N;
B:=&M;
IF (A>B)
DBMS_OUTPUT.PUT_LINE('THE MAXIMUM OF TWO NUMBER IS:' || TO_CHAR(A));
ELSE
DBMS_OUTPUT.PUT_LINE('THE MAXIMUM OF TWO NUMBERS IS:' || TO_CHAR(B));
END IF;
END;
I get the error 'BIND VARIABLE "A" NOT DECLARED' and I don't know why. What is causing this?
There are a couple of things wrong with your code.
The first problem is that there should be no colon after BEGIN. What you've written is being interpreted by Oracle as BEGIN :A := ..., and that should explain why you're getting an error about bind variable A.
The second problem is with the line IF (A>B). You need to add a THEN to the end.
Incidentally, you can use GREATEST(A, B) to return the larger of two numbers.
I've to work on some older EDW scripts, which I think are in PL/SQL and queries fech data from Oracle table as well. But there is some problem with them, the part which declares variables, as shown in the image gives error. I'm unable to understand why?
Below is some part of script,
VARIABLE begin_exp_date varchar2(8)
VARIABLE end_exp_date varchar2(8)
VARIABLE begin_cal_key number
Declare
begin
:begin_exp_date := 'begin_exp_date';
:end_exp_date := 'end_exp_date';
:begin_cal_key := 'begin_cal_key';
end;
These lines produce error ORA-00900: Invalid SQL statement.
Any help?
If you plug the script in SQL*Plus, it will be executed without the ORA-00900 error. I guess you received the error when it was run in Toad.
If it is indeed PL/SQL, it should be more like this
DECLARE
begin_exp_date varchar2(8);
end_exp_date varchar2(8);
begin_cal_key number;
BEGIN
begin_exp_date := 'begin_exp_date';
-- and so on
END;
You can set constant values to the variables in the DECLARE section if you want. Note that you've defined begin_cal_key as a NUMBER so cannot assign the string 'begin_cal_key'