cursor management

Upload: kothasudarshan-kumar

Post on 03-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Cursor Management

    1/8

    Avoid COUNT(*) Unless Count is RequiredConsider the following requirement:Get the ID for the company which matches specified name.If no match, display error.If more than one match, show list.

    If single match, return ID.How do you handle this situation? The most o !ious and common solution is to useC"#$T to figure out how many matches you ha!e for the name, and then e%ecute logicfrom there. &ut are you really answering the 'right question?'(hen you do a C"#$T, you find out how many matches there are. If you don)t reallycare how many, ut *ust that there is more than one match, C"#$T is o!er+ill. ore%ample:

    &-GI$ -/-CT C"#$T012 I$T" the3count 4"5 company

    (H-4- name /I6- :cllr.cname7 I the3count 8 9 TH-$ -/-CT company3id I$T" the3id 4"5 company (H-4- name 8 :cllr.cname7 return3!alue 0the3id27 -/ I the3count 9 TH-$ show3list7 -/ I the3count 8 ;

    TH-$ display 0)$o matches found.)27 -$D I 7-$D7

    $otice a second -/-CT has to e e%ecuted to retrie!e the single ID. In this case, wedon4"(T @-7&-GI$ "@-$ comp3cur7 -TCH comp3cur I$T" comp3rec7 I comp3cur>$"T "#$D TH-$ display 0)$o match found.)27

  • 8/13/2019 Cursor Management

    2/8

    -/ - -TCH comp3cur I$T" comp3rec7 I comp3cur> "#$D TH-$ show3list7

    -/ - :employee.company3id :8 comp3rec.company3id7 :employee.company3nm :8 comp3rec.company3nm7 -$D I 7 -$D I 7

    C/" - comp3cur7 -$D7

    =d!antages of Dual etch =pproach= solute minimum of C@# and memory employed to meet requirement.

    =!oids duplication of A/ statement.ou would ha!e to maintain the (H-4- clause in oth as there were data ase changes.&e sure to read your requirements carefully and decide on the most sensi le approach.(hat seems at first glance to e the most sensi le solution may turn out to ha!e ma*ordraw ac+s.

    (hat a out an B-fficient C"#$T012?-/-CT C"#$T012 I$T" my3count

    4"5 emp (H-4- empno 8 emp3in =$D 4"($#5 E7In the a o!e implicit cursor, 4"($#5 is used to ma+e sure that only one row isretrie!ed. This query will not e!er return a !alue greater than one, and it will return ; ifno matches are found. = good fit?

    There are se!eral draw ac+s to this approach:

    The A/ layer still performs two fetches.ou are Btric+ing @/F A/. ou don

  • 8/13/2019 Cursor Management

    3/8

  • 8/13/2019 Cursor Management

    4/8

    CURSOR RECORDS

    Declare a record for the cursor with >4"(T @- attri ute.=utomatically ta+es on structure of -/-CT column list.Don

  • 8/13/2019 Cursor Management

    5/8

    =!oid Hard Coding Cursor @arametersCompare the following two cursors7 one uses an em edded ind !aria le,the other a parameter. The first cursor requires the presence of thenew3company3id local !aria le to compile and e%ecute. The second cursorcan e used where!er a company id is a!aila le.

    D-C/=4- C#4 "4 company3cur I -/-CT comp3nm 4"5 company (H-4- company3id 8 new3company3id7 &-GI$ "@-$ company3cur7 D-C/=4- C#4 "4 company3cur

    0id3in I$ company.company3id>T @-2 I -/-CT comp3nm 4"5 company (H-4- company3id 8 id3in7 &-GI$ "@-$ company3cur 0new3company3id27

    CURSOR PACKA ES

    Consolidate Cursors and A/ in @ac+ages&y placing cursors inside pac+ages, you control the use of A/ in your@/F A/ code. This remo!es the urden of +nowledge a out comple% *oins,etc., from indi!idual de!elopers. It also allows you to maintain one area inwhich data ase structure changes impact @/F A/ programs.It may not e practical, ut it is pro a ly worth attempting to perform anad!ance analysis of all of the programmers< needs to produce a full set ofqueries pre defined in the pac+age.

    @=C6=G- emp3access I C#4 "4 emp3all ...7 C#4 "4 in3dept3order ... 7 C#4 "4 in3sal3order ... 7 -$D emp3access7

  • 8/13/2019 Cursor Management

    6/8

    MANA !N CURSOR PACKA ES

    Declaring @ac+aged CursorsIf you are declaring an e%plicit cursor in a pac+age specification, you ha!etwo options:Declare the entire cursor, including the query, in the specification. This ise%actly the same as if you were declaring a cursor in a local @/F A/ loc+.Declare only the header of the cursor and do not include the query itself. Inthis case, the query is defined in the pac+age ody only . ou ha!e, in effect,hidden the implementation of the cursor.If you declare only the header, then you must add a 4-T#4$ clause to acursor definition that indicates the data elements returned y a fetch from thecursor. "f course, these data elements are actually determined y the

    -/-CT statement for that cursor, ut the -/-CT statement appears onlyin the ody, not in the specification.The 4-T#4$ clause may e made up of either of the following datatypestructures:

    = record defined from a data ase ta le using the >4"(T @- attri ute= record defined from a programmer defined recordIf you declare a cursor in a pac+age ody, the synta% is the same as if youwere declaring it in a local @/F A/ loc+.Here is a simple pac+age specification that shows oth of these approaches:

    9 C4-=T- "4 4-@/=C- @=C6=G- oo+3infoE IJ C#4 "4 yauthor3cur 0K author3in I$ oo+s.author>T @-L 2M IN -/-CT 1O 4"5 oo+s

    P (H-4- author 8 author3in79;99 C#4 "4 ytitle3cur 09E title3filter3in I$ oo+s.title>T @-9J 2 4-T#4$ oo+s>4"(T @-79K9L T @- author3summary3rt I 4-C"4D 0

  • 8/13/2019 Cursor Management

    7/8

    9M author oo+s.author>T @-,9N total3page3count @/ 3I$T-G-4,9O total3 oo+3count @/ 3I$T-G-4279PE; C#4 "4 summary3cur 0E9 author3in I$ oo+s.author>T @-EE 2 4-T#4$ author3summary3rt7EJ -$D oo+3info7"n lines JQP you can see a !ery typical e%plicit cursor definition, fullydefined in the pac+age specification. "n lines 99Q9J, I define a cursorwithout a query. In this case, I am telling whoe!er is loo+ing at thespecification that if they open and fetch from this cursor they will recei!e asingle row from the oo+s ta le for the specified Btitle filter, theimplication eing that wild cards are accepted in the description of the title.

    "n lines 9LQ9O, I define a new record type to hold summary information fora particular author, and on lines E;QEE, I declare a cursor that returnssummary information 0*ust three !alues2 for a gi!en author.

    (or+ing with @ac+aged Cursors $ow let4"(T @-7&-GI$ "@-$ oo+3info. ytitle3cur 0)>@/F A/>)27 /""@ - IT (H-$ oo+3info. ytitle3cur>$"T "#$D7 -TCH oo+3info. ytitle3cur I$T" one oo+7 oo+3info.display 0one oo+27

    -$D /""@7 C/" - oo+3info. ytitle3cur7-$D7=s you can see, you can >4"(T @- a pac+aged cursor and chec+ itsattri utes *ust as you would with a locally defined e%plicit cursor.

  • 8/13/2019 Cursor Management

    8/8