Pass cursor name from a variable - oracle

I have a package that contains a procedure(takes an input parameter;cursor name) and two cursors.
The procedure is to use the cursor specified by the input parameter
PROCEDURE insertdetails(typ IN VARCHAR2) IS
BEGIN
OPEN typ;
LOOP
FETCH typ INTO colA;
EXIT WHEN typ%notfound;
--MISSING CODE
END LOOP;
END;
if i run the procedure and pass on of the name of the cusrsor as an input parameter i get the error PLS-00456: item 'TYP' is not a cursor
Is their a way out of this

You will need to use REF CURSORS to do this. Here's a pretty good explanation.
Your procedure definition would look like this:
PROCEDURE insertdetails(typ IN sys_refcursor) IS
You would probably not open the cursor in this procedure, generally you open it elsewhere, in the code where the cursor is defined.

Assuming the cursors are declared elsewhere in the package, you could use the parameter to decide which actual cursor to work with, something like:
PROCEDURE insertdetails(typ IN VARCHAR2) IS
BEGIN
IF typ = 'CURSOR_A' THEN
OPEN cursor_a;
LOOP
FETCH cursor_a INTO colA;
EXIT WHEN cursor_a%notfound;
--MISSING CODE
END LOOP;
ELSE
OPEN cursor_b;
LOOP
FETCH cursor_b INTO colA;
EXIT WHEN cursor_b%notfound;
--MISSING CODE
END LOOP;
END IF;
END;
Or if the missing code is common, as seems likely:
PROCEDURE insertdetails(typ IN VARCHAR2) IS
BEGIN
IF typ = 'CURSOR_A' THEN
OPEN cursor_a;
ELSE
OPEN cursor_b;
END IF;
LOOP
IF typ = 'CURSOR_A' THEN
FETCH cursor_a INTO colA;
EXIT WHEN cursor_a%notfound;
ELSE
FETCH cursor_b INTO colA;
EXIT WHEN cursor_b%notfound;
END IF;
--MISSING CODE
END LOOP;
END;
Either way you might prefer a case over an if; particularly if the number of possible cursors that typ can represent grows.

Related

String Comparision in If condition in PL/SQL

