pl/sql cursors session - ii. attributes attributes %type %rowtype % found % notfound % rowcount %...
TRANSCRIPT
AttributesAttributes AttributesAttributes%TYPE
%ROWTYPE
% Found
% NotFound
% RowCount
% IsOPen
%TYPE
%ROWTYPE
% Found
% NotFound
% RowCount
% IsOPen
%TYPE%TYPE%TYPE%TYPE useful when declaring variables
that refers to Database Columns
Name Varchar(15);
Name Emp.Ename%TYPE;
%ROWTYPE%ROWTYPE%ROWTYPE%ROWTYPE
Provides a record Type that represents a Row in a Table.
ENAME DESIGNATION SAL
SMITHJONES ADAMSKING
CLERK SALESMANPRSIDENT MANAGER
6000456645674500
DeclareDeclare
EmRecEmRec Emp%Rowtype; Emp%Rowtype;
BeginBegin
Select * INTO Select * INTO EmRecEmRec from emp where from emp where empno=7369;empno=7369;
Dbms_Output.put_line( Dbms_Output.put_line( emrecemrec.ename||.ename||emrecemrec.Sal.Sal););
End;End;
DeclareDeclare
EmRecEmRec Emp%Rowtype; Emp%Rowtype;
BeginBegin
Select * INTO Select * INTO EmRecEmRec from emp where from emp where empno=7369;empno=7369;
Dbms_Output.put_line( Dbms_Output.put_line( emrecemrec.ename||.ename||emrecemrec.Sal.Sal););
End;End;
CURSORSCURSORSCURSORSCURSORS
It is a Temporary Table created in the SGA of the Server.
It is a Temporary Table created in the SGA of the Server.
IMPLICIT CursorIMPLICIT CursorIMPLICIT CursorIMPLICIT Cursor
PL/SQL Implicitly declares a cursor for all SQL data Manipulation Statements, Including Queries that return only One Row
PL/SQL Implicitly declares a cursor for all SQL data Manipulation Statements, Including Queries that return only One Row
EXPLICIT CursorEXPLICIT CursorEXPLICIT CursorEXPLICIT Cursor
Declares a cursor that process more than one row individually.
Declares a cursor that process more than one row individually.
Using Explicit CursorsUsing Explicit CursorsUsing Explicit CursorsUsing Explicit Cursors
Declare the CursorOpen the CursorFetch the CursorClose the Cursor
Declare the CursorOpen the CursorFetch the CursorClose the Cursor
Declaring a Explicit Declaring a Explicit CursorCursor
Declaring a Explicit Declaring a Explicit CursorCursor
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
Opening a Explicit Opening a Explicit CursorCursor
Opening a Explicit Opening a Explicit CursorCursor
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;
Fetching a Explicit Fetching a Explicit CursorCursor
Fetching a Explicit Fetching a Explicit CursorCursor
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;
Closing a Explicit Closing a Explicit CursorCursor
Closing a Explicit Closing a Explicit CursorCursor
Close Statement disables the Cursor.
CLOSE emp_curs;
Close Statement disables the Cursor.
CLOSE emp_curs;
C
DEFINE CURSOR
ENAME DEPTNO
SMITHJONESADAMS
20
30
40
CURSOR C is Select Ename,deptno from emp ;
OPEN CURSOR
OPEN C;
ENAME DEPTNO
FETCHING CURSOR
SMITHJONESADAMS
20
30
40
FETCH C INTO ENAM,DEPTN;
CLOSING CURSOR
CLOSE C;
Declareenam varchar(15);depno number(3);CURSOR C Is Select ename,deptno from emp where
job=‘SALESMAN’;BeginOPEN C;LOOPFETCH C into enam,depno ;exit when c%notfound;dbms_output.put_line(enam||depno);END LOOP;Close C;End;
Declareenam varchar(15);depno number(3);CURSOR C Is Select ename,deptno from emp where
job=‘SALESMAN’;BeginOPEN C;LOOPFETCH C into enam,depno ;exit when c%notfound;dbms_output.put_line(enam||depno);END LOOP;Close C;End;
Explicit Cursor AttributesExplicit Cursor Attributes
Every Explicit cursor has four attributes.
Lets you access usefull information about the execution of a multirow query.
Every Explicit cursor has four attributes.
Lets you access usefull information about the execution of a multirow query.
%NOTFOUND%NOTFOUND%NOTFOUND%NOTFOUNDEvaluates 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;
%FOUND%FOUND%FOUND%FOUND Is the Logical Opposite of
%NOTFOUNDLoop Fetch emp_cur INTO enam,detpn; IF emp_cur%FOUND Then Dbms_output.Put_line(‘Record Found’); else exit; End if;End loop;
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;
%ROWCOUNT%ROWCOUNT%ROWCOUNT%ROWCOUNT
Returns the number of rows FetchedReturns the number of rows Fetched
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;
%ISOPEN%ISOPEN%ISOPEN%ISOPENChecks 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;
Implicit Cursor Implicit Cursor AttributesAttributes
Implicit Cursor Implicit Cursor AttributesAttributes
Use cursor attributes to access the SQL% cursors context area.
%NOTFOUND , %FOUND%ROWCOUNT
%NOTFOUND%NOTFOUND%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
Cursor For LoopsCursor For LoopsCursor For LoopsCursor 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
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;
DECLAREDECLARE CURSOR c1 CURSOR c1 (dnum number) IS(dnum number) IS Select * from emp where deptno = dnum;Select * from emp where deptno = dnum;
dep emp.deptno%type; dep emp.deptno%type; BEGIN BEGIN Select deptno into dep from emp Select deptno into dep from emp where ename = where ename = 'SMITH';'SMITH'; FOR emp_rec IN c1(dep) FOR emp_rec IN c1(dep) loop loopDbms_output.put_line(emp_rec.ename)Dbms_output.put_line(emp_rec.ename);; End loop;End loop;END;END;
DECLAREDECLARE CURSOR c1 CURSOR c1 (dnum number) IS(dnum number) IS Select * from emp where deptno = dnum;Select * from emp where deptno = dnum;
dep emp.deptno%type; dep emp.deptno%type; BEGIN BEGIN Select deptno into dep from emp Select deptno into dep from emp where ename = where ename = 'SMITH';'SMITH'; FOR emp_rec IN c1(dep) FOR emp_rec IN c1(dep) loop loopDbms_output.put_line(emp_rec.ename)Dbms_output.put_line(emp_rec.ename);; End loop;End loop;END;END;