Top Banner
Cursors Cursors In Oracle
15

13436 Cursors

Apr 08, 2018

Download

Documents

Sushil Chauhan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 1/15

CursorsCursorsIn Oracle

Page 2: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 2/15

What is a cursorWhat is a cursor

y For every SQL statement execution

certain area in memory is allocated.

y PL/SQL allow you to name this area.

y This private SQL area is called context

area or cursor.

y A cursor acts as a handle or pointer into

the context area.

Page 3: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 3/15

What is a cursorWhat is a cursor� When you declare a cursor, you get a

pointer variable, which does not point anything.

� When the cursor is opened, memory isallocated and the cursor structure iscreated.

� The cursor variable now points the cursor.

� When the cursor is closed the memory

allocated for the cursor is released.� Cursors allow the programmer to retrieve

data from a table and perform actions onthat data one row at a time.

Page 4: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 4/15

Types of CursorsTypes of Cursors

y Implicit Cursors: For SQL queries

returning single row

y Explicit Cursors: are used in queries

that return multiple rows.

Page 5: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 5/15

Implicit CursorsImplicit Cursors

y Implicit cursors are simple SELECT

statements

y They are written in the BEGIN block  

(executable section) of the PL/SQL.

y Implicit cursors are easy to code

y They retrieve exactly one row.

Page 6: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 6/15

ExampleExample

SELECT ename, sal INTO ena, esa FROM EMP whereEMPNO = 7844;

Page 7: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 7/15

Explicit CursorsExplicit Cursors

y Explicit cursors are used in queries that

return multiple rows.

y The set of rows fetched by a query is

called active set.

y The size of the active set meets the

search criteria in the select statement.

y Explicit cursor is declared in theDECLARE section of PL/SQL program.

Page 8: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 8/15

SyntaxSyntax

� CURSOR <cursor-name> IS <select statement>

� Sample Code:

DECLARE

CURSOR emp_cur IS SELECT ename FROMEMP;

BEGIN

--------- - --------------- - ------

END ;

Page 9: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 9/15

Cont..Cont..

y Processing multiple rows is similar to file

processing.

y For processing a file you need to open it,

process records and then close.

y Similarly user-defined explicit cursor

needs to be opened, before reading the

rows, after which it is closed.

Page 10: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 10/15

Opening a CursorOpening a Cursor

y Syntax: OPEN <cursor-name>;

y Example : OPEN emp_cur;

When a cursor is opened the active set is

determined, the rows satisfying the where

clause in the select statement are added

to the active set. A pointer is established

and points to the first row in the activeset.

Page 11: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 11/15

Fetching From the CursorFetching From the Cursor

y Syntax: FETCH <cursor-name> INTO

<variables>;

y Example: FETCH emp_cur INTO ena;

Page 12: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 12/15

Closing a CursorClosing a Cursor

y CLOSE <cursor-name>;

Page 13: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 13/15

Explicit Cursor AttributesExplicit Cursor Attributes

� Every cursor defined by the user has 4 attributes.

The attributes are:

� %NOTFOUND: It is a Boolean attribute, which evaluates to true, if the last fetch failed. i.e. when

there are no rows left in the cursor to fetch.� %FOUND: Boolean variable, which evaluates to

true if the last fetch, succeeded.

� %ROWCOUNT: It·s a numeric attribute, which returns number of rows fetched by the cursor so

far.� %ISOPEN: A Boolean variable, which evaluates to

true if the cursor is opened otherwise to false.

Page 14: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 14/15

Example: Fetch for Each rowExample: Fetch for Each row

Page 15: 13436 Cursors

8/6/2019 13436 Cursors

http://slidepdf.com/reader/full/13436-cursors 15/15

Fetch using LOOPFetch using LOOP