I have the following code
create or replace procedure deact_user (i_email in varchar2)
as
var1 varchar2(200);
begin
for em_id in (select abc.emai_id from abc)
loop
if (i_email <> em_id) then
dbms_output.put_line('Not working');
else
dbms_output.put_line('Working');
end if;
end loop;
end;
I need to compare the i_email which is a input parameter with em_id which is a for loop which loops the table abc having field as emai_id.
Iam facing error PLS=00306 wrong type of arguments in call to '!='
Please help
When you use a for loop with select, its creates a record type. To access the value, you have to change your if to this:
if (I_email <> em_id.emai_id)
.....
That should solve your problem. Now, on the other hand, it would be quicker (and easier) to just query with a where clause using your variable. (that way, you wouldn't need a for loop).
I recommend you to use Cursors for read content and compare values like this:
create or replace procedure deact_user (i_email in varchar2)
as
var_id varchar2(200);
cursor cur_em_id is select abc.emai_id from abc;
begin
open cur_em_id;
loop
fetch cur_em_id into var_id;
exit when cur_em_id%NOTFOUND;
if (i_email <> var_id) then
dbms_output.put_line('Not working');
else
dbms_output.put_line('Working');
end if;
end loop;
close cur_em_id;
end;

How to build a dynamic PLSQL query to fetch records?

I am trying to create a stored procedure in Oracle and make a dynamic query work to get a bunch of records. I have read many examples but so far I can't get this to work unless I do this:
CREATE OR REPLACE PROCEDURE GiveMeResultSet(
v_par1 IN CHAR,
v_par2 IN CHAR,
v_par3 IN CHAR,
v_par4 IN VARCHAR2,
v_par5 IN VARCHAR2,
v_par6 IN VARCHAR2,
cur_typ OUT SYS_REFCURSOR)
IS
BEGIN
OPEN cur_typ FOR 'select * from complex_query';
--CLOSE cur_typ;
END;
And I am executing it this way:
var c refcursor;
execute GiveMeResultSet(null,null,null,null,null,null,:c);
print c;
This way I get the header names and the records from the query, but I am not closing the cursor that is fetching the results. If I close it then I get nothing at all. I guess leaving it open could cause some kind of memory leak problem at some point.
I have seen similar cases in Oracle documentation where they do something like this:
sql_stmt := 'SELECT * FROM emp';
OPEN emp_cv FOR sql_stmt;
LOOP
FETCH emp_cv INTO emp_rec;
EXIT WHEN emp_cv%NOTFOUND;
-- process record
END LOOP;
CLOSE emp_cv;
But I have no clue what goes on the "process record" part of the code which would allow to get the whole set of records at the end, plus that my record has a complex structure that doesn't fit with a fixed set of fields as in a table.
Can you please show me the proper way to do this?.
Thanks a lot.
ok CodeRoller, here is my sample code for a unspecified ref cursor:
Code of the Function which returns the ref cursor:
create or replace function test_ref_cursor(pi_sql_statement in varchar2) return SYS_REFCURSOR is
result_cursor SYS_REFCURSOR;
begin
open result_cursor for pi_sql_statement;
return result_cursor;
end;
Now, in the next step I use this function to get data from v$parameter:
declare
type t_my_cursor is ref cursor;
my_cursor t_my_cursor;
l_rec v$parameter%rowtype;
begin
my_cursor := test_ref_cursor('select * from v$parameter');
loop
fetch my_cursor into l_rec;
exit when my_cursor%notfound;
dbms_output.put_line(l_rec.name || ' = ' || l_rec.value);
end loop;
close my_cursor;
end;
Take care, to close the ref-cursor!

return data as cursor from function in pl/sql wiithout create type oracle 11g

I wrote this code in pl/sql but I couldnt take answer.
create or replace function mostafa.sbs_Topic_LedgerBalance8Column
(BranchID number,DateFrom number,DateTo number)
RETURN SYS_REFCURSOR
IS O_RESULT SYS_REFCURSOR;
BEGIN
open O_RESULT for
Select s* From Mostafa.topic ;
RETURN O_RESULT;
end sbs_Topic_LedgerBalance8Column;
and I called it this way:
DECLARE v_refcursor SYS_REFCURSOR;
BEGIN
v_refcursor :=mostafa.sbs_topic_ledgerbalance8column(12,12,12);
FOR employee_rec IN v_refcursor
LOOP
DBMS_OUTPUT.put_line (
employee_rec.ID);
END LOOP;
end;
why did I get error when I retrieve result?
error is :v_refcursor is not a procedure or is undefined
When you are using a refcursor, you can't access it by using the cursor for loop. Use something like the following instead (Untested):
DECLARE
v_refcursor SYS_REFCURSOR;
v_emp_rec topic%ROWTYPE;
BEGIN
v_refcursor :=mostafa.sbs_topic_ledgerbalance8column(12,12,12);
LOOP
FETCH v_refcursor INTO v_emp_rec;
EXIT WHEN v_refcursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_rec.id);
END LOOP;
close v_refcursor;
END;

Need an alternative solution to this oracle query?Without using (flag) variable

