Presentation is loading. Please wait.

Presentation is loading. Please wait.

PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.

Similar presentations


Presentation on theme: "PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount."— Presentation transcript:

1 PL/SQL Cursors Session - II

2 Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen

3 %TYPE%TYPE useful when declaring variables that refers to Database Columns Name Varchar(15); Name Emp.Ename%TYPE;

4 %ROWTYPE%ROWTYPE Provides a record Type that represents a Row in a Table. ENAMEDESIGNATION SAL SMITH JONES ADAMS KING CLERK SALESMAN PRSIDENT MANAGER 6000 4566 4567 4500

5 Name Varchar(15); Desig Varchar(15); Salary Number(8,2); Emp_Rec Emp%ROWTYPE;

6 Declare EmRec Emp%Rowtype; Begin Select * INTO EmRec from emp where empno=7369; Dbms_Output.put_line( emrec.ename||emrec.Sal); End;Declare EmRec Emp%Rowtype; Begin Select * INTO EmRec from emp where empno=7369; Dbms_Output.put_line( emrec.ename||emrec.Sal); End;

7 CURSORSCURSORS It is a Temporary Table created in the SGA of the Server. It is a Temporary Table created in the SGA of the Server.

8 Two Types of CURSORS l IMPLICIT l EXPLICIT

9 IMPLICIT Cursor PL/SQL Implicitly declares a cursor for all SQL data Manipulation Statements, Including Queries that return only One Row

10 EXPLICIT Cursor Declares a cursor that process more than one row individually.

11 Using Explicit Cursors l Declare the Cursor l Open theCursor l Fetch theCursor l Close theCursor l Declare the Cursor l Open theCursor l Fetch theCursor l Close theCursor

12 Declaring a Explicit Cursor While Declaring we have to name it and Associate with a Query. Declare CURSOR emp_cursor IS SELECT ename,deptno from emp where sal>2000; Begin

13 Opening a Explicit Cursor Opening the Cursor executes the SELECT statement and Identifies the active set. OPEN emp_curs; Opening the Cursor executes the SELECT statement and Identifies the active set. OPEN emp_curs;

14 Fetching a Explicit Cursor Fetch statement retrieves each row in the active set, one at a time. FETCH emp_cur INTO emp_name; Fetch statement retrieves each row in the active set, one at a time. FETCH emp_cur INTO emp_name;

15 Closing a Explicit Cursor Close Statement disables the Cursor. CLOSE emp_curs; Close Statement disables the Cursor. CLOSE emp_curs;

16 C DEFINE CURSOR ENAME DEPTNO SMITH JONES ADAMS 20 30 40 CURSOR C is Select Ename,deptno from emp ; OPEN CURSOR OPEN C;

17 ENAME DEPTNO FETCHING CURSOR SMITH JONES ADAMS 20 30 40 FETCH C INTO ENAM,DEPTN; CLOSING CURSOR CLOSE C;

18 Declare enam varchar(15); depno number(3); CURSOR C Is Select ename,deptno from emp where job=‘SALESMAN’; Begin OPEN C; LOOP FETCH C into enam,depno ; exit when c%notfound; dbms_output.put_line(enam||depno); END LOOP; Close C; End; Declare enam varchar(15); depno number(3); CURSOR C Is Select ename,deptno from emp where job=‘SALESMAN’; Begin OPEN C; LOOP FETCH C into enam,depno ; exit when c%notfound; dbms_output.put_line(enam||depno); END LOOP; Close C; End;

19 Explicit Cursor Attributes l Every Explicit cursor has four attributes. l Lets you access usefull information about the execution of a multirow query. l Every Explicit cursor has four attributes. l Lets you access usefull information about the execution of a multirow query.

20 l %NOTFOUND l %FOUND l %ROWCOUNT l %ISOPEN l %NOTFOUND l %FOUND l %ROWCOUNT l %ISOPEN

21 %NOTFOUND%NOTFOUND Evaluates to TRUE if the last FETCH failed because no more rows were available. Loop Fetch emp_cur INTO enam,depno; EXIT WHEN emp_cur %NOTFOUND; End loop; Evaluates to TRUE if the last FETCH failed because no more rows were available. Loop Fetch emp_cur INTO enam,depno; EXIT WHEN emp_cur %NOTFOUND; End loop;

22 %FOUND%FOUND l Is the Logical Opposite of %NOTFOUND Loop Fetch emp_cur INTO enam,detpn; IF emp_cur%FOUND Then Dbms_output.Put_line(‘Record Found’); else exit; End if; End loop; l Is the Logical Opposite of %NOTFOUND Loop Fetch emp_cur INTO enam,detpn; IF emp_cur%FOUND Then Dbms_output.Put_line(‘Record Found’); else exit; End if; End loop;

23 %ROWCOUNT%ROWCOUNT Returns the number of rows Fetched

24 n:=&number; open emp_cur; loop Fetch emp_cur INTO enam,deptn; IF emp_cur%ROWCOUNT>n Then Dbms_output.Put_line('more than '||n||' records'); exit; End if; exit when emp_cur%notfound; End loop; Close emp_cur; n:=&number; open emp_cur; loop Fetch emp_cur INTO enam,deptn; IF emp_cur%ROWCOUNT>n Then Dbms_output.Put_line('more than '||n||' records'); exit; End if; exit when emp_cur%notfound; End loop; Close emp_cur;

25 %ISOPEN%ISOPEN Checks Cursor is Opened Loop Fetch emp_cur INTO enam,detpn; IF emp_cur%ISOPEN Then Dbms_output.Put_line(‘Opened’); End if; End loop; Checks Cursor is Opened Loop Fetch emp_cur INTO enam,detpn; IF emp_cur%ISOPEN Then Dbms_output.Put_line(‘Opened’); End if; End loop;

26 Implicit Cursor Attributes l Use cursor attributes to access the SQL% cursors context area. l %NOTFOUND, %FOUND l %ROWCOUNT

27 %NOTFOUND%NOTFOUND Evaluates True if any INSERT, UPDATE or DELETE affected no Rows. Update emp set ename=‘RAM’ where empno=3445; IF SQL%NOTFOUND then Evaluates True if any INSERT, UPDATE or DELETE affected no Rows. Update emp set ename=‘RAM’ where empno=3445; IF SQL%NOTFOUND then

28 Cursor For Loops It Implicitly OPENS a Cursor, FETCH each row returned by the query associated with Cursor and CLOSE the Cursor. Advantages :- Lesser Coding It Implicitly OPENS a Cursor, FETCH each row returned by the query associated with Cursor and CLOSE the Cursor. Advantages :- Lesser Coding

29 declare cursor lst is select * from emp; begin for I in lst loop dbms_output.put_line( I.ename|| I.job); end loop; end; declare cursor lst is select * from emp; begin for I in lst loop dbms_output.put_line( I.ename|| I.job); end loop; end;

30 Dynamic Cursors

31 DECLARE CURSOR c1 (dnum number) IS Select * from emp where deptno = dnum; dep emp.deptno%type; BEGIN Select deptno into dep from emp where ename ='SMITH'; FOR emp_rec IN c1(dep) loop Dbms_output.put_line(emp_rec.ename) ; End loop; END;


Download ppt "PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount."

Similar presentations


Ads by Google