A cursor contains information on a select statement and the rows of data accessed by it. This temporary work area is used to store the data retrieved from the database, and manipulate this data. A cursor can hold more than one row, but can process only one row at a time. The set of rows the cursor holds is called the active set. There are two types of cursors in PL SQL!
Implicit cursors
These are created by default when "#L statements like, $%S&'T, (P"AT&, and "&L&T& statements are executed. They are also created when a S&L&)T statement that returns *ust one row is executed.
Explicit cursors
They must be created when you are executin+ a S&L&)T statement that returns more than one row. &ven thou+h the cursor stores multiple records, only one record can be processed at a time, which is called as current row. ,hen you fetch a row the current row position moves to next row. -oth implicit and explicit cursors have the same functionality, but they differ in the way they are accessed.
The return value is 0ALS&, if "#L statements like $%S&'T, "&L&T& and (P"AT& do not affect row and if S&L&)T1.$%T. statement do not return a row. /%.T0.(%" The return value is 0ALS&, if "#L statements SQL/%.T0.(%" like $%S&'T, "&L&T& and (P"AT& at least one row and if S&L&)T 1.$%T. statement return at least one row. The return value is T'(&, if a "#L statement like $%S&'T, "&L&T& and (P"AT& do not affect even one row and if S&L&)T 1.$%T. statement does not return a row. /'.,).(%T 'eturn the number of rows affected by the "#L SQL/'.,).(%T operations $%S&'T, "&L&T&, (P"AT&, S&L&)T
0or &xample! )onsider the PL SQL -lock that uses implicit cursor attributes as shown below!
DECLARE var_rows number(5); BEGIN UPDATE emp o!ee "ET sa ar! # sa ar! $ %&&&; I' "(L)N*T'*UND T+EN ,bms_ou-pu-.pu-_ /ne(0None o1 -2e sa ar/es w2ere up,a-e,0); EL"I' "(L)'*UND T+EN var_rows 3# "(L)R*4C*UNT; ,bms_ou-pu-.pu-_ /ne(0"a ar/es 1or 0 55 var_rows 55 0emp o!ees are up,a-e,0); END I'; END;
$n the above PL SQL -lock, the salaries of all the employees in the 2employee3 table are updated. $f none of the employee3s salary are updated we +et a messa+e 4%one of the salaries where updated4. &lse we +et a messa+e like for example, 4Salaries for 5666 employees are updated4 if there are 5666 rows in 2employee3 table.
.racle creates a memory area, known as context area, for processin+ an SQL statement, which contains all information needed for processin+ the statement, for example, number of rows processed, etc. A cursor is a pointer to this context area. PL SQL controls the context area throu+h a cursor. A cursor holds the rows 7one or more8 returned by a SQL statement. The set of rows the cursor holds is referred to as the active set. 9ou can name a cursor so that it could be referred to in a pro+ram to fetch and process the rows returned by the SQL statement, one at a time. There are two types of cursors!
Implicit Cursors
$mplicit cursors are automatically created by .racle whenever an SQL statement is executed, when there is no explicit cursor for the statement. Pro+rammers cannot control the implicit cursors and the information in it. ,henever a "#L statement 7$%S&'T, (P"AT& and "&L&T&8 is issued, an implicit cursor is associated with this statement. 0or $%S&'T operations, the cursor holds the data that needs to be inserted. 0or (P"AT& and "&L&T& operations, the cursor identifies the rows that would be affected. $n PL SQL, you can refer to the most recent implicit cursor as the SQL cursor, which always has the attributes like /0.(%", /$S.P&%, /%.T0.(%", and /'.,).(%T. The SQL cursor has additional attributes, /-(L:;'.,).(%T and /-(L:;&<)&PT$.%S, desi+ned for use with the 0.'ALL statement. The followin+ table provides the description of the most used attributes! Attribute /0.(%" /%.T0.(%" /$S.P&% /'.,).(%T Description 'eturns T'(& if an $%S&'T, (P"AT&, or "&L&T& statement affected one or more rows or a S&L&)T $%T. statement returned one or more rows. .therwise, it returns 0ALS&. The lo+ical opposite of /0.(%". $t returns T'(& if an $%S&'T, (P"AT&, or "&L&T& statement affected no rows, or a S&L&)T $%T. statement returned no rows. .therwise, it returns 0ALS&. Always returns 0ALS& for implicit cursors, because .racle closes the SQL cursor automatically after executin+ its associated SQL statement. 'eturns the number of rows affected by an $%S&'T, (P"AT&, or "&L&T& statement, or returned by a S&L&)T $%T. statement.
Any SQL cursor attribute will be accessed as sql%attribute_name as shown below in the example.
Example:
,e will be usin+ the )(ST.#&'S table we had created and used in the previous chapters.
"e e6- 7 1rom 6us-omers; $8888$8888888888$88888$88888888888$8888888888$ 5 ID 5 NA9E 5 AGE 5 ADDRE"" 5 "ALAR: 5 $8888$8888888888$88888$88888888888$8888888888$ 5 % 5 Rames2 5 ;< 5 A2me,aba, 5 <&&&.&& 5 5 < 5 =2/ an 5 <5 5 De 2/ 5 %5&&.&& 5 5 ; 5 >aus2/> 5 <; 5 =o-a 5 <&&&.&& 5 5 ? 5 C2a/-a / 5 <5 5 9umba/ 5 @5&&.&& 5 5 5 5 +ar,/> 5 <A 5 B2opa 5 B5&&.&& 5 5 @ 5 =oma 5 << 5 9P 5 ?5&&.&& 5 $8888$8888888888$88888$88888888888$8888888888$
The followin+ pro+ram would update the table and increase salary of each customer by =66 and use the SQL/'.,).(%T attribute to determine the number of rows affected!
DECLARE -o-a _rows number(<); BEGIN UPDATE 6us-omers "ET sa ar! # sa ar! $ 5&&; I' sC )no-1oun, T+EN ,bms_ou-pu-.pu-_ /ne(0no 6us-omers se e6-e,0); EL"I' sC )1oun, T+EN -o-a _rows 3# sC )row6oun-; ,bms_ou-pu-.pu-_ /ne( -o-a _rows 55 0 6us-omers se e6-e, 0); END I'; END; D
,hen the above code is executed at SQL prompt, it produces the followin+ result!
@ 6us-omers se e6-e, PLD"(L pro6e,ure su66ess1u ! 6omp e-e,.
$f you check the records in customers table, you will find that the rows have been updated!
"e e6- 7 1rom 6us-omers; $8888$8888888888$88888$88888888888$8888888888$ 5 ID 5 NA9E 5 AGE 5 ADDRE"" 5 "ALAR: 5 $8888$8888888888$88888$88888888888$8888888888$ 5 % 5 Rames2 5 ;< 5 A2me,aba, 5 <5&&.&& 5 5 < 5 =2/ an 5 <5 5 De 2/ 5 <&&&.&& 5 5 ; 5 >aus2/> 5 <; 5 =o-a 5 <5&&.&& 5 5 ? 5 C2a/-a / 5 <5 5 9umba/ 5 A&&&.&& 5 5 5 5 +ar,/> 5 <A 5 B2opa 5 E&&&.&& 5 5 @ 5 =oma 5 << 5 9P 5 5&&&.&& 5 $8888$8888888888$88888$88888888888$8888888888$
Explicit Cursors
&xplicit cursors are pro+rammer defined cursors for +ainin+ more control over the context area. An explicit cursor should be defined in the declaration section of the PL SQL -lock. $t is created on a S&L&)T Statement which returns more than one row. The syntax for creatin+ an explicit cursor is !
CUR"*R 6ursor_name I" se e6-_s-a-emen-;
"eclarin+ the cursor for initiali>in+ in the memory .penin+ the cursor for allocatin+ memory 0etchin+ the cursor for retrievin+ data )losin+ the cursor to release allocated memory
Example:
,hen the above code is executed at SQL prompt, it produces the followin+ result!
% < ; ? 5 @ Rames2 A2me,aba, =2/ an De 2/ >aus2/> =o-a C2a/-a / 9umba/ +ar,/> B2opa =oma 9P ! 6omp e-e,.