CREATE PROCEDURE Pname(in_empno IN NUMBER out_name OUT VARCHAR2)
AS
BEGIN
select EmpName into out_name from emptable where Empno = in_empno;
END Pname;
In the above procedure, how can I check if the SELECT query executed successfully or not with the given condition?
You can use the EXCEPTION block to determine if rows were returned or if some other exceptions occurred. Try this
BEGIN
SELECT EmpName into out_name from emptable where Empno = in_empno
EXCEPTION
WHEN NO_DATA_FOUND THEN
out_name := NULL;
END;
If you would like to see the output of out_name parameter try as follows
CREATE OR REPLACE PROCEDURE pname (in_empno IN NUMBER, out_name OUT VARCHAR2)
IS
BEGIN
SELECT empName
INTO out_name
FROM emptable
WHERE empno = in_empno;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
raise_application_error (
-20000,
'Unable to execute procedure because of: ' || SQLERRM
);
END pname;
This procedure can be executed from sql plus as
var ret varchar2(512);
exec pname(2345,:ret);
print ret;
Update 1
If you would like to return a value if sql query executes successfully and if there is an error return another value then do as
CREATE OR REPLACE PROCEDURE pname (in_empno IN VARCHAR2,
out_name OUT VARCHAR2,
returnval IN OUT NUMBER
)
IS
BEGIN
SELECT employee_name
INTO out_name
FROM employees
WHERE emp_number = in_empno;
returnval := 0;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
returnval := 1;
END pname;
and call the procedure from sql plus as
var ret varchar2(512);
var ret2 number;
exec pname(2346,:ret,:ret2);
print ret;
print ret2;
In this example i can stop using one more out variable for return right? and ill check for the condition in my front end like if 'out_name' returns null ill handle in someway, if it returns value it'll be handled in other way, Which one is efficient?????Using one more out variable for Retvalue, or doing like this
CREATE OR REPLACE PROCEDURE pname (in_empno IN VARCHAR2, out_name OUT VARCHAR2)
IS
BEGIN
SELECT employee_name
INTO out_name
FROM employees
WHERE emp_number = in_empno;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
out_name := NULL;
END;
Related
whenever i call this function it will return error like - invalid cursor
so, this is the body part of the package, the package specification also return a sys_refcursor. the error message shows that sys_refcursor invalid cursor.
FUNCTION SUMMARY
(i_name VARCHAR2, i_id VARCHAR2, i_label VARCHAR2)
RETURN SYS_REFCURSOR
IS
rc_result SYS_REFCURSOR;
v_sql CLOB;
Server VARCHAR2(100) := '#AI';
v_r_count NUMBER;
v_sp_count VARCHAR2(200);
V VARCHAR2(10);
BEGIN
EXECUTE IMMEDIATE 'select count(*) from run'||Server||' where id='''||i_id||''' and label='''||i_label||''' and re is not null' INTO v_r_count;
EXECUTE IMMEDIATE 'select table_name from all_tables where table_name like ''%se%'' and owner ='''||i_name||'''' INTO v_sp_count;
IF v_r_count > 0 THEN
BEGIN
v_sql := 'select RE from run'||v_Server||' where id='''||i_id||''' and label='''||i_label||'''';
END;
ELSIF v_sp_count IS NOT NULL THEN
BEGIN
v_sql := 'SELECT
process,
desc,
p_desc,
date
FROM
'||i_name||'.se
WHERE
errors = 1
AND lower(p_desc) LIKE lower(''%summary%'')';
END;
ELSE v_sql := NULL;
END IF;
OPEN result FOR v_sql;
RETURN result;
EXCEPTION
WHEN no_data_found THEN
RETURN NULL;
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END;
You can simply assign NULL to the refcursor like this: Example
PROCEDURE test( id_number IN VARCHAR2,
resultIN OUT SYS_REFCURSOR) AS
BEGIN
if false then
OPEN resultIN FOR
SELECT dummy
from dual;
ELSE
resultIN := null;
END IF;
END;
I have create a PL/SQL stored procedure from which I want to return output and display it.
I want to return the output either the success or failure message into variable : MSG
How do I need to do this any idea
PL/SQL code:
CREATE OR REPLACE PROCEDURE CHECKFILED
(
MY_NAME EMP.FIRSTNAME%TYPE,
MY_ID EMP.ID%TYPE
) RETURN VARCHAR2
IS
V_MSG VARCHAR(4000 CHAR);
BEGIN
SELECT FIRSTNAME INTO MY_NAME FROM EMP WHERE ID=MY_ID;
IF MY_NAME IS NOT NULL THEN
INSERT INTO CUSTOMER VALUES (UID,FIRSTNAME);
V_MSG='FIELD IS NOT EMPTY';
ELSE
RAISE_APPLICATION_ERROR(-20000,'FIELD IS EMPTY');
END IF;
EXCEPTION
WHEN OTHERS THEN
V_MSG := SQLCODE || '-' || SQLERRM;
RETURN(V_MSG);
RETURN(V_MSG);
END;
Calling the stored procedure:
DECLARE
MSG VARCHAR2(4000 CHAR);
BEGIN
SELECT CHECKFILED('10A') AS MSG FROM DUAL;
END;
If it is a procedure, it should have an OUT parameter which will then be used to return some value.
Something like this; note that it is probably useless to check whether select returned null (employees do have names; select would return no row and therefore raise no_data_found exception).
CREATE OR REPLACE PROCEDURE checkfiled (my_id IN emp.id%TYPE,
v_msg OUT VARCHAR2)
IS
my_name emp.firstname%TYPE;
BEGIN
SELECT firstname
INTO my_name
FROM emp
WHERE id = my_id;
INSERT INTO customer
VALUES (UID, firstname);
v_msg := 'FIELD IS NOT EMPTY';
EXCEPTION
WHEN OTHERS
THEN
v_msg := SQLCODE || '-' || SQLERRM;
END;
You'd then call it as
DECLARE
msg VARCHAR2 (4000 CHAR);
BEGIN
checkfiled ('10A', msg);
DBMS_OUTPUT.put_line ('Procedure returned ' || msg);
END;
/
PROCEDUREs do not have any return value. If you want to return a value, it needs to be a FUNCTION. Try the code below.
CREATE OR REPLACE FUNCTION CHECKFILED (MY_NAME EMP.FIRSTNAME%TYPE, MY_ID EMP.ID%TYPE)
RETURN VARCHAR2
IS
V_MSG VARCHAR (4000 CHAR);
BEGIN
SELECT FIRSTNAME
INTO MY_NAME
FROM EMP
WHERE ID = MY_ID;
IF MY_NAME IS NOT NULL
THEN
INSERT INTO CUSTOMER
VALUES (UID, FIRSTNAME);
V_MSG := 'FIELD IS NOT EMPTY';
ELSE
RAISE_APPLICATION_ERROR (-20000, 'FIELD IS EMPTY');
END IF;
RETURN (V_MSG);
EXCEPTION
WHEN OTHERS
THEN
V_MSG := SQLCODE || '-' || SQLERRM;
RETURN (V_MSG);
END;
I have created a procedure for updating my t_ritm table. First I have select rrcd_qnty (which is my product quantity) of a product id from t_rrcd table. Then I update the rrcd_qnty value in t_ritm table.
Here is my procedure:
procedure update_ritm_new_rate(p_oid in varchar2, p_ritm_rate in varchar2, p_euser in varchar2)
is
nrate varchar2(4);
begin
SELECT rrcd_rate into nrate
FROM (select oid, t_rrcd.rrcd_rate
from t_rrcd
where rrcd_ritm= p_oid
ORDER BY oid DESC )
WHERE rownum <= 1
ORDER BY rownum DESC ;
EXCEPTION
WHEN NO_DATA_FOUND THEN nrate := 0;
update t_ritm
set ritm_rate = nrate, euser = p_euser, edat = sysdate
where oid = p_oid;
commit;
end update_ritm_new_rate;
Some of my product id Quantity was null. so I was getting No_Data_Found error. But when and which product id has Quantity value they were successfully updating. For avoiding No_Data_Found I used EXCEPTION WHEN NO_DATA_FOUND THEN nrate := 0; which solved my no_Data_Found error. But when product id has quantity value they were not updating.
I had search lot of for this issue but not get good solution. What should be the best practice for avoiding No_Data_Found error? Could I pass my value if I don't get any No_Data_Found error?
thank in advance
That's because - if your SELECT returns something, it never reaches UPDATE as it is hidden behind the EXCEPTION handler.
Therefore, enclose it (SELECT) into its own BEGIN-END block, and put UPDATE out of it so that it is executed with whichever NRATE value is used.
PROCEDURE update_ritm_new_rate (p_oid IN VARCHAR2,
p_ritm_rate IN VARCHAR2,
p_euser IN VARCHAR2)
IS
nrate VARCHAR2 (4);
BEGIN
BEGIN --> this
SELECT rrcd_rate
INTO nrate
FROM ( SELECT oid, t_rrcd.rrcd_rate
FROM t_rrcd
WHERE rrcd_ritm = p_oid
ORDER BY oid DESC)
WHERE ROWNUM <= 1
ORDER BY ROWNUM DESC;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
nrate := 0;
END; --> this
UPDATE t_ritm
SET ritm_rate = nrate, euser = p_euser, edat = SYSDATE
WHERE oid = p_oid;
COMMIT;
END update_ritm_new_rate;
I have fixed the issue by adding EXCEPTION WHEN NO_DATA_FOUND THEN nrate := 0; after the update query.
procedure update_ritm_new_rate(p_oid in varchar2, p_ritm_rate in varchar2, p_euser in varchar2)
is
nrate varchar2(4);
begin
SELECT rrcd_rate into nrate FROM (select oid, t_rrcd.rrcd_rate from t_rrcd where rrcd_ritm= p_oid ORDER BY oid DESC )
WHERE rownum <= 1 ORDER BY rownum DESC ;
update t_ritm set ritm_rate = nrate, euser = p_euser, edat = sysdate where oid = p_oid;
commit;
EXCEPTION WHEN NO_DATA_FOUND THEN nrate := 0;
end update_ritm_new_rate;
What I have to do is to INSERT in table "info" different content, depending on the select result: if it is one row, no rows or more than one row.
I want to set the outretvalue variable on the exception section, then do the insert in the IF section, depending on outretvalue value.
Anyway, I get an error at compiling saying that f2 function is in an invalid state. I have 2 errors: for the INSERT and for not recognising rowcount. Why?
CREATE OR REPLACE FUNCTION f2 (v_nume employees.last_name%TYPE DEFAULT 'Bell')
RETURN NUMBER
IS
salariu employees.salary%type;
outretvalue number(2) := 0;
BEGIN
SELECT salary
INTO salariu
FROM employees
WHERE last_name = v_nume;
RETURN salariu;
EXCEPTION
WHEN NO_DATA_FOUND THEN
outretvalue := 1;
WHEN TOO_MANY_ROWS THEN
--at this row I have 2 errors: for the INSERT and for not recognising rowcount
INSERT INTO info(`no_lines`) VALUES(SQL%ROWCOUNT);
END f2;
/
SELECT f2('King') FROM dual;
Your function:
DECLARE
BEGIN
END;
... something
END;
Add another BEGIN at begin or move your IF inside existing BEGIN END block and remove second END.
EDIT: after clarification
CREATE OR REPLACE FUNCTION f2 (v_nume employees.last_name%TYPE DEFAULT 'Bell')
RETURN NUMBER
IS
salariu employees.salary%type;
outretvalue number(2) := 0;
BEGIN
SELECT salary
INTO salariu
FROM employees
WHERE last_name = v_nume;
RETURN salariu;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN -1;
WHEN TOO_MANY_ROWS THEN
SELECT count(*)
INTO salariu
FROM employees
WHERE last_name = v_nume;
INSERT INTO info(no_lines) VALUES(salariu);
RETURN -2;
WHEN OTHERS THEN
RETURN -3;
END f2;
/
SET SERVEROUTPUT on
DECLARE
l_ret NUMBER;
BEGIN
dbms_output.put_line(f2('Bell'));
dbms_output.put_line(f2('noBell'));
dbms_output.put_line(f2('King'));
END;
Try this. It will definelty help you out.
CREATE OR REPLACE FUNCTION f2(
v_nume employees.last_name%TYPE DEFAULT 'Bell')
RETURN NUMBER
IS
salariu employees.salary%type;
outretvalue NUMBER(2) := 0;
lv_cnt PLS_INTEGER;
BEGIN
SELECT salary INTO salariu FROM employees WHERE last_name = v_nume;
RETURN salariu;
EXCEPTION
WHEN NO_DATA_FOUND THEN
outretvalue := 1;
WHEN TOO_MANY_ROWS THEN
SELECT COUNT(1) INTO lv_cnt FROM employees WHERE last_name = v_nume;
INSERT INTO info
( no_lines
) VALUES
( lv_cnt
);
RETURN 2;
WHEN OTHERS THEN
RETURN 3;
END f2;
Oracle saves the compile errors in a table. I use the following query for retrieving the PL/SQL errors in my stored procs/funcs:
SELECT '*** ERROR in ' || TYPE || ' "' || NAME || '", line ' || LINE || ', position ' || POSITION || ': ' || TEXT
FROM SYS.USER_ERRORS
You could try running it and see if it helps identify the error in the function.
I creates a procedure using Toad Client that is
create or replace procedure getuid(eid_pro varchar2)is
l_value number;
begin
select uniqueid
into l_value
from enrollment
where eid=eid_pro;
end ;
When I execute it by Followings
begin
getuid('245698154');
end;
It executed successfully but result is not displayed in data grid.
Please help me in this
Turn the procedure into a function.
create or replace function getuid(eid_pro varchar2) return number is
l_value number;
begin
select uniqueid
into l_value
from enrollment
where eid = eid_pro;
return l_value;
end;
And then select the function.
select getuid('245698154') from dual;
In that case please use a function and then you can use it in a select statement which can show the result in the grid.
create or replace function getuid(eid_pro varchar2) return number is
l_value number;
begin
select uniqueid
into l_value
from enrollment
where eid=eid_pro;
return l_value;
end ;
select getuid('245698154') from dual;