Vous êtes sur la page 1sur 7

Cursors Def: -> Cursor is also a synonym for context area- a wor area in memory where Oracle stores

the current Sql statement. -> Cursor is a temporary storage area which is created explicitly in RAM of a System. From that perticular area we can extract large volumes of data. Types Of Cursors: -> Implicit Cursors -> Explicit Cursors ----------------------------------------------------------------------------------------------> Implicit Cursor: This is created by its own in the SQL private area, whenever the user issues DML statements. The User can't manipulate with the data or fetch the dat a that is stored in Implicit Cursor. Implicit Cursor having the same name and same address as SQL is having. -> Attributes Of Implicit Cursors: (a) %Isopen - This will give wether the cursor is open or not. (b) %Found - This is to find wether the cursor is created or not . (c) %NotFound - This is to find wether the cursor is not create d or created. (d) %Rowcount - This is to count how many rows has been deleted or inserted. E.g. Begin Delete from emp where ename='SCOTT'; if sql%found then dbms_output.put_line('Record(s) Is Found'||'No.Of Records:'||sql%ro wcount); else dbms_output.put_line('Not Found'); end if; End; ----------------------------------------------------------------------------------------------> Explicit Cursors: When a query returns multiple rows, you can explicitly declare a cursor to process the rows. Moreever you can declare a cursor in the declarative part of any Pl/Sql bloc , subprogram, or pac age. -> Three Commands Over a Cursor: -> Open : Which identifies the result set. -> Fetch: To retrieve the first row, you can execute Fetch repeatedly until all rows have been retrieved. -> Close: When the last row has been processed,you can release the cursor with the Close statement. -> Declaring a Cursor: -> Syntax: Cursor Cursor_Name [(para1 [,para2]...)]

[Return Return_Type] Is Sql Query. E.g. Cursor c1 Is Select * from emp Where deptno=20; E.g. Cursor c2 Return emp%Rowtype Is Select * from emp where sal>=3000; -> Opening The Cursor: Opening the cursor executes the query and identifies the result set. Rows in the result set are not retieved when the Open statement is executed. Rather, the Fetch Statement retrieves the rows. -> Syntax: Open Cursor_Name; -> Fetch: The Fetch Statement retrieves the rows in the result set one at a time. After each fetch,the cursor advances to the next row in the result set. E.g. Fetch c1 into my_empno,my_ename,my_deptno; Fetch c1 into my_tuple;

-> Close: The Close statement disables the cursor, and the result set becomes Undefined. E.g. Close c1; -> E.g. Declare cursor c is select * from emp; e emp%rowtype; TotSal emp.sal%type; AnlSal emp.sal%type; Begin open c; loop fetch c into e; if e.comm is null then TotSal:=e.sal; else TotSal:=e.sal+e.comm; end if; AnlSal:=TotSal*12; dbms_output.put_line(e.ename||' whos a '||e.job||' draws Rs.'||Totsal ||' per month and Rs.'||AnlSal||' per Year'); exit when c%NOTFOUND; end loop; close c; End; -> E.g. Declare cursor c is select * from emp; e emp%rowtype; TotSal emp.sal%type; AnlSal emp.sal%type; Begin open c; loop fetch c into e; if e.comm is null then TotSal:=e.sal;

else TotSal:=e.sal+e.comm; end if; AnlSal:=TotSal*12; dbms_output.put_line(e.ename||' '||AnlSal); exit when c%NOTFOUND; end loop; close c; End;

'||e.job||'

'||Totsal ||'

-> E.g. Declare cursor c is select * from emp where sal between 3000 and 5000; e emp%rowtype; Begin if c%IsOpen then dbms_output.put_line('Cursor is Already Opened'); else open c; end if; loop fetch c into e; dbms_output.put_line(e.ename||' '||e.job||' '||e.sal); exit when c%NOTFOUND; end loop; dbms_output.put_line(c%ROWCOUNT ||' Rows were displayed'); if c%IsOpen then close c; else dbms_output.put_line('Cursor is already closed'); end if; End; ---------------------------------------------------------------------------------------------> Passing Cursor Parameters: Using Open statement we can pass parameters to a Cursor. Unless you want to accept default values. Each formal parameter in the Cursor declaration must have a corresponding actual parameter in the OPEN statement. E.g. Declare dno emp.deptno%type; ejob emp.job%type; Cursor C1(dno number, ejob varchar) Is Select ................. -> -> -> OPEN C1(dno,'MANAGER'); OPEN C1(30.'CLERK'); OPEN C1(dno,ejob);

