You are on page 1of 13

Alchemy Solutions

SQL STATEMENT PROCESSING

Alchemy Solutions

SQL STATEMENT PROCESSING

Objectives At the end of this unit, you will be able to Process a query with an explicit cursor Process the same query with different input values.

Topics What is a cursor Explicit cursors Cursor attributes Cursor F ! " P

Parameteri#ed Cursors $E"EC% F ! &P'A%E Cursors

Alchemy Solutions

SQL STATEMENT PROCESSING

h!t is ! Cu"so" For the database to process a $(" statement it allocates memory. %his memory is )nown as the context area. %he context area has information about the number of rows processed by the statement, a pointer to the parsed representation of the statement, and in case of query, the set of rows returned by the query. A Cursor is a pointer to the context area. %hrou*h the Cursor, a P"+$(" pro*ram can control the context area and what happens to it as the statement is processed. .A cursor lets the user name the context area and access its stored information. P"+$(" implicitly declares a cursor for all $(" data manipulation statements, includin* queries that include only one row. For queries that return more than one row, one can explicitly declare a cursor to process rows individually. %he set of rows returned by a multi,row query is called the active set. %here are two types of cursors. -mplicit Explicit Every $(" statement .li)e -/$E!%, &P'A%E, $E"EC% etc01 executed will be associated with individual cursor. %he cursor controls all the processin* of $(" statements. E#plicit Cu"so"s &se explicit cursor if a query returns more than one row. Explicit cursor will help to Process beyond the first row returned by the query 2eep trac) of which row is currently bein* processed.

$teps to 'eclare and Execute an Explicit Cursor 'eclare the cursor pen the cursor Fetch data from the cursor, do intermediate processin*, if required, record by record. Close the cursor.

Alchemy Solutions

SQL STATEMENT PROCESSING

St!tement Pu"pose Synt!#

$ECLARE 'eclarin* a cursor defines the names of the cursor and associates with it a $E"EC% statement. $ECLARE C%RSOR cu"so"&n!me IS SELECT&st!tement' OPEN penin* a cursor identifies a cursor that has been previously been declared and the followin* thin*s happen %he values of bind variables are examined. 3ased on the values of bind variables the active set is determined %he active set pointer is set to the first row

St!tement Pu"pose

Synt!#

OPEN cursor4name5

St!tement Pu"pose Synt!# St!tement Pu"pose

(ETC) returns the current row from the active set and increments the active set pointer to point to the next row. (ETC) cu"so"&n!me INTO v!"i!ble*+ v!"i!ble,-. CLOSE %o close the cursor when all the rows of the active set have been retrieved. %his is an indication to the P"+$(" en*ine to release the associated resources. CLOSE cu"so"&n!me'

Synt!# he"e

cu"so"&n!me SELECT&st!tement /!"i!ble Note

is a P"+$(" identifier. is a $E"EC% statement without the -/% clause. is an output variable to store the results.

'eclare the cursor to associate a name with the $E"EC% statement pen the cursor after specifyin* values for all input variables Fetch data from the cursor and store it in output varaibles. Close the cursor after completin* the processin* of the $E"EC% statement.

Alchemy Solutions

SQL STATEMENT PROCESSING

Cu"so" Att"ibutes Fetch returns the successive rows in a loop from the active set, until the entire set is returned. Cursor attributes can be inspected to determine when to enter and exit the loop.

Explicit Cursor attributes Cursor attribute 0ISOPEN 0 (O%N$ 'escription 3oolean attribute that evaluates to %!&E if the cursor is open. 3oolean attribute that evaluates to %!&E if previous FE%C6 returns a row and FA"$E if it does not return a row. 3ehaves opposite to 7F &/'. /umeric attribute that evaluates to the total number of rows returned so far.

0NOT(O%N$ 0RO CO%NT

/ote8

'o not reference cursor attributes directly within a $(" statement. 'o not include -/% clause in 'EC"A!A%- / section. 'eclare all the variables before the cursor declaration, if the $E"EC% statement references those variables. %he number of variables and the data type should be same within the -/% clause of the FE%C6 statement as output columns in the $E"EC% statement. 'o not attempt to fetch data from a cursor once it has been closed. !etrieve all rows from emp table, one by one. 'EC"A!E 9empno emp.empno7%:PE5 9ename emp.ename7%:PE5 9;ob emp.;ob7%:PE5 C&!$ ! emp4cursor -$ $E"EC% empno, ename, <ob F! = emp W6E!E ;ob>?C"E!2?5 " PE/ emp4cursor5 P
5

E#!mple PL1SQL Co2e

3E@-/

Alchemy Solutions

SQL STATEMENT PROCESSING


FE%C6 emp4cursor -/% vempno,vename,v;ob5 EA-% W6E/ emp4cursor7/ %F &/'5 -/$E!% -/% temp 9A"&E$.vempno,vename,v;ob15 E/' " P5 C" $E emp4cursor5 C ==-%5

E/'5 +

0ISOPEN determinin* E#!mple

%his is used to fetch row only when the cursor is opened by whether the cursor is open. %o retrieve the current line item, if the cursor is unavailable, open the cursor. 9empno emp.empno7%:PE5 9ename emp.ename7%:PE5 9< 3 emp.;ob7%:PE5 C&!$ ! emp4cursor -$ $E"EC% empno,ename,;ob F! = emp W6E!E ;ob>?C"E!2?5 -F emp4cursor7-$ PE/ %6E/ FE%C6 emp4cursor -/% vempno,vename,v;ob5 E"$E PE/ emp4cursor5 E/' -F5 " P EA-% W6E/ emp4cursor7/ %F &/'5 FE%C6 emp4cursor -/% vempno,vename,v;ob5 -/$E!% -/% temp 9A"&E$.vempno,vename,v;ob15 E/' " P5 C" $E emp4cursor5 C ==-%5

PL1SQL Co2e 'EC"A!E

3E@-/

E/'5 +

0NOT(O%N$ %his is used to chec) whether there are no more rows to process.

Alchemy Solutions

SQL STATEMENT PROCESSING

E#!mple

!etrieve the line items for an order one by one until there are no more line items left. $tore the cumulative total for each product in a separate table.

PL1SQL Co2e ACCEP% p4ordid P! =P% BPlease enter the order -'? 'EC"A!E 94ordid item.ordid7%:PE8>Cp4ordid5 94prodid item.prodid7%:PE5 94itemtot /&=3E!.DD,E18>F5 C&!$ ! item4cursor -$ $E"EC% prodid,actualpriceGqty F! = item W6E!E ordid>v4ordid5 3E@-/ PE/ item4cursor5 " P FE%C6 item4cursor -/% v4prodid,v4itemtot5 EA-% W6E/ item4cursor7/ %F &/'5 94ordtot8>v4ordtot H v4itemtot5 -/$E!% -/% tempD.prodid,cumulative4total1 9A"&E$.v4prodid,v4ordtot15 E/' " P5 C" $E item4cursor5 C ==-% 5 E/'5 + 0(O%N$ E#!mple %his is used to chec) whether a row exists and then to perform the required operation. !etrieve the line items for an order one by one until there are no more line items left. $tore the cumulative total for each product in a separate table.

PL1SQL Co2e ACCEP% p4ordid P! =P% BPlease enter the order -'8? 'EC"A!E 94ordid item.ordid7%:PE8>Cp4ordid5 94prodid item.prodid7%:PE5 94itemtot /&=3E!.DD,E18>F5 C&!$ ! item4cursor -$ $E"EC% prodid,actualpriceGqty F! = item W6E!E ordid>v4ordid5 3E@-/ PE/ item4cursor5 FE%C6 item4cursor -/% v4prodid,v4itemtot5 W6-"E item4cursor7F &/' " P 94ordtot8>v4ordtot H v4itemtot5 -/$E!% -/% tempD .prodid,cumulative4total1 9A"&E$.v4prodid,v4ordtot15

Alchemy Solutions

SQL STATEMENT PROCESSING


FE%C6 item4cursor -/% v4prodid,v4itemtot5 E/' " P5 C" $E item4cursor5 C ==-% 5 E/'5 +

0RO CO%NT %his is used to find out the number of rows that have been fetched thus far within the procedure. E#!mple !etrieve the first five items for an order one by one. $tore the cumulative total for each product in a separate table.

PL1SQL Co2e ACCEP% p4ord4id P! =P% BPlease enter the order -'8? 'EC"A!E 94ordid item.ordid7%:PE8>Cp4ord4id5 94prodid item.prodid7%:PE5 94itemtot /&=3E!.DD,E15 94ordtot /&=3E!.DD,E18>F5 C&!$ ! item4cursor -$ $E"EC% prodid,actualpriceGqty F! = item W6E!E ordid>v4ordid5 3E@-/ PE/ item4cursor5 " P FE%C6 item4cursor -/% v4prodid,v4itemtot5 EA-% W6E/ item4cursor7! WC &/%IJ. ! item4cursor7/ %F &/'5 94ordtot8>v4ordtot H v4itemtot5 -/$E!% -/% tempD.prodid,cumulative4total1 9A"&E$.v4prodid,v4ordtot15 E/' " P5 C" $E item4cursor5 C ==-% 5 E/'5 +

Cu"so" (OR LOOP As shortcut, fetch the rows in a cursor F ! loop, which iterates once for each row returned by the query. Synt!# F ! record4name -/ cursor4name " $tatement D5 $tatement E5 P

Alchemy Solutions
0.. 0. E/' " P5 he"e !ecord4name cursor4name

SQL STATEMENT PROCESSING

is the name of an implicitly declared record .record4name cursor4name7! W%:PE51 is the name of the previously declared cursor.

%he cursor itself determines when the cursor F ! loop is exited as well as all other cursor operations. Cursor F ! loop An implicit PE/ is executed when the loop is initiated. An implicit FE%C6 is executed for each iteration of the loop. An implicit C" $E is executed when the loop is exited. /ote 'o not declare the record that controls the loop. 'o not use a cursor F ! loop when the cursor operations have to be handled manually. !etrieve all the items for an order, one by one, store the cumulative total for each product in a separate table.

E#!mple

PL1SQL co2e ACCEP% p4ord4id P! =P%?Please enter the order id8? 'EC"A!E 94ordid item.ordid7%:PE8> Cp4ordid5 94ordtot /&=3E!.DD,E1 8>F5 C&!$ ! item4cursor -$ $E"EC% prodid,actualprice,qty F! = item W6E!E ordid >v4ordid5 3E@-/ F ! item4record -/ item4cursor " P 94ordtot8>v4ordtot H . item4record.actualpriceGitem4record.qt y15 -/$E!% -/% tempD.prodid,cumulative4total1 9A"&E$.item4record.prodid,v4ordtot15 E/' " P5 C ==-% 5 E/'5 P!"!mete"i3e2 Cu"so"s Cursors can ta)e parameters. A cursor parameter can appear in a query wherever a constant can appear.

Alchemy Solutions
Synt!#

SQL STATEMENT PROCESSING

C&!$ ! cursor4nameK.varaibleD datatypeD, varaibleE datatypeE,001L -$ $E"EC% 4statement Cursor4name 9ariable 'atatype $E"EC%4statement is is is is a P"+$(" identifier the name of a parameter. the datatype of the parameter. a $E"EC% statement without the -/% clause.

he"e

Note

%he scope of the cursor parameters is local to the cursor i.e they can be referenced only within the query used in the cursor declaration. %he values of cursor parameters are used by the associated query when the cursor is opened. !etrieve all employees whose number is *reater than the specified number, one by one.

E#!mple

PL1SQL Co2e ACCEP% p4eno P! =P% B Enter the employee number8? 'EC"A!E 9empno emp.empno7%:PE5 9ename emp.ename7%:PE5 9;ob emp.;ob7%:PE5 C&!$ ! cD.eno -/ /&=3E!1 -$ $E"EC% empno,ename,;ob F! = emp W6E!E empnoIeno5 3E@-/ PE/ cD.Cp4eno15 " P FE%C6 cD -/% vempno,vename,v;ob5 EA-% W6E/ cD7/ %F &/'5 -/$E!% -/% temp 9A"&E$.vempno,vename,v;ob15 E/' " P5 C" $E cD5 C ==-%5 E/'5 +

SELECT (OR %P$ATE Cu"so"s A select will not loc) any rows bein* accessed, this allows other sessions connected to database to chan*e the data bein* selected. At time of openin* of the cursor, racle ta)es the snapshot of the table and any chan*es committed before this point are reflected in this active set. Any chan*es made after this point even if they are committed, are not reflected unless the cursor is reopened, which will evaluate the active set a*ain.

10

Alchemy Solutions

SQL STATEMENT PROCESSING

-f F ! &P'A%E clause is present in the cursor statement, exclusive row loc)s are ta)en on rows in the active set before cursor open operation returns. these loc)s prevent other sessions from chan*in* the rows in the active set until the transaction is committed. Note &P'A%E statement updates only the column listed in the F ! &P'A%E clause of the cursor declaration. -f no columns are listed, any column can be updated.

C ==-% cannot be used within a fetch loop, since F ! &P'A%E clause acquires loc)s and C ==-% will release any loc)s held by the session released. -f the cursor is declared with F ! &P'A%E clause, the W6E!E C&!!E/% F clause can be used in an update or delete statement. E#!mple !etrieve the items for specific order one by one. -f the cumulative total for the order reaches JFFF then delete the remainin* items.

PL1SQL Co2e ACCEP% p4ord4id P! =P%?Please enter the order id8? 'EC"A!E 94ordid item.ordid7%:PE8>Cp4ordid5 94ortot /&=3E!.DD,E18>F5 C&!$ ! item4cursor -$ $E"EC% prodid,actualprice,qty F! = item W6E!E ordid >v4ordid F ! &P'A%E5 3E@-/ F ! item4record -/ item4cursor " P -F v4ordtot H .item4record.actualpriceGitem4record.qty1I JFFF %6E/ 'E"E%E F! = item W6E!E C&!!E/% F item4cursor5 E"$E 94ordtot8>v4ordtot H .item4record.actualpriceGitem4record.qty 15 E/' -F5 E/' " P5 C ==-% 5 E/'5 +

11

Alchemy Solutions

SQL STATEMENT PROCESSING

S%MMAR4 56 racle creates a private $(" areas to excute $(" statement and store information called cursors. %here are two types of cursors 8, -mplicit Cursor M %hey are declared by P"+$(" implicitly for all '=" statement and for sin*le row queries. Explicit Cursor M %hey are declared explicitly alon* with other identifiers to be used in a bloc) and manipulated throu*h specific statements with the bloc)s executable actions. Cursor manipulation is done throu*h PE/, FE%C6 and C" $E statements Explicit cursor has four attributes M 7/ %F &/' 7F &/' 7! WC &/% 7-$ PE/ !ecords can be defined based upon the selected list of columns in an explicit cursors F ! &P'A%E C"A&$E can be used within cursor query can be used with $(" commands with the name of the cursor Parameters allow values to be passed to a cursor when it is opened and used within the query when it excutes Create a P"+$(" bloc) that determines the top employees with respect to salaries. Accept a number n as user input with $(" G Plus substitution parameter. -n a loop, *et ename and sal for top n people with respect to sal in the E=P table. $tore the names and salaries in the % P4$A" table. Assume that no two employees have the same salary. !edo the above exercise to incorporate P"+$(" tables. 'eclare two P"+$(" tables, /A=E4%A3"E and $A"A!: 4%A3"E, to temporarily store the names and salaries. As each name and salary is retrieved within the loop, store them in P"+$(" tables.

L!b E#e"cises

12

Alchemy Solutions

SQL STATEMENT PROCESSING

utside the loop, transfer the names and salaries from the P"+$(" tables into the % P4$A" table.

13

You might also like