Home » SQL & PL/SQL » SQL & PL/SQL » Ref cursor ? (4.1.3 sql developer)
Ref cursor ? [message #648387] Mon, 22 February 2016 18:05 Go to next message
M123
Messages: 47
Registered: February 2016
Location: USA
Member


declare
type emp_refcursor  is ref cursor;
emp_cv emp_refcursor;
emprec employees%rowtype;
stmt varchar2(100):= 'select * from employees';
   v_deptid number :=10;
begin
   if v_deptid is  null then open emp_cv for stmt;
else
    stmt :=stmt ||'where department_id := v_deptid';
end if;
    open emp_cv for stmt using v_deptid;
loop
fetch emp_cv into emprec;
dbms_output.put_line(emprec.salary);
end loop;
close emp_cv;
end;

error:ORA-00933: SQL command not properly ended

any suggestion on this and please suggest me any good resource to understand ref cursor if possible. ?
got it from youtube ,any suggestion what exactly it is doing.

Re: Ref cursor ? [message #648389 is a reply to message #648387] Mon, 22 February 2016 18:12 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
stmt contains 'select * from employeeswhere department_id := v_deptid';
so few lines & so many errors!


NEVER do in PL/SQL that which can be done in plain SQL!
Re: Ref cursor ? [message #648390 is a reply to message #648387] Mon, 22 February 2016 19:14 Go to previous messageGo to next message
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.







Re: Ref cursor ? [message #648482 is a reply to message #648390] Tue, 23 February 2016 17:57 Go to previous message
M123
Messages: 47
Registered: February 2016
Location: USA
Member

thankyou
Previous Topic: quoting string in multiple lines
Next Topic: ORA-00001: unique constraint (UNKNOWN.obj#=179690) violated
Goto Forum:
  


Current Time: Thu Apr 25 08:55:06 CDT 2024