Anda di halaman 1dari 6

A cursor is a temporary work area created in the system memory when a SQL statement is executed.

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.

Implicit Cursors: Application


,hen you execute "#L statements like "&L&T&, $%S&'T, (P"AT& and S&L&)T statements, implicit statements are created to process these statements. .racle provides few attributes called as implicit cursor attributes to check the status of "#L operations. The cursor attributes available are /0.(%", /%.T0.(%", /'.,).(%T, and /$S.P&%. 0or example, ,hen you execute $%S&'T, (P"AT&, or "&L&T& statements the cursor attributes tell us whether any rows are affected and how many have been affected. ,hen a S&L&)T... $%T. statement is executed in a PL SQL -lock, implicit cursor attributes can be used to find out whether any row has been returned by the S&L&)T statement. PL SQL returns an error when no data is selected. The status of the cursor for each of these attributes are defined in the below table. Attributes /0.(%" Return Value The return value is T'(&, if the "#L statements like $%S&'T, "&L&T& and (P"AT& affect at least one row and if S&L&)T 1.$%T. statement return at least one row. Example SQL/0.(%"

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!

$mplicit cursors &xplicit 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-;

,orkin+ with an explicit cursor involves four steps!


"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

Declarin t!e Cursor


"eclarin+ the cursor defines the cursor with a name and the associated S&L&)T statement. 0or example!
CUR"*R 6_6us-omers I" "ELECT /,F nameF a,,ress 'R*9 6us-omers;

"penin t!e Cursor


.penin+ the cursor allocates memory for the cursor and makes it ready for fetchin+ the rows returned by the SQL statement into it. 0or example, we will open above?defined cursor as follows!
*PEN 6_6us-omers;

#etc!in t!e Cursor


0etchin+ the cursor involves accessin+ one row at a time. 0or example we will fetch rows from the above?opened cursor as follows!
'ETC+ 6_6us-omers INT* 6_/,F 6_nameF 6_a,,r;

Closin t!e Cursor


)losin+ the cursor means releasin+ the allocated memory. 0or example, we will close above? opened cursor as follows!
CL*"E 6_6us-omers;

Example:

0ollowin+ is a complete example to illustrate the concepts of explicit cursors!


DECLARE 6_/, 6us-omers./,)-!pe; 6_name 6us-omers.name)-!pe; 6_a,,r 6us-omers.a,,ress)-!pe; CUR"*R 6_6us-omers /s "ELECT /,F nameF a,,ress 'R*9 6us-omers; BEGIN *PEN 6_6us-omers; L**P 'ETC+ 6_6us-omers /n-o 6_/,F 6_nameF 6_a,,r; ,bms_ou-pu-.pu-_ /ne(6_/, 55 0 0 55 6_name 55 0 0 55 6_a,,r); EGIT 4+EN 6_6us-omers)no-1oun,; END L**P; CL*"E 6_6us-omers; END; D

,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,.

PLD"(L pro6e,ure su66ess1u

Anda mungkin juga menyukai