Sie sind auf Seite 1von 2

Cursors: begin

Cursor is a private SQL area where oracle can for rec in c1(10) loop
manipulate the data. dbms_output.put_line(rec.ename||rec.sal);
declare end loop;
v_ename emp.ename%TYPE; end;
v_sal emp.sal%TYPE; We will pass the values during run time.
cursor c1 is Declare
SELECT ename, sal FROM emp; Cursor c_dept is
Begin Select deptno from dept;
Open c1; Cursor c1(v_deptno number) is
Loop Select ename, sal from emp
Fetch c1 into v_ename, v_sal; Where deptno = v_deptno;
Exit when c1%notfound; Begin
End loop; For rec in c_dept loop
Close c1 dbms_output.put_line(dept no is: ||rec.deptno);
End; for c1_rec in c1(rec.deptno) loop
dbms_output.put_line(c1_rec.ename||c1_rec.sal);
CURSOR FOR LOOP: end loop;
We dont need to write cursor life cycle which is end loop;
open, fetch and close if we use cursor for loop. end;
declare
cursor c1 is REF CURSOR:
select ename, sal from emp; A ref cursor is a data type in the oracle. Refcusor
begin also referred as cursor variables. Cursor variables
for rec in c1 loop are like pointers to result set. Cursor can be
dbms_output.put_line(rec.ename||rec.sal); attached to only one query while refcursor can be
end loop; used to multiple queries at run time.
end; Declare
TYPE cur_type IS REFCURSOR RETURN
cursor aattributes: emp%ROWTYPE;
1) c1% ISOPENdefault false Refcur1 empcurtype;
2) c1%NOTFOUND Begin
3) c1%FOUND Open refcur1 for select * from emp;
4) c1%ROWCOUNT Open refcur1 for select * from dept;
End;
CURSOR TYPES: REFCURSOR TYPES
1) IMPLICIT CURSOR: Strong ref cursor, Week ref cursor,
It will be created by Oracle and name will be given SYS-REFCURSOR
by oracle internally maintains the cursor life cycle. 1) Strong ref cursor:
2) EXPLICIT CURSOR: Refcursors which has a return type is classified as
It will be created by user and user has to give the strong refcursor
cursor name. User must maintain the cursor life Declare
cycle. TYPE empcurtype IS REFCURSOR RETURN
emp%ROWTYPE
CURSOR WITH PARAMETERS: Begin
Declare NULL;
Cursor c1(v_deptno Number) is End;
Select ename, sal from emp
where deptno=v_deptno; 2) Weak ref cursor:
Refcursors which has no return type is classified as
weak ref cursor.
Declare
TYPE empcurtype IS REFCURSOR;
Begin
NULL;
End;

3) SYSTEM REF CURSOR:


This is system defined ref cursor. This is also
considered weak. System ref cursor need not to
declare explicitly.
Empcur SYS-REFCURSOR;

Advantages:
Refcursor itself is a datatype an easy to declare .
More flexible because it is not tied with any
specific query. Easily pass as argument from sub
routine to subroutine. Cursor variables are bind
variable.
Disadvantages:
Refcursors are not efficient as static cursors. Need
additional code to print refcursor values.
SELECT FOR UPDATE:
The select for update statement allows you to lock
the records in the cursor result set. You are not
required to make changes to the records in order
to use this statement. The record locks are
released when the next commit or rollback
statement is issued.
Cursor c1 is
Select id, name from emp for update of name;
Where current of clause:
If you plan on updating or deleting records that
have been referenced by a SELECT FOR UPDATE
statement, you can use the where current of
statement.
Update emp
Set set_clause
Where current of cursor_name;

Das könnte Ihnen auch gefallen