Question:
Create a Doctor table (Docname, Qualification, Specialization, Working_shift).
Use parameterized cursor to check the availability of doctors given the specialization
and working shift of the day to serve the patients
I am just learning databases so if the question may seem trivial i apologize for that.
Getting the desired output on inputting the values but i need an alternative way to solve the question without using flag variable (so that i could get the exception)...if i don't use the flag it prints the exception as well as the docname and qualification
I am using oracle(cursor in a normal pl/sql block) to execute this query.
Solution:
--table creation
create table doctor
(
docname varchar2(20),
qualification varchar2(20),
specialization varchar2(20),
shift varchar2(20)
)
my solution
declare
cursor c1 (specialization varchar2,shift varchar2) is select docname,qualification from doctor
where specialization='&sp' and shift='&shift'
sp doctor.specialization%type;
shift doctor.shift%type;
flag number(10);
begin
flag:=0;
for r1 in c1(sp,shift)
loop
if c1%found then
flag:=1;
dbms_output.put_line('Doctor is available');
dbms_output.put_line('Docname: '||r1.docname);
dbms_output.put_line('qualification: '||r1.qualification);
else
flag:=0;
end if;
end loop;
if flag=0 then
dbms_output.put_line('Invalid specialization/shift');
end if;
end;
Try given below code
declare
cursor c1 (specialization varchar2,shift varchar2)
is
select docname,qualification
from doctor
where specialization='&sp'
and shift='&shift'
sp doctor.specialization%type;
shift doctor.shift%type;
flag number(10);
begin
flag:=0;
for r1 in c1(sp,shift)
loop
if c1%found then
flag:=1;
dbms_output.put_line('Doctor is available');
dbms_output.put_line('Docname: '||r1.docname);
dbms_output.put_line('qualification: '||r1.qualification);
else
raise;
end if;
end loop;
exception
when others then
dbms_output.put_line('Invalid specialization/shift');
end;
You don't need to reset the flag within the loop, you already initialised it to 0 at the start of the procedure.
You dont need to check c1%found because you're inside the loop; by definition a record was found, otherwise it wouldn't go into your loop code.
Your cursor should use the variables provided, not the SQL*Plus substitution variables, e.g.:
cursor c1 (specialization varchar2,shift varchar2) is
select docname,qualification
from doctor
where doctor.specialization=c1.specialization
and doctor.shift=c1.shift;
If you don't want to have to use all those aliases, you can use a naming convention to distinguish between the different identifiers (shift vs shift), e.g.:
cursor c1 (i_specialization varchar2, i_shift varchar2) is
select docname,qualification
from doctor
where specialization=i_specialization
and shift=i_shift;
Note also, you missed a semicolon at the end of the query.
Finally:
If you change your loop as follows, it should work fine:
for r1 in c1(&sp,&shift)
loop
flag:=1;
dbms_output.put_line('Doctor is available');
dbms_output.put_line('Docname: '||r1.docname);
dbms_output.put_line('qualification: '||r1.qualification);
end loop;
Now, your last bit of code:
if flag=0 then
dbms_output.put_line('Invalid specialization/shift');
end if;
will work fine - it will only execute if flag is still 0 (i.e. the query found no rows).
If you do not use parameters in "c1" cursor you do not need it...
DECLARE
CURSOR c1 IS
SELECT docname, qualification
FROM doctor
WHERE specialization = '&sp'
AND shift = '&shift';
TYPE c1_ntt IS TABLE OF c1%ROWTYPE;
l_c1 c1_ntt;
BEGIN
OPEN c1;
FETCH c1 BULK COLLECT INTO l_c1;
CLOSE c1;
IF l_c1.COUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'Invalid specialization/shift');
END IF;
FOR indx IN l_c1.FIRST..l_c1.LAST LOOP
DBMS_OUTPUT.PUT_LINE('Doctor is available');
DBMS_OUTPUT.PUT_LINE('Docname: ' || l_c1(indx).docname);
DBMS_OUTPUT.PUT_LINE('qualification: ' || l_c1(indx).qualification);
END LOOP;
END;

Using of cursors as function output in procedure (Oracle)

I'm trying to find a solution but all the time something is wrong. So what is my problem:
I have a function:
function fun1 (
p_param1 number) return sys_refcursor
is
c_result sys_refcursor;
begin
open c_result for select e.username, c.contract_id from employees e
join contracts c on c.employee_id = e.employee_id;
return c_result;
end fun1;
I want to use this function inside my stored procedure:
procedure proc1 (...)
is ...
cur_contract sys_refcursor;
begin
...
open cur_contract for fun1(p_param1);
loop
fetch cur_contract into v_username, v_contract_id;
exit when cur_contract%notfound;
...
end loop;
close cur_contract;
...
end proc1;
And I get error: expression is of wrong type in line "open cur_contract for fun1(p_param1);"
What should I change to make my procedures work?
You've already opened the cursor in fun1. Try the following:
procedure proc1 (...)
is
...
cur_contract sys_refcursor;
begin
...
cur_contract := fun1(p_param1);
loop
fetch cur_contract into v_username, v_contract_id;
exit when cur_contract%notfound;
...
end loop;
close cur_contract;
...
end proc1;
I hope this helps.

Resources