|
|
Re: Ref cursor ? [message #648390 is a reply to message #648387] |
Mon, 22 February 2016 19:14 |
|
Barbara Boehmer
Messages: 9090 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
M123 wrote on Mon, 22 February 2016 16:05
any suggestion on this and please suggest me any good resource to understand ref cursor if possible. ?
http://docs.oracle.com/database/121/LNPLS/static.htm#LNPLS00605
M123 wrote on Mon, 22 February 2016 16:05
any suggestion what exactly it is doing.
It is producing a lot of errors. The following contains minimal corrections to make make it run without errors the way it seems to be trying to do it, which is not necessarily a good way, especially not knowing exactly what is was intended to do. It declares some variables of different kinds, assigning the value 10 to a variable v_deptid, then checks whether that variable is null, which will never happen, since the value is set to 10, not some changeable parameter. If it had been null, it would have opened a ref cursor for the stmt "select * from employees". Since it is not null, it modifies the stmt to select only the salary for those in department id 10 and opens the ref cursor for that. It then loops, fetching from the ref cursor into emprec, outputting the salary for each record. Then it closes the ref cursor.
SCOTT@orcl> declare
2 type emp_refcursor is ref cursor;
3 emp_cv emp_refcursor;
4 emprec employees%rowtype;
5 stmt varchar2(100) := 'select * from employees';
6 v_deptid number := 10;
7 begin
8 if v_deptid is null then
9 open emp_cv for stmt;
10 else
11 -- need space before "where" and
12 -- need "department_id = :b_deptid" instead of "department_id := v_deptid" to use bind variable:
13 stmt := stmt || ' where department_id = :b_deptid';
14 -- need to open modified stmt here, not after endif:
15 open emp_cv for stmt using v_deptid;
16 end if;
17 loop
18 fetch emp_cv into emprec;
19 -- need exit command to avoid endless loop:
20 exit when emp_cv%notfound;
21 dbms_output.put_line (emprec.salary);
22 end loop;
23 close emp_cv;
24 end;
25 /
2450
5000
1300
PL/SQL procedure successfully completed.
|
|
|
|