Alchemy Solutions

SQL STATEMENT PROCESSING

1

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

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

-. CLOSE cu"so"&n!me' Synt!# he"e cu"so"&n!me SELECT&st!tement /!"i!ble Note • • • • is a P"+$(" identifier. is an output variable to store the results. 4 . '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. is a $E"EC% statement without the -/% clause. %his is an indication to the P"+$(" en*ine to release the associated resources. CLOSE %o close the cursor when all the rows of the active set have been retrieved. (ETC) cu"so"&n!me INTO v!"i!ble*+ v!"i!ble. 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. $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.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.

3oolean attribute that evaluates to %!&E if previous FE%C6 returns a row and FA"$E if it does not return a row.empno7%:PE5 9ename emp. /umeric attribute that evaluates to the total number of rows returned so far. %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. one by one. until the entire set is returned.Alchemy Solutions SQL STATEMENT PROCESSING Cu"so" Att"ibutes Fetch returns the successive rows in a loop from the active set.. Cursor attributes can be inspected to determine when to enter and exit the loop. <ob F! = emp W6E!E . 3ehaves opposite to 7F &/'./ section.ob>?C"E!2?5 " PE/ emp4cursor5 P 5 E#!mple PL1SQL Co2e 3E@-/ . if the $E"EC% statement references those variables. 'eclare all the variables before the cursor declaration. Explicit Cursor attributes Cursor attribute 0ISOPEN 0 (O%N$ 'escription 3oolean attribute that evaluates to %!&E if the cursor is open.ob emp. 'o not include -/% clause in 'EC"A!A%. 'EC"A!E 9empno emp.ename7%:PE5 9. 'o not attempt to fetch data from a cursor once it has been closed. 0NOT(O%N$ 0RO CO%NT /ote8 • • • • • 'o not reference cursor attributes directly within a $(" statement. ename.ob7%:PE5 C&!$ ! emp4cursor -$ $E"EC% empno. !etrieve all rows from emp table.

ename.ob7%:PE5 C&!$ ! emp4cursor -$ $E"EC% empno.vename.vempno.ob5 EA-% W6E/ emp4cursor7/ %F &/'5 -/$E!% -/% temp 9A"&E$. open the cursor.v.empno7%:PE5 9ename emp.vename.. 6 .ob F! = emp W6E!E .vename.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.vempno.ename7%:PE5 9< 3 emp.vename.vename.v. 9empno emp.ob>?C"E!2?5 -F emp4cursor7-$ PE/ %6E/ FE%C6 emp4cursor -/% vempno.ob5 E"$E PE/ emp4cursor5 E/' -F5 " P EA-% W6E/ emp4cursor7/ %F &/'5 FE%C6 emp4cursor -/% vempno. %o retrieve the current line item..v.Alchemy Solutions SQL STATEMENT PROCESSING FE%C6 emp4cursor -/% vempno.ob5 -/$E!% -/% temp 9A"&E$. if the cursor is unavailable.v.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.

E18>F5 C&!$ ! item4cursor -$ $E"EC% prodid.v4prodid.ordid7%:PE8>Cp4ordid5 94prodid item.prodid7%:PE5 94itemtot /&=3E!. $tore the cumulative total for each product in a separate table.cumulative4total1 9A"&E$.v4itemtot5 W6-"E item4cursor7F &/' " P 94ordtot8>v4ordtot H v4itemtot5 -/$E!% -/% tempD .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.DD.E18>F5 C&!$ ! item4cursor -$ $E"EC% prodid. $tore the cumulative total for each product in a separate table.cumulative4total1 9A"&E$. PL1SQL Co2e ACCEP% p4ordid P! =P% BPlease enter the order -'? 'EC"A!E 94ordid item.actualpriceGqty F! = item W6E!E ordid>v4ordid5 3E@-/ PE/ item4cursor5 FE%C6 item4cursor -/% v4prodid.prodid7%:PE5 94itemtot /&=3E!.ordid7%:PE8>Cp4ordid5 94prodid item.v4itemtot5 EA-% W6E/ item4cursor7/ %F &/'5 94ordtot8>v4ordtot H v4itemtot5 -/$E!% -/% tempD. !etrieve the line items for an order one by one until there are no more line items left.prodid.actualpriceGqty F! = item W6E!E ordid>v4ordid5 3E@-/ PE/ item4cursor5 " P FE%C6 item4cursor -/% v4prodid. PL1SQL Co2e ACCEP% p4ordid P! =P% BPlease enter the order -'8? 'EC"A!E 94ordid item.prodid.v4prodid.DD.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.v4ordtot15 7 .

v4prodid. Synt!# F ! record4name -/ cursor4name " $tatement D5 $tatement E5 P 8 .E18>F5 C&!$ ! item4cursor -$ $E"EC% prodid.cumulative4total1 9A"&E$.v4ordtot15 E/' " P5 C" $E item4cursor5 C ==-% 5 E/'5 + Cu"so" (OR LOOP As shortcut.ordid7%:PE8>Cp4ord4id5 94prodid item.E15 94ordtot /&=3E!.prodid7%:PE5 94itemtot /&=3E!.DD. $tore the cumulative total for each product in a separate table.actualpriceGqty F! = item W6E!E ordid>v4ordid5 3E@-/ PE/ item4cursor5 " P FE%C6 item4cursor -/% v4prodid. ! item4cursor7/ %F &/'5 94ordtot8>v4ordtot H v4itemtot5 -/$E!% -/% tempD.DD. fetch the rows in a cursor F ! loop.Alchemy Solutions SQL STATEMENT PROCESSING FE%C6 item4cursor -/% v4prodid. which iterates once for each row returned by the query. PL1SQL Co2e ACCEP% p4ord4id P! =P% BPlease enter the order -'8? 'EC"A!E 94ordid item.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.prodid. E#!mple !etrieve the first five items for an order one by one.v4itemtot5 EA-% W6E/ item4cursor7! WC &/%IJ.

A cursor parameter can appear in a query wherever a constant can appear.E1 8>F5 C&!$ ! item4cursor -$ $E"EC% prodid.DD. • An implicit C" $E is executed when the loop is exited.record4name cursor4name7! W%:PE51 is the name of the previously declared cursor. E#!mple PL1SQL co2e ACCEP% p4ord4id P! =P%?Please enter the order id8? 'EC"A!E 94ordid item. one by one. item4record. 'o not use a cursor F ! loop when the cursor operations have to be handled manually. 9 .v4ordtot15 E/' " P5 C ==-% 5 E/'5 P!"!mete"i3e2 Cu"so"s Cursors can ta)e parameters. store the cumulative total for each product in a separate table. !etrieve all the items for an order.actualpriceGitem4record. • An implicit FE%C6 is executed for each iteration of the loop.prodid.Alchemy Solutions 0. 0.actualprice.item4record.qt y15 -/$E!% -/% tempD. Cursor F ! loop • An implicit PE/ is executed when the loop is initiated. /ote • • 'o not declare the record that controls the loop.qty F! = item W6E!E ordid >v4ordid5 3E@-/ F ! item4record -/ item4cursor " P 94ordtot8>v4ordtot H .cumulative4total1 9A"&E$.prodid.ordid7%:PE8> Cp4ordid5 94ordtot /&=3E!. %he cursor itself determines when the cursor F ! loop is exited as well as all other cursor operations. E/' " P5 he"e !ecord4name cursor4name SQL STATEMENT PROCESSING is the name of an implicitly declared record ..

ob F! = emp W6E!E empnoIeno5 3E@-/ PE/ cD.ob5 EA-% W6E/ cD7/ %F &/'5 -/$E!% -/% temp 9A"&E$..vename. E#!mple PL1SQL Co2e ACCEP% p4eno P! =P% B Enter the employee number8? 'EC"A!E 9empno emp. racle ta)es the snapshot of the table and any chan*es committed before this point are reflected in this active set. one by one. are not reflected unless the cursor is reopened. the datatype of the parameter.vename. At time of openin* of the cursor. a $E"EC% statement without the -/% clause.ename.v. 10 .vempno. Any chan*es made after this point even if they are committed.ename7%:PE5 9. !etrieve all employees whose number is *reater than the specified number.empno7%:PE5 9ename emp.e they can be referenced only within the query used in the cursor declaration.001L -$ $E"EC% 4statement Cursor4name 9ariable 'atatype $E"EC%4statement is is is is a P"+$(" identifier the name of a parameter.v.varaibleD datatypeD..Cp4eno15 " P FE%C6 cD -/% vempno. this allows other sessions connected to database to chan*e the data bein* selected.ob emp.Alchemy Solutions Synt!# SQL STATEMENT PROCESSING C&!$ ! cursor4nameK. he"e Note %he scope of the cursor parameters is local to the cursor i.ob7%:PE5 C&!$ ! cD. varaibleE datatypeE.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. %he values of cursor parameters are used by the associated query when the cursor is opened.eno -/ /&=3E!1 -$ $E"EC% empno. which will evaluate the active set a*ain.

Note &P'A%E statement updates only the column listed in the F ! &P'A%E clause of the cursor declaration.ordid7%:PE8>Cp4ordid5 94ortot /&=3E!. exclusive row loc)s are ta)en on rows in the active set before cursor open operation returns.qty 15 E/' -F5 E/' " P5 C ==-% 5 E/'5 + 11 .qty F! = item W6E!E ordid >v4ordid F ! &P'A%E5 3E@-/ F ! item4record -/ item4cursor " P -F v4ordtot H . -f the cursor is declared with F ! &P'A%E clause.actualprice. since F ! &P'A%E clause acquires loc)s and C ==-% will release any loc)s held by the session released.E18>F5 C&!$ ! item4cursor -$ $E"EC% prodid. the W6E!E C&!!E/% F clause can be used in an update or delete statement.qty1I JFFF %6E/ 'E"E%E F! = item W6E!E C&!!E/% F item4cursor5 E"$E 94ordtot8>v4ordtot H .item4record.Alchemy Solutions SQL STATEMENT PROCESSING -f F ! &P'A%E clause is present in the cursor statement.actualpriceGitem4record. these loc)s prevent other sessions from chan*in* the rows in the active set until the transaction is committed.actualpriceGitem4record. C ==-% cannot be used within a fetch loop.DD. E#!mple !etrieve the items for specific order one by one. any column can be updated. -f the cumulative total for the order reaches JFFF then delete the remainin* items.item4record. -f no columns are listed. PL1SQL Co2e ACCEP% p4ord4id P! =P%?Please enter the order id8? 'EC"A!E 94ordid item.

%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. store them in P"+$(" tables. 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. As each name and salary is retrieved within the loop. $tore the names and salaries in the % P4$A" table. to temporarily store the names and salaries. *et ename and sal for top n people with respect to sal in the E=P table. -n a loop. /A=E4%A3"E and $A"A!: 4%A3"E. Cursor manipulation is done throu*h PE/. Accept a number n as user input with $(" G Plus substitution parameter.Alchemy Solutions SQL STATEMENT PROCESSING S%MMAR4 56 • • racle creates a private $(" areas to excute $(" statement and store information called cursors. • !edo the above exercise to incorporate P"+$(" tables. • • • • • L!b E#e"cises 12 . 'eclare two P"+$(" tables. Assume that no two employees have the same salary. • 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.

13 .Alchemy Solutions SQL STATEMENT PROCESSING utside the loop. transfer the names and salaries from the P"+$(" tables into the % P4$A" table.