-> Cursor For Loop: -> E.g. declare cursor d is select * from dept; cursor e(dno number) is select * from emp where deptno=dno; begin for department in d loop dbms_output.put_line('DEPARTMENT NUMBER : ' || department.deptno);

dbms_output.put_line('--------------------------------'); for employee in e(department.deptno) loop dbms_output.put_line(employee.ename||' '||department.dname|| ' '||department.loc||' '||employee.job) ; end loop; end loop; end; -> E.g. Declare salary emp.sal%type; name emp.ename%type; cursor c is select ename,sal from emp; Begin open c; loop fetch c into name,salary; dbms_output.put_line(name||salary); exit when c%notfound; end loop; close c; End; --------------------------------------------------------------------------------------------Cursor Variables: -> Li e a Cursor, cursor variable points to the current row in the result set of multi-row query. Cursors differ from cursor variable, where as cursor is static and a cursor variable is dynamic becaz it is not tied to a specific query. -> Which holds the memory location of some item. So declaring the cursor variable creates a pointer. -> Uses: -> To pass query result sets between Pl/Sql stored subprograms and various clients. -> A query wor area remains accessible as long as any cursor variable points to it. Therefore u can pass the value of a cursor variable fre ely from one scope to another. -> Declaring Cursor Variable: Declare type DeptCurType Is ref cursor ; dept_cv DeptCurType; -> Ref. Cursors: -----------------> Implementing REF Cursor -> E.g. Declare tname varchar2(10):='&Table'; type MyRefCur is ref cursor; MyCur MyRefCur; e emp%rowtype; d dept%rowtype;

Begin if upper(tname)='EMP' then open MyCur for select * from emp; loop fetch MyCur into e; dbms_output.put_line(e.ename||'->'||e.job); exit when MyCur%NOTFOUND; end loop; close MyCur; else open MyCur for select * from dept; loop fetch MyCur into d; dbms_output.put_line(d.deptno||'->'||d.loc); exit when MyCur%NOTFOUND; end loop; close MyCur; end if; Exception when others then dbms_output.put_line(' Unhandled Exception '); End; -> E.g. Declare type ref_cur is ref cursor; MyCur ref_cur; t_name varchar2(20):='&Table_Name'; e emp%rowtype; d dept%rowtype; Begin if t_name='EMP' then open MyCur for select * from emp; loop fetch MyCur into e; dbms_output.put_line(e.ename||' - '||e.job); exit when MyCur%NOTFOUND; end loop; close MyCur; elsif t_name='DEPT' then open MyCur for select * from dept; loop fetch MyCur into d; dbms_output.put_line(d.dname||' - '||d.loc||' - '||d.deptno); exit when MyCur%NOTFOUND; end loop; Close MyCur; end if; End; /

declare cursor c is select * from emp; c1 emp%rowtype; --or c1 c%rowtype;

begin open c; loop fetch c into c1; dbms_output.put_line(c1.empno||' '||c1.ename||' '||c1.sal||' '||c1.dept no); exit when c%notfound; end loop; close c; end;

declare cursor c is select * from emp; c1 emp%rowtype; --or c1 c%rowtype; begin open c; if c%isopen then loop fetch c into c1; if c%found then dbms_output.put_line(c1.empno||' '||c1.ename||' '||c1.sal||' '||c1.deptno); else exit; end if; end loop; dbms_output.put_line(c%rowcount); else dbms_output.put_line('sorry there is no cursor'); end if; open c; exception when cursor_already_open then dbms_output.put_line('cursor already open'); end; //for cursor declare cursor c is select * from emp; begin for c1 in c loop dbms_output.put_line(c1.empno||' '||c1.ename||' '||c1.sal||' '||c1.deptno); end loop; end; declare cursor c is select * from emp; NO NUMBER; c1 c%rowtype; begin open c; loop fetch c into c1; exit when c%notfound; if c1.sal<2000 then

update emp set sal=2000 where empno=c1.empno; dbms_output.put_line(c1.empno||' '||c1.ename||' '||c1.sal||' '||'upd'); else delete from emp where empno=c1.empno; NO:=&NO; dbms_output.put_line('DELTED'); end if; end loop; dbms_output.put_line(c%rowcount); end;

Vous aimerez peut-être aussi