+ All Categories
Home > Documents > Advanced Techniques of PL/SQL

Advanced Techniques of PL/SQL

Date post: 06-Mar-2016
Category:
Upload: sunilrgurav
View: 11 times
Download: 0 times
Share this document with a friend
Description:
Advanced Techniques of PL/SQL

of 269

Transcript
  • 7/21/2019 Advanced Techniques of PL/SQL

    1/269

    PL/SQL Advanced Techniques - page 1

    Achieving PL/SQL Excellence

    Oracle PL/SQLAdvanced Techniques

    Oracle7 thru Oracle8i

    Steven euerstein

    !!!"Steven euerstein"c#$!!!"Quest"c#$!!!"O%eill&"c#$

    and c#ntri'uti#ns $#st excellent (r#$ )ill Pri'&l and *ic+ )#l,

  • 7/21/2019 Advanced Techniques of PL/SQL

    2/269

    PL/SQL Advanced Techniques - page

    O'.ective OutlineO'.ective

    Expand ur +n#!ledge and a!areness #( i$p#rtant and ne!(eatures #( the PL/SQL language"

    Outline )uilding !ith Pac+ages 0Oracle7 2 PL/SQL 3#llecti#ns 0Oracle7 and Oracle8 2 3urs#r 4aria'les 0Oracle7 and Oracle8 2 *&na$ic SQL5 *)6S SQL and ative *&na$ic SQL 08i2 3alling 9ava (r#$ PL/SQL 0Oracle8i2 and 3 0Oracle82 Oracle Advanced Queuing !ith *)6S AQ 0Oracle82 6anaging Large O'.ects !ith *)6S LO) 0Oracle82 Other Oracle8i e! eatures

    Autonomous Transactions (Oracle8i) Invoker Rights Model (Oracle8i) Row Level Security !"MS#RLS

  • 7/21/2019 Advanced Techniques of PL/SQL

    3/269

    PL/SQL Advanced Techniques - page :

    S#(t!are ;sed in TrainingPL/4isi#n5 a li'rar& #( pac+ages installed #n t#p #( PL/SQL" PL/4isi#n Lite - use it< c#p&< change it (#r (ree -- unless u 'uild

    s#(t!are t# 'e s#ld c#$$erciall&" Active PL/SQL =n#!ledge )ase5 c#ntains PL/4isi#n Pr#(essi#nal< the

    (ull& supp#rted and enhanced versi#n"

    *e$#nstrati#n scripts executed in the training can 'e(#und #n the %eveal et PL/SQL Pipeline5 http5//!!!"revealnet"c#$/Pipelines/PLSQL/index"ht$ Archives sur('#ard< 6iscellane#us< PL/SQL Se$inar iles See (iledesc"d#c (#r a listing #( $an& #( the (iles"

    The PL/SQL >*E 0>ntegrated *evel#p$ent Envir#n$ent2" ?#u n# l#nger have t# use SQL@Plus and a crude edit#r 3h##se (r#$

    a$#ng the $an& listed in plsql ides"txt"

    plsql ides"txt

  • 7/21/2019 Advanced Techniques of PL/SQL

    4/269

    PL/SQL Advanced Techniques - page B

    )uilding !ithPL/SQL Pac+ages

    Achieving PL/SQL Excellence

    Overvie!

    >nitiali,ati#n secti#n

    Overl#ading

  • 7/21/2019 Advanced Techniques of PL/SQL

    5/269PL/SQL Advanced Techniques - page C

    Dhat is a Pac+ageA c#llecti#n #( c#de ele$ents< (r#$ pr#cedures and

    (uncti#ns t# T?PE< varia'le and curs#r declarati#ns" Single-$#st i$p#rtant structure !ithin PL/SQL< and al$#st certainl&

    #ne #( the $#st under-utili,ed" 3#nceptuall& ver& si$ple< it can ta+e s#$e ti$e t# (ull& grasp the

    i$plicati#ns and p#tential #( the pac+age"

    The $eth#d #( ch#ice '& Oracle and #ther s#(t!aredevel#pers (#r extending the PL/SQL language" ?#u !ill (ind pac+ages in the data'ase< in Oracle *evel#per/ FFF< in

    Oracle Applicati#n Server"

    LetGs revie! s#$e #( the 'ene(its #( pac+ages"t$r"p+g

    d'par$"p+g

  • 7/21/2019 Advanced Techniques of PL/SQL

    6/269PL/SQL Advanced Techniques - page H

    Dhen t# )uild a Pac+age9#in ph&sicall& l#gicall&-related c#de"

    3an lead t# per(#r$ance i$pr#ve$ents" Puts $#re structure and #rgani,ati#n in ur '#d& #( c#de"

    >$pr#ve transacti#n integrit& '& hiding data structures

    'ehind the pac+age inter(ace" >nstead #( !riting SQL directl& in ur pr#gra$s< u call thepac+aged pr#cedures and (uncti#ns instead"

    3#nstruct ver& (lexi'le and usa'le utilities (#r devel#pers" ThereIs a 'ig di((erence 'et!een a 'unch #( separate pr#gra$s and

    a c#herent< pac+age-'ased Jc#$p#nentJ"!atch"p+g

    custrules"p+ginsga"p+g

    te e$pl#&ee"p+ste e$pl#&ee"p+'

  • 7/21/2019 Advanced Techniques of PL/SQL

    7/269PL/SQL Advanced Techniques - page 7

    Pac+age >nitiali,ati#nThe initiali,ati#n secti#n is a 'l#c+ #( c#de at the end #(

    the pac+age '#d& that is executed #nce per sessi#n< the(irst ti$e an& pac+age ele$ent is re(erenced" The PL/SQL runti$e engine deter$ines !hen and i( this c#de

    sh#uld 'e run"

    *#es thepac+age have an

    init secti#n

    Pr#gra$ re(erencespac+age ele$entthe (irst ti$e ineach sessi#n"

    no

    %un initiali,ati#nc#de"

    yes

    3#$plete request(#r pac+aged

    ele$ent"

  • 7/21/2019 Advanced Techniques of PL/SQL

    8/269PL/SQL Advanced Techniques - page 8

    Pac+age >nitiali,ati#n StructureThe initiali,ati#n secti#n5

    >s de(ined a(ter and #utside #( an& pr#gra$sin the pac+age" >s n#t required" >n (act< $#st pac+ages u

    'uild !#nIt have #ne" 3an have its #!n excepti#n handling

    secti#n"

    ;se(ul (#r5 Per(#r$ing c#$plex setting #( de(ault #r

    initial values" Setting up pac+age data !hich d#es n#t

    change (#r the durati#n #( a sessi#n" 3#n(ir$ing that pac+age is pr#perl&

    instantiated"

    PACKAGE BODY pkgIS PROCEDURE proc IS BEGIN END;

    FUNCTION func RETURN

    BEGIN END;BEGINEND pkg;

    )EK> a(ter/#utside#( an& pr#gra$

    de(ined in the p+g"

  • 7/21/2019 Advanced Techniques of PL/SQL

    9/269PL/SQL Advanced Techniques - page

    3#n(igure Sessi#n !ith >nit" Secti#nPACKAGE BODY sessini IS !" No #ec$%re# p%ck%ge e$e&en s % %$$' "!BEGIN !" Ge user preferences for (is user) "! SE*ECT $o+,f$%g- .,f$%g- #efprin er INTO s(o/,$o+- s(o/, oo$.%r- prin er

    FRO0 user,config 12ERE user,i# 3 USER;

    E4CEPTION 12EN NO,DATA,FOUNDT2EN

    !" No recor# for (is user) "! s(o/,$o+ 53 6Y6; s(o/, oo$.%r 53 6Y6; prin er 53 6$p 76;

    12EN OT2ERST2EN

    RAISE,APP*ICATION,ERROR89: -

    6No profi$e for 6 ARC2AR:8? =;END sessini ;

    An unusual pac+age Speci(icati#n c#ntains

    #nl& varia'les" )#d& c#ntains #nl&

    initiali,ati#n secti#n"

    init"p+ginit"tst

    Als# a pac+age !ith$an& design (la!s"""

  • 7/21/2019 Advanced Techniques of PL/SQL

    10/269PL/SQL Advanced Techniques - page 1F

    P#pulate 3#llecti#nsThe PL/4isi#n *ate pac+age< PL4date< e$pl#&s several

    PL/SQL ta'les t# c#nvert strings and t# per(#r$ datearith$etic" This increases the (lexi'ilit& #( the date c#nversi#n pr#cess" The date$gr"p+g (ile de$#nstrates the 'asic technique 0and the

    reliance #n an initiali,ati#n secti#n2 used t# achieve this (lexi'ilit&"

    BEGIN f& s87= 53 6DD90ON9RR6; f& s8:= 53 6DD90ON9YYYY6; f& s8@= 53 6DD90ON6; f& s8 = 53 600!DD6; ))) f& s8 = 53 600!DD!YYYY6; f& s87 = 53 600DDYYYY6; f& s877= 53 6YYYY00DD6; f& s87:= 53 6RR00DD6; f& ,coun 53 7:;END # ;

    date$gr"p+gdates"sql

    >nitiali,ati#n secti#np#pulates a

    PL/SQL ta'le"

  • 7/21/2019 Advanced Techniques of PL/SQL

    11/269PL/SQL Advanced Techniques - page 11

    Pr#gra$ Overl#adingDhen u #verl#ad pr#gra$s< u give

    t!# #r $#re pr#gra$s the same name " ?#u can #verl#ad $#dules in an& declarati#n

    secti#n and in pac+ages"$&pr#c

    $&pr#c

    $&pr#c

    Overl#ading is a critical (eature !hen'uilding c#$prehensive pr#gra$$aticinter(aces 0AP>s2 #r c#$p#nents usingpac+ages" >( u !ant #thers t# use ur c#de< u need

    t# $a+e that c#de as s$art and as eas& t# useas p#ssi'le" Overl#ading trans(ers the Jneed t# +n#!J (r#$

    the user t# the #verl#aded pr#gra$"

  • 7/21/2019 Advanced Techniques of PL/SQL

    12/269PL/SQL Advanced Techniques - page 1

    Overl#ading in PL/SQL )uilt-insPL/SQL uses #verl#ading in $an& c#$$#n (uncti#ns"

    ?#u .ust pr#'a'l& never gave it a th#ught< and t##+ (uncti#ns li+eTO 3MA% and TO *ATE t#tall& (#r granted" )ut Oracle c#uldnIt #((er that level #( c#nvenience !ith#ut #verl#ading"

    #% e,s ring 53 TO,C2AR 8SYSDATE- 600DDYY6=;

    nu&.er,s ring 53 TO,C2AR 87 =;

    Dith#ut #verl#ading< u !#uld have t# deal !ith s#$ethingli+e this5

    #% e,s ring 53TO,C2AR,FRO0,DATE 8SYSDATE- 600DDYY6=;

    nu&.er,s ring 53TO,C2AR,FRO0,NU0BER 87 =;

  • 7/21/2019 Advanced Techniques of PL/SQL

    13/269

    PL/SQL Advanced Techniques - page 1:

    M#! Overl#ading D#r+s#r t!# #r $#re $#dules t# 'e #verl#aded< the c#$piler $ust 'e a'le t#

    distinguish 'et!een the t!# calls at c#$pile-ti$e" There are t!# di((erentJc#$pile ti$esJ5 1" Dhen u c#$pile the pac+age #r 'l#c+ c#ntaining the #verl#aded c#de" " Dhen u c#$pile pr#gra$s that use the #verl#aded c#de"

    *istinguishing characteristics5 The (#r$al para$eters #( #verl#aded $#dules $ust di((er in nu$'er< #rder #r

    datat&pe (a$il& 03MA% vs" 4A%3MA% is n#t di((erent en#ugh2" The pr#gra$s are #( di((erent t&pes5 pr#cedure and (uncti#n"

    ;ndistinguishing characteristics5 uncti#ns di((er #nl& in their %ET;% datat&pe" Argu$ents di((er #nl& in their $#de 0> < O;T< > O;T2"

    Their (#r$al para$eters di((er #nl& in datat&pe and the datat&pes are in the sa$e(a$il&"

  • 7/21/2019 Advanced Techniques of PL/SQL

    14/269

    PL/SQL Advanced Techniques - page 1B

    Exa$ples #( >nvalid Overl#adingsPACKAGE oo,si&i$%rIS

    PROCEDURE c%$c 8reg,in IN C2AR=; PROCEDURE c%$c 8reg,in IN >ARC2AR:=;END oo,&%n ,c%$s;

    PACKAGE on$ ,re urnsIS

    FUNCTION func7 8+%$ IN >ARC2AR:= RETURN DATE;FUNCTION func7 8+%$ IN >ARC2AR:= RETURN >ARC2AR:;

    END on$ ,re urns;

    PACKAGE p%r%&,&o#esIS

    PROCEDURE proc7 8+%$ IN >ARC2AR:=;PROCEDURE proc7 8+%$ IN OUT >ARC2AR:=;

    END p%r%&,&o#es;

    Para$eter

    data t&pescause c#n(lict"

    Onl& di((erenceis (uncti#n

    %ET;% t&pe"

    Onl& di((erenceis para$eter

    $#de"

    oo,si&i$%r)c%$c 867:@6=;!hich #ne

    DB0S,OUTPUT)PUT,*INE 8on$ ,re urns)func7 8+,+%$ue==;!hich #ne

    p%r%&,&o#es)proc7 8+,+%$ue=;!hich #ne

  • 7/21/2019 Advanced Techniques of PL/SQL

    15/269

    PL/SQL Advanced Techniques - page 1C

    Overl#ad Dherever P#ssi'leSupp#rting 6an& *ata 3#$'inati#ns

    Appl& the sa$e acti#n t# di((erent +inds #r c#$'inati#ns #( data" >n this case E pr#cedure illustrates this technique -- and thePL/4isi#n p"l su'stitute d#es an even 'etter .#'"

    itting the Pr#gra$ t# the ;ser T# $a+e ur c#de as use(ul as p#ssi'le< u $a& c#nstruct di((erent

    versi#ns #( the Nsa$e pr#gra$ !hich c#rresp#nd t# di((erent patterns #(use"

    Overl#ading '& T&pe< n#t 4alue A less c#$$#n applicati#n #( #verl#ading" ?#u use the type #( data and n#t

    its value t# deter$ine !hich #( the #verl#aded pr#gra$s sh#uld 'e executed"

  • 7/21/2019 Advanced Techniques of PL/SQL

    16/269

    PL/SQL Advanced Techniques - page 1H

    A J3lassicJ Overl#aded Pac+age

    PACKAGE pIS PROCEDURE $

    8#% e,in IN DATE- &%sk,in IN >ARC2AR: 53 0on ( DD- YYYY 9 2250I5SS P06=;

    PROCEDURE $ 8nu&.er,in IN NU0BER=; PROCEDURE $ 8c(%r,in IN >ARC2AR:=;

    PROCEDURE $ 8c(%r,in IN >ARC2AR:- nu&.er,in IN NU0BER=;

    PROCEDURE $8c(%r,in IN >ARC2AR:- #% e,in IN DATE-

    &%sk,in IN >ARC2AR: 53 60on ( DD- YYYY 9 2250I5SS P06=;

    PROCEDURE $ 8.oo$e%n,in IN BOO*EAN=;

    PROCEDURE $ 8c(%r,in IN >ARC2AR:- .oo$e%n,in IN BOO*EAN=;END p;

    p"spsp"sp'

    6an& di((erent datat&pe c#$'inati#ns< all#!ing the user t# pass data t# the Jdispla&engineJ !ith#ut !riting Jpre-pr#cessingJ c#de"

  • 7/21/2019 Advanced Techniques of PL/SQL

    17/269

    PL/SQL Advanced Techniques - page 17

    Advantage #( Extended Overl#ading

    p)$ 86So /(% is #ifferen 6=;

    p)$ 8prin ,repor ,f$=;

    p)$ 8S *ERR0- S *CODE=;

    p)$ 8SYSDATE=;

    Extended #verl#adings are

    $#re li+el& t# $eet user needs"DB0S,OUTPUT)PUT,*INE

    86So /(% is #ifferen 6=;

    DB0S,OUTPUT)PUT,*INE 8TO,C2AR 8SYSDATE-

    600!DD!YY 2250I5SS6==;

    DB0S,OUTPUT)PUT,*INE 8S *ERR0

  • 7/21/2019 Advanced Techniques of PL/SQL

    18/269

    PL/SQL Advanced Techniques - page 18

    itting the Pr#gra$ t# the ;ser

    A single piece #( (uncti#nalit&< such as Jdispla& dataJ #r Jcreatea (ileJ< can 'e applied #r needed under ver& di((erentcircu$stances"

    >( u ta+e these di((erent circu$stances int# acc#unt !hen udesign ur pac+age speci(icati#n< the user #( ur pac+age can'ene(it (r#$ !riting less c#de" ?#ur c#de is a a $#re natural J(itJ under a variet& #(

    require$ents"

    >n $& experience< (e! devel#pers are c#nsiderate en#ugh #(their users t# tr& t# anticipate their needs" >( u !ant t# !rite s#(t!are that is ad$ired E 3OL;6 pr#cedure t# de(inethe datat&pe #( the th c#lu$n in the curs#r"

    There are three !a&s t# acc#$plish this5 *#nIt #verl#ad" *e(ine a di((erent pr#gra$ na$e (#r each

    datat&pe" Pass a string Nna$e #( the datat&pe" Pass a piece #( data #( the right t&pe"

  • 7/21/2019 Advanced Techniques of PL/SQL

    22/269

    PL/SQL Advanced Techniques - page

    Opti#ns (#r Speci(&ing 3#lu$n T&pe*#nIt even '#ther #verl#ading"""

    BEGIN DB0S,S *)DEFINE,INTEGER,CO*U0N 8cur- 7=; DB0S,S *)DEFINE,>ARC2AR:,CO*U0N 8cur- :- @ =;

    BEGIN DB0S,S *)DEFINE,CO*U0N 8cur- 7- 6NU0BER6=; DB0S,S *)DEFINE,CO*U0N 8cur- :- 6STRING6- @ =;

    BEGIN DB0S,S *)DEFINE,CO*U0N 8cur- 7- DB0S,S *)NU0BER,TYPE=; DB0S,S *)DEFINE,CO*U0N 8cur- :- DB0S,S *)>ARC2AR:,TYPE- @ =;

    Pass a na$ed c#nstant"""

    Pass a literal value"""

    #! letIs l##+ at t!# exa$ples #( #verl#ading '& datat&pe""" *)6S SQL"*E > E 3OL;6 PL4gen"(unc

    S# $an& pr#gra$na$es t# re$e$'er

    ast&hard-c#ding"""

    L#tsa t&ping E 3OL;6 pr#cedure de(ines the datat&pe #( a c#lu$n" T# $a+e it easier t# acc#$plish this tas+< u #nl& need t# pass a value -- an& value -- #( the c#rrect t&pe"

    The three c#de 'l#c+s 'el#! are equivalent< (r#$ the perspective #( *)6S SQL"*E > E 3OL;6 "

    BEGIN DB0S,S *)DEFINE,CO*U0N 8cur- 7- 7=; DB0S,S *)DEFINE,CO*U0N 8cur- :- 6%6- @ =;

    BEGIN DB0S,S *)DEFINE,CO*U0N 8cur- 7- DB0S,UTI*ITY)GET,TI0E=; DB0S,S *)DEFINE,CO*U0N 8cur- :- USER- @ =;

    BEGIN DB0S,S *)DEFINE,CO*U0N 8cur- 7- +,e&pno=; DB0S,S *)DEFINE,CO*U0N 8cur- :- +,en%&e- @ =;

  • 7/21/2019 Advanced Techniques of PL/SQL

    24/269

    PL/SQL Advanced Techniques - page B

    Kenerating uncti#ns '& 4alue>n PL4gen< the user indicates the t&pe #( (uncti#n t# 'e generated '& pr#viding a value " The particular value itsel( is #( no importance. An& nu$'er< an& date< an& string< an&

    )##lean !ill d#"

    PACKAGE P*>genIS PROCEDURE func 8n%&e,in IN >ARC2AR:- pe,in IN >ARC2AR:=;

    PROCEDURE func 8n%&e,in IN >ARC2AR:- pe,in IN NU0BER=;

    PROCEDURE func 8n%&e,in IN >ARC2AR:- pe,in IN DATE=;

    S *H e ec p$+gen)func86 o %$,s%$%r 6- 7=

    A nu$'er (uncti#n< please

    S *H e ec p$+gen)func86$%s ,#% e6- SYSDATE=

    A date (uncti#n< please

  • 7/21/2019 Advanced Techniques of PL/SQL

    25/269

    PL/SQL Advanced Techniques - page C

    The rustrati#ns #( Overl#adingDatch #ut An #verl#ading can c#$pile success(ull&< 'ut

    u $ight later (#und #ut that u cann#t actuall& call an& #(the #verl#aded pr#gra$s"

    PACKAGE profi sIS PROCEDURE c%$c 8co&p,i#,IN IN NU0BER=;

    PROCEDURE c%$c 8co&p,i#,IN IN co&p%n )co&p,i# TYPE=;END;

    >n the a'#ve exa$ple< > rel& #n an anch#red t&pe 0 T?PE2

    t# esta'lish the datat&pe #( the sec#nd calcGs para$eter" Dhen > c#$pile pr#(its< PL/SQL d#es n#t sense a c#n(lict !itha'#ve #verl#ading even th#ugh c#$p id is a nu$eric c#lu$n"

  • 7/21/2019 Advanced Techniques of PL/SQL

    26/269

    PL/SQL Advanced Techniques - page H

    Quiz! uances #( Overl#ading

    3an > #verl#ad t!# pr#gra$s !hich have para$eters that di((er #nl& '& name < li+ecalc t#tals sh#!n a'#ve >( n#t< !h& n#t >( s#< h#! !#uld u d# it 0*#nIt pee+ at the next page 2

    PACKAGE s%$es

    IS PROCEDURE c%$c, o %$ 8Jone,in IN >ARC2AR:=;

    PROCEDURE c%$c, o %$ 8reg,in IN >ARC2AR:=;

    END s%$es;

    BEGIN

    s%$es)c%$c, o %$ 86NORT21EST6=;

    s%$es)c%$c, o %$ 86 ONE:6=;END;

    sales"p+g

  • 7/21/2019 Advanced Techniques of PL/SQL

    27/269

    PL/SQL Advanced Techniques - page 7

    ;sing a$ed #tati#n

    Explicit ass#ciati#n 'et!een the (#r$al para$eter 0the Jna$eJ2!ith the actual para$eter 0the JvalueJ2"

    Advantages #( na$ed n#tati#n include5 3#de is $#re Jsel(-d#cu$entingJ" This is especiall& use(ul !hen !#r+ing

    !ith in(requentl& used 'uilt-in pr#gra$s" ?#u can s+ip #ver 0n#t speci(& values (#r2 an& > para$eters that have

    de(ault values" That !a& u d#nIt have t# know and pass de(ault values "

    Lfor&%$ p%r%&e er n%&eH 3H Le pressionH

    DB0S,MOB)su.&i 8 o. 3H +, o.no- /(% 3H 6DB0S,DD*)ANA*Y E,OBMECT 6

  • 7/21/2019 Advanced Techniques of PL/SQL

    28/269

    PL/SQL Advanced Techniques - page 8

    PL/SQL

    3#llecti#ns

    Achieving PL/SQL Excellence

    3#llecti#ns are single-di$ensi#ned lists #( in(#r$ati#n"

    Three t&pes #( c#llecti#ns5 >ndex-'& ta'les 0Oracle7 #nl&< #riginall& called PL/SQL ta'les2 ested ta'les 0Oracle8 and a'#ve2 4aria'le arra&s 04A%%A?s< Oracle8 and a'#ve2

  • 7/21/2019 Advanced Techniques of PL/SQL

    29/269

    PL/SQL Advanced Techniques - page

    Dhen t# ;se 3#llecti#ns6aintain an& +ind #( list #( related in(#r$ati#n (#r use in ur

    pr#gra$s"

    E$ulate 'i-directi#nal curs#rs< !hich are #ther!ise n#tsupp#rted in PL/SQL3ache data in sessi#n-level $e$#r& (#r (aster access"

    )uild hash ta'les 0cust#$ indexing structures2"

    >$pr#ve quer& per(#r$ance '& av#iding .#ins"Av#id $utating ta'le err#rs in data'ase triggers"

    d ) l

  • 7/21/2019 Advanced Techniques of PL/SQL

    30/269

    PL/SQL Advanced Techniques - page :F

    >ndex-)& Ta'les

    3haracteristics #( an index-'& ta'le5 ;n'#unded

    $ractically s%eaking& 'alid row num ers range *+,-.+-8/+0-. to *+,-.+-8/+0-. 1ou will not actually create ta les this large& Instead+ this road range allows you to em%loy the row num er as an

    intelligent key&

    Sparse !ata does not have to e stored in consecutive rows o2 in2ormation&

    M#$#gene#us !ata in each row has the same structure&

    Availa'le #nl& in PL/SQL

    TYPE L %.$e, peH IS TAB*E OF L#% % peH

    INDE4 BY BINARY,INTEGER;DEC*ARE TYPE in&e&,e&p, IS TAB*E OF e&p RO1TYPE

    INDE4 BY BINARY,INTEGER; e&p,cop in&e&,e&p, ;

    d '& T 'l

  • 7/21/2019 Advanced Techniques of PL/SQL

    31/269

    PL/SQL Advanced Techniques - page :1

    TYPEdeclaration

    PACKAGE BODY family IS

    TYPE child_list_type ISTABLE OF VA C!A " #$%&I'DE( BY BI'A Y_I'TEGE )

    child*e+ child_list_type )Variable

    declaration

    >ndex '& Ta'les

    children

    6412

    6904 Lisa Marie

    ar! "ichard

    #arbara $nne6%06

    Lisa &ade'h(a6904

    6)10 $da* "+ssell6412 ar! "ichard

    #arbara $nne6%06

    childrenchildren 0H81F2 5 RAda$ %ussellchildren 0H FB2 5 ILisa ade,h+aI

    ,o*-onent.election

    +id 5 children 0B2

    Err#r5O *ATA O; *

    date$gr"p+g

    d T 'l

  • 7/21/2019 Advanced Techniques of PL/SQL

    32/269

    PL/SQL Advanced Techniques - page :

    ested Ta'les

    ested ta'le characteristics M#$#gene#us

    3ach row contains the same structure o2 data&

    ;n'#unded< 'ut #nl& !ith explicit E TE * requests $ractically s%eaking& 'alid row num ers range , to *+,-.+-8/+0-.

    >nitiall& dense< 'ut can 'ec#$e sparse i( u *ELETE inner r#!s

    Availa'le '#th in PL/SQL and SQL 0as a c#lu$n in a ta'le2 The #rder #( ele$ents is n#t preserved in the data'ase

    QCREATE OR REP*ACE TYPE L %.$e, peH ISTAB*E OF L#% % peH QNOT NU** ;

    DEC*ARE TYPE /(en, IS TAB*E OF DATE; .ir (#% s /(en, ;

    d T 'l

  • 7/21/2019 Advanced Techniques of PL/SQL

    33/269

    PL/SQL Advanced Techniques - page ::

    Lisa 6arieKar& %ichard

    )O *

    ested Ta'lesCREATE OR REPLACETYPE child_table_type IS TABLE OF VARCHAR2 (3 !"

    CREATE TABLE db_#a$ily (%&' a$e VARCHAR2 (3 !)*id% child_table_type!

    +ESTE, TABLE *id% STORE AS *id%_ tab "

    )OLU

    )ar'ara Anned' (a$il&

    #r O%A3LEIs use #nl&

    surna$e +ids

    6ax %ichardEric Th#$as

    ntde$#"sql

    4 i 'l A &

  • 7/21/2019 Advanced Techniques of PL/SQL

    34/269

    PL/SQL Advanced Techniques - page :B

    4aria'le Arra&s

    3haracteristics #( varia'le arra&s5 M#$#gene#us

    3ach row contains the same structure o2 data& )#unded

    4%%er limit esta lished when the T1$3 is de2ined& Ma5imum value *+,-.+-8/+0-.

    *ense 6ever any ga%s etween de2ined rows+ can e 37T36!ed or TRIMmed&

    Availa'le '#th in PL/SQL and SQL 0as a c#lu$n in a ta'le2 And the order o2 elements are %reserved in the data ase&

    4aria'le arra&s are actuall& st#red in the *) ta'le

    QCREATE OR REP*ACE TYPE L %.$e, peH IS >ARRAY 8N= OF L#% % peH QNOT NU** ;

    DEC*ARE TYPE nu&.ers, IS >ARRAY 87 = OF NU0BER; s%$%ries nu&.ers, ;

    4 i 'l A &

  • 7/21/2019 Advanced Techniques of PL/SQL

    35/269

    PL/SQL Advanced Techniques - page :C

    4aria'le Arra&s

    vade$#"sql

    CREATE OR REPLACETYPE child_-a_type IS VARRAY (.! OF VARCHAR2 (3 !"

    CREATE TABLE db_#a$ily (%&' a$e VARCHAR2 (3 !) *id% child_-a_type!"

    )O *1 Eric Th#$as

    6ax %ichard

    )OLU1 )ar'ara Anne

    Kar& %ichard: Lisa 6arie

    d' (a$il&surna$e +ids

    *e(ining 3#llecti#ns

  • 7/21/2019 Advanced Techniques of PL/SQL

    36/269

    PL/SQL Advanced Techniques - page :H

    *e(ining 3#llecti#nsirst< u de(ine the T?PE #( the c#llecti#n"

    #r index-'& ta'les< this can #nl& #ccur in a PL/SQL declarati#nsecti#n" )est #pti#n5 pac+age speci(icati#n"

    #r nested ta'les and 4A%%A?s< u can de(ine the T?PE in thedata'ase !ith a 3%EATE state$ent< #r in a PL/SQL declarati#nsecti#n"

    Then u declare an instance #( that t&pe< a c#llecti#n< (r#$the T?PE" ?#u can declare $ultiple c#llecti#ns (r#$ that T?PE"

    CREATE OR REP*ACE PACKAGE %. pesIS

    TYPE in eger,i. IS TAB*E OF INTEGER INDE4 BY BINARY,INTEGER; TYPE in eger,n IS TAB*E OF INTEGER; TYPE in eger,+% IS >ARRAY87 = OF INTEGER; )))END %. pes;

    O'taining 3#llecti#n >n(#r$ati#n

  • 7/21/2019 Advanced Techniques of PL/SQL

    37/269

    PL/SQL Advanced Techniques - page :7

    O'taining 3#llecti#n >n(#r$ati#n

    ALL 3OLL T?PES The t&pes u have created 0#r have access t#2 in the data'ase

    ALL T?PE ATT%S Attri'utes #( the data t&pe used in the T?PE de(initi#n" The c#de used t# de(ine the c#llecti#n T?PE

    There is n# in(#r$ati#n in the data dicti#nar& availa'le (#r index-'& ta'les"

    c#lldd"sql

    SE*ECT A)% r,n%&e T6= AND T)e$e&, pe,n%&e 3 A) pe,n%&e;

    >nitiali ing 3#llecti#ns

  • 7/21/2019 Advanced Techniques of PL/SQL

    38/269

    PL/SQL Advanced Techniques - page :8

    >nitiali,ing 3#llecti#ns)e(#re u can use a c#llecti#n< it $ust 'e initiali,ed" >ndex-'& ta'les are initiali,ed aut#$aticall&< e$pt& !hen declared" ested ta'les and 4A%%A?s are at#$icall& null" ?#u $ust initiali,e

    the$ explicitl& !ith a c#nstruct#r"

    DEC*ARE TYPE nu&.ers, IS >ARRAY 87 = OF NU0BER; s%$%ries nu&.ers, 53 nu&.ers, 87 - : - @ =;BEGIN

    CREATE TYPE nu&.ers, IS >ARRAY 87 = OF NU0BER;!DEC*ARE 99 Ini i%$iJe (e co$$ec ion) s%$%ries nu&.ers, 53 nu&.ers, 87 - : - @ =;BEGIN

    T?PE de(ined inthe data'ase

    T?PE de(ined inPL/SQL

    CREATE TAB*E e&p$o ee,#enor& 8 e&p$o ee,i# INTEGER- s%$%r ,(is or nu&.ers, =;

    3#llecti#n usedin a ta'le

    3#llecti#ns #( 3#$p#sites

  • 7/21/2019 Advanced Techniques of PL/SQL

    39/269

    PL/SQL Advanced Techniques - page :

    3#llecti#ns #( 3#$p#sitesStarting !ith Oracle 7":< the Nh#$#gene#us c#ntents #( an index-'& ta'leIs r#! can 'e a rec#rd " 3an easil& create an index-'& ta'le !ith the sa$e structure as a data'ase ta'le '& declaring a rec#rd !ith

    %ODT?PE"

    Starting !ith Oracle8< the datat&pe (#r an& #( the c#llecti#n t&pes can als# 'e an #'.ect" )ut u cann#t have nested c#$p#site datat&pes"

    DEC*ARE TYPE co&p,rec pe IS RECORD 8co&p,i# co&p%n )co&p%n ,i# TYPE- o %$,re+ NU0BER=;

    TYPE co&p, %. pe IS TAB*E OF co&p,rec pe INDE4 BY BINARY,INTEGER;

    co&p, %. co&p, %. pe;BEGIN co&p, %.87=)co&p,i# 53 7 ;

    Mere !e have a threestep pr#cess" Againnclude a handler (#r O *ATA O; * #r use the E >STS$eth#d t# av#id these excepti#ns"

    BEGIN 99 E en# . 7 since I kno/ I /i$$ nee# (% &%n )

    99 Se (e +%$ue of e%c( ne/ ro/ o (e con en s of (e firs ro/) s%$%ries)E4TEND 87 - s%$%ries8s%$%ries)FIRST==;

    BEGIN IF s%$%ries)E4ISTS 8+,e&p$o ee,i#=

    T2EN 99 1e %re OK) E*SE

    DB0S,OUTPUT)PUT,*INE 86D% % for e&p$o ee no %+%i$%.$e)6=; END IF;

    preextend"tst

    3#llecti#n 6eth#ds

  • 7/21/2019 Advanced Techniques of PL/SQL

    44/269

    PL/SQL Advanced Techniques - page BB

    3#llecti#n 6eth#dsO'tain in(#r$ati#n a'#ut the c#llecti#n 3O; T returns nu$'er #( r#!s currentl& de(ined in the ta'le" E >STS returns T%;E i( the speci(ied r#! is de(ined" >%ST/LAST return l#!est/highest nu$'ers #( de(ined r#!s" E T/P%>O% return the cl#sest de(ined r#! a(ter/'e(#re the speci(ied

    r#!" L>6>T tells u the $ax" nu$'er #( ele$ents all#!ed in a 4A%%A?"

    6#di(& the c#ntents #( the c#llecti#n *ELETE deletes #ne #r $#re r#!s (r#$ the index-'& ta'le" E TE * adds r#!s t# a nested ta'le #r 4A%%A?" T%>6 re$#ves r#!s (r#$ a 4A%%A?"

    The 'uilt-in pac+age plit'l$ 0PL/sql >ndex-Ta)Le 6eth#ds2de(ines these $eth#ds"

    The *ELETE 6eth#d

  • 7/21/2019 Advanced Techniques of PL/SQL

    45/269

    PL/SQL Advanced Techniques - page BC

    The ELETE 6eth#d ?#u can delete #ne #r $#re r#!s (r#$ a c#llecti#n using*ELETE5

    BEGIN 99 De$e e %$$ ro/s & Co$$ec ion)DE*ETE;

    99 De$e e one 8 (e $%s = ro/ & Co$$ec ion)DE*ETE 8& Co$$ec ion)*AST=;

    99 De$e e % r%nge of ro/s & Co$$ec ion)DE*ETE 87 - 7 : =;END;

    *ELETE releases $e$#r&< 'ut u $a& als# !ant t# call*)6S SESS>O " %EE ; ;SE* ;SE% 6E6O%?"

    avigating Thr#ugh 3#llecti#ns

  • 7/21/2019 Advanced Techniques of PL/SQL

    46/269

    PL/SQL Advanced Techniques - page BH

    avigating Thr#ugh 3#llecti#ns;se >%ST and E T t# $#ve (r#$ 'eginning t# end"

    ;se LAST and P%>O% t# $#ve (r#$ end t# 'eginning"

    ro/in# P*S,INTEGER 53 .ir (#% s)FIRST; 99 .ir (#% s)*AST

    BEGIN *OOP E4IT 12EN ro/in# IS NU**;

    DB0S,OUTPUT)PUT,*INE8.ir (#% s8ro/in#=).es ,presen =;

    ro/in# 53 .ir (#% s)NE4T 8ro/in#=; 99 .ir (#% s)PRIOR

    END *OOP;END;

    ;sing 3#llecti#ns >nside SQL

  • 7/21/2019 Advanced Techniques of PL/SQL

    47/269

    PL/SQL Advanced Techniques - page B7

    ;sing 3#llecti#ns nside SQLested ta'les and 4A%%A?s can 'e de(ined as c#lu$ns #( a

    ta'le and re(erenced directl& !ithin SQL"

    ?#u can als# appl& SQL #perati#ns t# the c#ntents #( nestedta'les and 4A%%A?s !ith these #perat#rs5 TME - 6aps a single c#lu$n value in a single r#! t# a virtual

    data'ase ta'le

    3AST - 6aps a c#llecti#n #( #ne t&pe t# a c#llecti#n #( an#ther t&pe 6;LT>SET - 6aps a data'ase ta'le t# a c#llecti#n TA)LE - 6aps a c#llecti#n t# a data'ase ta'le

    >ndex-'& ta'les are pr#gra$$atic c#nstructs #nl&"

    ?#u cann#t $a+e a direct re(erence t# an index-'& ta'le in SQL" >nstead< s# d# indirectl& !ith a PL/SQL (uncti#n"

    ;sing 3#llecti#ns inside SQL

  • 7/21/2019 Advanced Techniques of PL/SQL

    48/269

    PL/SQL Advanced Techniques - page B8

    ;sing 3#llecti#ns inside SQL

    Lisa 6arieKar& %ichard

    )O *)OLU

    )ar'ara Anned' (a$il&

    surna$e children

    6ax %ichardEric Th#$as

    Lisa 6arieKar& %ichard)ar'ara Annec#lu$n value

    SE*ECT co$u&n,+%$ueFRO0 TAB*E 8SE*ECT c(i$#ren FRO0 #.,f%&i$

    12ERE surn%&e 3 6BO* 6=;

    UPDATE TAB*E8SE*ECT c(i$#ren FRO0 #.,f%&i$ 12ERE SURNA0E 3 6BO* =

    SET co$u&n,+%$ue 3 6*is% N%#eJ(k%6 12ERE co$u&n,+%$ue 3 6*is% 0%rie6=;

    Lisa ade,h+aKar& %ichard)ar'ara Anne

    " " ")OLU

    d' (a$il&

    surna$e children

    ;sing the TME Operat#r

  • 7/21/2019 Advanced Techniques of PL/SQL

    49/269

    PL/SQL Advanced Techniques - page B

    ;s g t e Ope at;se TME t# $anipulate 0retrieve< > SE%T< ;P*ATE< *ELETE2 c#ntents #( a nested ta'le in adata'ase ta'le" 3an #nl& use !ith nested ta'les< n#t 4A%%A?s #r index-'& ta'les"

    Onl& accessi'le (r#$ !ithin SQL state$ents in PL/SQL"

    CREATE TYPE %c ion,$is , IS TAB*E OF >ARC2AR:87 =;!CREATE TAB*E inf$% ion,.e% er 8

    focus,%re% >ARC2AR:87 =- %c i+i ies %c ion,$is , = NESTED TAB*E %c i+i ies STORE AS %c i+i ies, %.;

    SE*ECT >A*UE 8%c =FRO0 T2E 8SE*ECT %c i+i ies FRO0 inf$% ion,.e% er

    12ERE focus,%re% 3 6FORTUNE 7 6= %c ;

    UPDATE T2E 8SE*ECT %c i+i ies FRO0 inf$% ion,.e% er 12ERE focus,%re% 3 6FORTUNE 7 6=

    SET CO*U0N,>A*UE 3 6DISBAND OS2A6 12ERE CO*U0N,>A*UE 3 6SIDESTEP OS2A6;

    the"sql

    ;sing the TA)LE and 3AST Operat#rs

  • 7/21/2019 Advanced Techniques of PL/SQL

    50/269

    PL/SQL Advanced Techniques - page CF

    ; g ) p;se 3AST t# c#nvert a c#llecti#n (r#$ #ne t&pe t# an#ther< TA)LE t# c#nvert a T?PE int# a data'aseta'le" 3ann#t use !ith index-'& ta'les"

    ;se(ul !hen u !#uld li+e t# appl& SQL #perati#ns against a PL/SQL c#llecti#n 0ie< #ne n#t st#red in a data'ase ta'le2"

    DEC*ARE n c,#e+o$u ion cu .%cks,for, % cu s 53

    cu .%cks,for, % cu s 86S op r% e er&in% ion progr%&s6-6Fire .ui$#ing inspec ors6- 6C$ose pu.$ic (ospi %$s6=;BEGIN DB0S,OUTPUT)PUT,*INE 8 62o/ o 0%ke (e NYC Ric( 0uc(- 0uc( Ric(er56=; FOR rec IN 8SE*ECT CO*U0N,>A*UE o(&

    FRO0 TAB*E 8CAST 8n c,#e+o$u ion AS cu .%cks,for, % cu s=== *OOP DB0S,OUTPUT)PUT,*INE 8rec)o(& =; END *OOP;END;

    cast"sql

    ;sing the 6;LT>SET Operat#r

  • 7/21/2019 Advanced Techniques of PL/SQL

    51/269

    PL/SQL Advanced Techniques - page C1

    ; g ; p6;LT>SET is the inverse #( TA)LE< c#nverting a set #( data 0ta'le< vie!< quer&2 int# a 4A%%A? #r nested ta'le" 3ann#t use !ith index-'& ta'les" ?#u can use 6;LT>SET t# e$ulate #r trans(#r$ relati#nal .#ins int# c#llecti#ns< !ith p#tential client-server per(#r$ance i$pact"

    DEC*ARE CURSOR .ir#,curs IS

    SE*ECT .)genus- .)species-CAST8 0U*TISET 8SE*ECT .()coun r FRO0 .ir#,(%.i % s .(

    12ERE .()genus 3 .)genus AND .()species 3 .)species= AS coun r , %., = FRO0 .ir#s .; .ir#,ro/ .ir#,curs RO1TYPE;

    BEGIN OPEN .ir#,curs; FETC2 .ir#,curs in o .ir#,ro/;END; $ultiset"sql

    %etrieves all detailin(#r$ati#n (#r the

    $aster in #ne trip"

    %e(erencing >) Ta'les inside SQL

  • 7/21/2019 Advanced Techniques of PL/SQL

    52/269

    PL/SQL Advanced Techniques - page C

    CREATE OR REP*ACE PACKAGE i. %. IS FUNCTION ro/+%$ 8in# IN P*S,INTEGER= RETURN DATE; PRAG0A RESTRICT,REFERENCES 8ro/+%$- 1NPS- 1NDS=;END;

    CREATE OR REP*ACE PACKAGE BODY i. %. IS TYPE #% e, %. IS TAB*E OF DATE INDE4 BY BINARY,INTEGER; (ire#% es #% e, %.;

    FUNCTION ro/+%$ 8in# IN P*S,INTEGER= RETURN DATE IS BEGIN RETURN (ire#% es 8in# =; END;END;

    ( g ) Q ?#u canIt directl& re(erence an index-'& ta'leIs c#ntentsinside SQL"

    >nstead< call (uncti#ns that retrieve the ta'leIs data< 'ut hidethe index-'& ta'le structure"

    i'ta' in sql"sql

    6a+e accessi'lein SQL (#r

    Oracle8 and'el#!"

    Exa$ples #( 3#llecti#ns in Acti#n

  • 7/21/2019 Advanced Techniques of PL/SQL

    53/269

    PL/SQL Advanced Techniques - page C:

    p (

    E$ulati#n #( 'i-directi#nal curs#r #perati#ns

    Av#id $utating ta'le pr#'le$s in data'asetriggers"

    )i-*irecti#nal 3urs#r E$ulati#n

  • 7/21/2019 Advanced Techniques of PL/SQL

    54/269

    PL/SQL Advanced Techniques - page CB

    CREATE OR REP*ACE PACKAGE .i#irIS

    !" I er% e (roug( ro/s in (e resu$ se "! PROCEDURE se Ro/ 8n ( IN P*S,INTEGER=; FUNCTION ge Ro/ RETURN e&p$o ee,p$us RO1TYPE; PROCEDURE ne Ro/; PROCEDURE pre+Ro/;END;

    )Oracle d#es n#t &et supp#rt the a'ilit& t# $#ve 'ac+ and(#rth 0and at rand#$2 thr#ugh a curs#rIs result set" A tal+ed-a'#ut (eature (#r Oracle i -- n#pe< didnIt $a+e it

    >nstead< dep#sit ur data in a c#llecti#n and then pr#videpr#gra$s t# access that data in the necessar& (ashi#n"

    This is particularl& use(ul 0read5 e((icient2 !hen u need t#per(#r$ $ultiple passes against the data"

    'idir"p+g'idir"tst

    #tice that thec#llecti#n itsel( is

    hidden"

    The 6utating Ta'le Pr#'le$

  • 7/21/2019 Advanced Techniques of PL/SQL

    55/269

    PL/SQL Advanced Techniques - page CC

    %#! level triggers cann#t quer& (r#$ #rchange the c#ntents #( the ta'le t# !hich it isattached it is J$utatingJ"

    S# !hat are u supp#sed t# d# !hen a r#!-level #perati#n needs t# Jt#uchJ that ta'le

    UPDATE ro/ 7

    UPDATE ro/ N

    UPDATE e&p SET s%$ 3 7State$ent Level

    %#! Level

    *ata'ase triggers can 'e attached t# the SQL state$entand/#r the individual r#! #perati#ns #n a ta'le"

    #te5 in Oracle8i< ucan use aut#n#$#ustransacti#ns t# relax

    restricti#nsass#ciated !ithqueries"

    $utating"sql

    A S#luti#n )ased #n >ndex-'& Ta'les

  • 7/21/2019 Advanced Techniques of PL/SQL

    56/269

    PL/SQL Advanced Techniques - page CH

    Since u cann#t per(#r$ the pr#cessing desired in the r#!-level trigger< u need t# defer the acti#n until u get t# thestate$ent level">( u are g#ing t# de(er the !#r+< u have t# re$e$'er !hatu needed t# d#" an index-'& ta'le is an ideal rep#sit#r& (#r this re$inder list"

    7s ro/ rigger fires

    N ( ro/ rigger fires

    D#r+ List0PL/SQL Ta'le2

    S % e&en Trigger

    Drites t# list

    Drites t# list

    Pr#cess datain the list"

    An Exa$ple5 %an+ing Salespe#ple

  • 7/21/2019 Advanced Techniques of PL/SQL

    57/269

    PL/SQL Advanced Techniques - page C7

    A ta'le h#lds the ran+ings 'ased #n the a$#unt #( annualsales #( salespe#ple !ithin a depart$ent" As the sales a$#unt is updated in this ta'le< the ran+ings $ust als#

    change t# sh#! the ne! standings for that department #nl&"

    Salespers#n >* Sales A$#unt %an+*epart$ent >*

    7 ? @@@ ) @

    7 ? 7 @@) 77 ? ? 7 ):

    7 ? 77):

    7:

    ?

    J*e(erred !#r+J is n#t #nl& necessar&< 'ut pre(era'le"

    )& st#ring the salespers#nGs depart$ent ids as the& change< !e then+n#! !hich depart$ents t# re-ran+" De $ight update $#re than #ne depart$ent !ithin a state$ent s# !e

    $ust 'e a'le t# retain $ultiple depart$ent nu$'ers"

    Trigger L#gic %equired

  • 7/21/2019 Advanced Techniques of PL/SQL

    58/269

    PL/SQL Advanced Techniques - page C8

    CREATE OR REP*ACE TRIGGER R%nk,S%$es,S rg AFTER INSERT OR UPDATE OR DE*ETE

    ON r%nk,s%$es

    BEGIN r%nk)r%nk,#ep s;END;

    State$ent Level Trigger

    All details #( theran+ing are hidden

    in the pac+age '#d&"

    CREATE OR REP*ACE TRIGGER R%nk,S%$es,R rg AFTER inser OR up#% e OF s%$es,%& ON r%nk,s%$es FOR EAC2 RO1 12EN 8O*D)s%$es,%& '3 NE1)s%$es,%& =BEGIN r%nk)%##,#ep 85ne/)#ep ,i#=;END;

    %#! Level Trigger *#esnIt (ire unless

    the sales a$tis actuall& changed"

    ran+ing"p+g

    The %an+ing Pac+age

  • 7/21/2019 Advanced Techniques of PL/SQL

    59/269

    PL/SQL Advanced Techniques - page C

    PACKAGE r%nkIS PROCEDURE %##,#ep 8#ep ,i#,in IN INTEGER=;

    PROCEDURE r%nk,#ep s;END r%nk;

    PACKAGE BODY r%nkIS in,process BOO*EAN 53 FA*SE;

    TYPE #ep , %. pe IS TAB*E OF BOO*EAN INDE4 BY BINARY,INTEGER; #ep , %. #ep , %. pe;

    PROCEDURE %##,#ep 8#ep ,i#,in IN INTEGER= IS BEGIN IF NOT in,process

    T2EN #ep , %. 8#ep ,i#,in= 53 TRUE; END IF; END %##,#ep

    Ta'le h#lds indicat#rthat depart$ent

    needs re-ran+ing"

    3reate r#! t# indicate depart$ent t# 'e ran+ed"

    The %an+ing Pac+age< 3#ntinued

  • 7/21/2019 Advanced Techniques of PL/SQL

    60/269

    PL/SQL Advanced Techniques - page HF

    PROCEDURE r%nk,#ep sIS

    +,#ep i# P*S,INTEGER 53 #ep , %.)FIRST; BEGIN

    IF NOT in,process T2EN in,process 53 TRUE; *OOP

    E4IT 12EN +,#ep i# IS NU**; perfor&,r%nking 8+,#ep i#=;

    +,#ep i# 53 #ep , %.)NE4T 8+,#ep i#=; END *OOP; END IF;

    in,process 53 FA*SE;

    #ep , %.)DE*ETE; END r%nk,#ep ;

    END r%nk;

    Av#id recursiveexecuti#n #( l#gic

    %#! nu$'er isdepart$ent nu$'er"

    3lean up (#r next ti$e"

    Dhich 3#llecti#n T&pe Sh#uld > ;se

  • 7/21/2019 Advanced Techniques of PL/SQL

    61/269

    PL/SQL Advanced Techniques - page H1

    >ndex-'& ta'les

    eed t# use in '#th Oracle7 and Oracle8 applicati#ns Dant t# ta+e advantage #( sparse nature (#r Jintelligent +e&sJ"

    ested ta'les ?#u !ant t# st#re large a$#unts #( persistent data in a c#lu$n" ?#u !ant t# use inside SQL"

    4A%%A?s ?#u !ant t# preserve the #rder in !hich ele$ents are st#red" Set #( data is relativel& s$all 0av#id r#! chaining2" ?#u !ant t# use inside SQL" ?#u d#nIt !ant t# have t# !#rr& a'#ut sparseness"

    Tips (#r ;sing 3#llecti#ns

  • 7/21/2019 Advanced Techniques of PL/SQL

    62/269

    PL/SQL Advanced Techniques - page H

    Drap access t# ur c#llecti#ns" >n $an& cases< u !ill !ant t# av#id direct access t# 0assigning

    and retrieving2 r#!s in ur c#llecti#ns" This !ill give u the (lexi'ilit& t# change ur i$ple$entati#n" ?#u can als# hide c#$plex rules (#r setting the r#! nu$'er"

    Ket creative

    *#nIt al!a&s (ill and use the index-'& ta'le sequentiall&" >( u can s#$eh#! translate ur applicati#n data t# an integer< it

    can 'e used as a r#! nu$'er< and there(#re #((ers indexed access" 9ulian date (#r$ats and *)6S ;T>L>T?"KET MASM 4AL;E #((er

    t!# di((erent $eth#ds"

    Achieving PL/SQL Excellence

  • 7/21/2019 Advanced Techniques of PL/SQL

    63/269

    PL/SQL Advanced Techniques - page H:

    3urs#r 4aria'les

    Architecture #( 3urs#r 4aria'les

  • 7/21/2019 Advanced Techniques of PL/SQL

    64/269

    PL/SQL Advanced Techniques - page HB

    %esultSet

    %esultSet

    %esultSet

    %esult

    Set

    A curs#r varia'le p#ints t# an underl&ing cursor object in thedata'ase" The curs#r #'.ect in turns p#ints t# 0and +eeps its place in2 a result set"

    The curs#r varia'le can 'e passed 'et!een pr#gra$s 0even'et!een< sa&< a 9ava servlet and a PL/SQL st#red pr#cedure2" Static SQL #nl& -- until Oracle8i"

    3urs#rO'.ect

    3urs#rO'.ect

    3urs#r4aria'le

    3urs#r4aria'le

    Mard-3#ded3urs#r

    Mard-3#ded3urs#r

    3urs#r4aria'le

    3urs#r4aria'lePKA

    SharedKl#'alArea

    Dith Mard-3#ded 3urs#rs Dith 3urs#r 4aria'les

    )ene(its #( 3urs#r 4aria'les

  • 7/21/2019 Advanced Techniques of PL/SQL

    65/269

    PL/SQL Advanced Techniques - page HC

    Share curs#r $anage$ent 'et!een pr#gra$s< even acr#ssthe client-server divide" ?#u d#nIt have t# pass the result sets #( a curs#r in #rder t# all#!

    the client-side pr#gra$ t# have direct access t# the data in the resultset"

    Oracle *evel#per "1 utili,es curs#r varia'les !hen u ch##se t#c#nstruct a J'ase ta'le 'l#c+J ar#und st#red pr#cedures instead #(

    a data'ase ta'le"

    Share the sa$e c#de acr#ss $ultiple< di((erent queries" Since the curs#r na$e is n# l#nger hard-c#ded< u can use a single

    'l#c+ #( c#de 0sa&< a rep#rting pr#gra$2 against di((erent queries" De !ill tr& #ut this technique at the end #( the secti#n"

    3urs#r 4aria'le Exa$ple

  • 7/21/2019 Advanced Techniques of PL/SQL

    66/269

    PL/SQL Advanced Techniques - page HH

    ,ECLARE TYPE c/$pa y_c&'type

    ISREF C0RSOR RET0R+ c/$pa y1RO TYPE"

    c/$pa y_c&'-a' c/$pa y_c&'type"

    c/$pa y_'ec c/$pa y_c&'-a'1RO TYPE"

    BE I+

    OPE+ c/$pa y_c&'-a' FORSELECT 4 FRO5 c/$pa y "

    FETCH c/$pa y_c&'-a' I+TO c/$pa y_'ec"

    CLOSE c/$pa y_c&'-a'"E+,"

    *eclare a varia'lecurs#r T?PE"

    *eclare curs#r varia'le'ased #n that t&pe"

    *eclare a rec#rd(r#$ curs#r varia'le"

    OPE curs#r varia'leARC2AR:=RETURN pkg)c+, pe

    IS re +%$ pkg)c+, pe;BEGIN IF ge , pe,in 3 E0P T2EN OPEN re +%$ FOR SE*ECT " FRO0 e&p; E*SIF ge , pe,in 3 DEPT T2EN OPEN re +%$ FOR SE*ECT " FRO0 #ep ; END IF; RETURN re +%$;END;

    PACKAGE pkg IS TYPE c+, pe IS REF CURSOR;END;

    Either quer& !ill Jd#J"4eri(icati#n !ill ta+eplace at the ET3M"

    %E T?PE placed inpac+age s# that it

    is Jgl#'all&J availa'le"

    etching (r#$ the 3urs#r 4aria'le

  • 7/21/2019 Advanced Techniques of PL/SQL

    73/269

    PL/SQL Advanced Techniques - page 7:

    etching !ith curs#r varia'les (#ll#!s the sa$e rules asth#se !ith static curs#rs"

    The > TO structure $ust $atch in nu$'er and datat&pe t#5 O% ST%O K curs#r t&pes< it $atch the curs#r t&pe data

    speci(icati#n" O% DEA= curs#r t&pes< it $atch the OPE O% state$ent

    structure"

    3#$pati'ilit& chec+s are per(#r$ed pri#r t# (etching r#!" The %ODT?PE 6>S6AT3M excepti#n is raised #n (ailure"

    etching can c#ntinue !ith a di((erent > TO clause"

    FETC2 cursor,+%r,n%&e INTO recor#,n%&e;FETC2 cursor,+%r,n%&e INTO +%r,n%&e- +%r,n%&e- )));

    $is$atch"sql

    Dhen t# ;se 3urs#r 4aria'les

  • 7/21/2019 Advanced Techniques of PL/SQL

    74/269

    PL/SQL Advanced Techniques - page 7B

    6a+e it easier (#r calling pr#gra$s 0especiall& n#n-PL/SQLpr#gra$s2 t# $anipulate result sets" 9*)3 rec#gni,es curs#r varia'les"

    *e(ine a 'ase ta'le 'l#c+ in #r$s )uilder 0(#r$erl& Oracle#r$s2 #n st#red pr#cedures rather than a ta'le directl&"

    ;se a single 'l#c+ #( c#de t# $anipulate $ultiple queries" Dith explicit curs#rs< u have t# repeat the c#de (#r each curs#r66E*>ATE Vsql stringW< used (#r **L< *6L and single

    r#! (etches" OPE O% Vsql stringW< used (#r $ulti-r#! queries"

    E E3;TE >66E*>ATE

  • 7/21/2019 Advanced Techniques of PL/SQL

    83/269

    PL/SQL Advanced Techniques - page 8:

    ;se this state$ent t# execute an& d&na$ic SQL state$ent0including a PL/SQL 'l#c+2 except (#r $ulti-r#! queries"

    The > TO clause all#!s u t# pass values (r#$ the selectlist #( a single r#! quer& int# l#cal varia'les< including#'.ects< c#llecti#ns and rec#rds"

    The ;S> K clause all#!s u t# speci(& 'ind argu$ents #rvaria'les t# 'e passed int# the SQL string 'e(#re executi#n"

    E6EC0TE I55E,IATE %7l8%t'i 9

    : I+TO ;de#i e_-a'iable:) de#i e_-a'iable% 'ec/'d ?< : 0SI+ ;I+ > O0T > I+ O0T< bi d a'9&$e t :) ;I+ > O0T > I+ O0T< bi d a'9&$e t

  • 7/21/2019 Advanced Techniques of PL/SQL

    84/269

    PL/SQL Advanced Techniques - page 8B

    MereIs a hand& and si$ple utilit& ased #n S5

    IF %.Coun 86ci iJens6- 6insure# 3 66NO666= H - -T2EN DB0S,OUTPUT)PUT,*INE 8 6No (e .es (e%$ ( c%re s s e& in (e /or$#))) %n# no &uc( of % #e&ocr%c ei (er'6=;END IF;

    ta'c#unt81"s( c#$pare !ith5

    ta'c#unt"s(

    CREATE OR REP*ACE FUNCTION %.Coun 8 %. IN >ARC2AR:- /(r IN >ARC2AR: 53 NU**- sc( IN >ARC2AR: 53 NU**= RETURN INTEGER IS re +%$ INTEGER;BEGIN E4ECUTE I00EDIATE

    6SE*ECT COUNT8"= FRO0 6 * 8sc(- USER=

  • 7/21/2019 Advanced Techniques of PL/SQL

    85/269

    PL/SQL Advanced Techniques - page 8C

    ( pCREATE OR REP*ACE PROCEDURE up#nu&+%$ 8 %.,in IN >ARC2AR:- co$,in IN >ARC2AR:- s %r ,in IN DATE- en#,in IN DATE- +%$,in IN NU0BER= ISBEGIN E4ECUTE I00EDIATE 6UPDATE 6 ARC2AR:- n%&e,in IN >ARC2AR:= IS +,s r >ARC2AR: 87 =;BEGIN

    +,s r 53 6BEGIN 6

  • 7/21/2019 Advanced Techniques of PL/SQL

    86/269

    PL/SQL Advanced Techniques - page 8H

    PROCEDURE %##,profi ,source 8 (osp,n%&e IN >ARC2AR:- pers IN Person-

    con# IN pree is ing,con#i ions=ISBEGIN E4ECUTE I00EDIATE

    6INSERT INTO 6

  • 7/21/2019 Advanced Techniques of PL/SQL

    87/269

    PL/SQL Advanced Techniques - page 87

    ( (supp#rt $ulti-r#! d&na$ic queries"

    Mere is a si$ple utilit& the displa&s the values #( an& date< nu$'er #rstring c#lu$n in an& ta'le"

    CREATE OR REP*ACE PROCEDURE s(o/co$ 8 %. IN >ARC2AR:- co$ IN >ARC2AR:- /(r IN >ARC2AR: 53 NU**=IS TYPE c+, pe IS REF CURSOR; c+ c+, pe;

    +%$ >ARC2AR:8@: ? =;BEGIN OPEN c+ FOR 6SE*ECT 6

  • 7/21/2019 Advanced Techniques of PL/SQL

    88/269

    PL/SQL Advanced Techniques - page 88

    p $ $ $ $na$es< etc"2 thr#ugh the ;S> K clause"

    ?#u cann#t pass the ;LL literal directl& in the ;S> Kclause" >nstead< pass a varia'le !ith a ;LL value"

    The ;S> K clause (#r a quer& can #nl& have > 'indargu$ents"

    ?#u can have duplicate placeh#lders 0(#r 'ind argu$ents2" >( d&na$ic SQL< then u pr#vide a value (#r each placeh#lder 0'&

    p#siti#n2" >( d&na$ic PL/SQL< pr#vide a value (#r each distinct placeh#lder 0'&

    na$e2"

    str list"p+g

    *&na$ic SQL using *)6S SQLPri#r t# Oracle8i< the #nl& !a& t# per(#r$ d&na$ic SQL !as

  • 7/21/2019 Advanced Techniques of PL/SQL

    89/269

    PL/SQL Advanced Techniques - page 8

    p ( Q!ith the *)6S SQL pac+age"

    *)6S SQL is a ver& large and c#$plex pac+age< !ith $an&rules t# (#ll#! and l#ts #( c#de t# !rite"

    Supp#rts all (#ur $eth#ds #( d&na$ic SQL< in particular$eth#d B"

    The #verhead (#r using *)6S SQL has decreasedsigni(icantl& in Oracle8 and again in Oracle8i"

    ?#u sh#uld #nl& use *)6S SQL !hen u cann#t use *S"

    Learning Thr#ugh Exa$ples**L

  • 7/21/2019 Advanced Techniques of PL/SQL

    90/269

    PL/SQL Advanced Techniques - page F

    3reate an index (r#$ !ithin PL/SQL

    *6L ;pdate r#!s in a ta'le

    *6L !ith 'inding ;pdate r#!s using 'ind varia'les

    Queries 6eth#d : and a d&na$ic DME%E clause

    PL/SQL 4ersi#n #( JSELE3T @J Exa$ple #( 6eth#d B

    PL/SQL 3reate a generic calculati#n pr#gra$

    **L !ith *&na$ic SQLPROCEDURE cre% e,in#e

  • 7/21/2019 Advanced Techniques of PL/SQL

    91/269

    PL/SQL Advanced Techniques - page 1

    3reates an index #n an& c#lu$n0s2 in an& ta'le inur sche$a" Open a curs#r< !hich !ill 'e used t# execute the **L

    state$ent" 3#nstruct the **L state$ent as a string" Parse and execute that **L state$ent"

    PROCEDURE cre% e,in#e8in#e ,in IN >ARC2AR:- %.,in IN >ARC2AR:- co$,in IN >ARC2AR:=

    IS cur INTEGER 53 DB0S,S *)OPEN,CURSOR; f#.k INTEGER; DD*,s % e&en >ARC2AR:8: = 53 6CREATE INDE4 6

  • 7/21/2019 Advanced Techniques of PL/SQL

    92/269

    PL/SQL Advanced Techniques - page

    CREATE OR REP*ACE PROCEDURE up#nu&+%$ 8

    co$,in IN >ARC2AR:-en%&e,in IN e&p)en%&e TYPE-+%$,in IN NU0BER=

    IScur P*S,INTEGER 53 DB0S,S *)OPEN,CURSOR;

    f#.k P*S,INTEGER;BEGIN

    DB0S,S *)PARSE 8cur- 6UPDATE e&p SET 6

  • 7/21/2019 Advanced Techniques of PL/SQL

    93/269

    PL/SQL Advanced Techniques - page :

    CREATE OR REP*ACE PROCEDURE up#nu&+%$ 8

    co$,in IN >ARC2AR:-s %r ,in IN DATE- en#,in IN DATE- +%$,in IN NU0BER=IS

    cur P*S,INTEGER 53 DB0S,S *)OPEN,CURSOR; f#.k P*S,INTEGER;BEGIN

    DB0S,S *)PARSE 8cur- 6UPDATE e&p SET 6

  • 7/21/2019 Advanced Techniques of PL/SQL

    94/269

    PL/SQL Advanced Techniques - page B

    6a+e sure SELE3T is!ell (#r$ed0PA%SE2

    6a+e sure SELE3T is!ell (#r$ed0PA%SE2

    )ind an& varia'les0)> * 4A%>A)LE20)> * A%%A?2

    )ind an& varia'les0)> * 4A%>A)LE20)> * A%%A?2

    Kive curs#r structure0*E > E 3OL;6 20*E > E A%%A?2

    Kive curs#r structure0*E > E 3OL;6 20*E > E A%%A?2

    ill 'u((er !ith data0 ET3M %ODS20E E3;TE A * ET3M2

    ill 'u((er !ith data0 ET3M %ODS20E E3;TE A * ET3M2

    %etrieve the data03OL;6 4AL;E2%etrieve the data03OL;6 4AL;E2

    ill curs#r !ith data0E E3;TE2

    ill curs#r !ith data0E E3;TE2

    %elease curs#r $e$#r&03LOSE 3;%SO%2%elease curs#r $e$#r&03LOSE 3;%SO%2

    All#cate curs#r $e$#r&0OPE 3;%SO%2All#cate curs#r $e$#r&0OPE 3;%SO%2

    Queries !ith *&na$ic SQLSh#! e$pl#&ees using a d&na$ic DME%E clause"""

  • 7/21/2019 Advanced Techniques of PL/SQL

    95/269

    PL/SQL Advanced Techniques - page C

    CREATE OR REP*ACE PROCEDURE s(o/e&ps 8/(ere,in IN >ARC2AR: 53 NU**=IS cur INTEGER 53 DB0S,S *)OPEN,CURSOR; rec e&p RO1TYPE; f#.k INTEGER;BEGIN DB0S,S *)PARSE 8cur- 6SE*ECT e&pno- en%&e FRO0 e&p 6 E=;

    DB0S,S *) DEFINE,CO*U0N 8cur- 7- 7=; DB0S,S *) DEFINE,CO*U0N 8cur- :- 6%6- ? =;

    f#.k 53 DB0S,S *)E4ECUTE 8cur=; *OOP E4IT 12EN DB0S,S *) FETC2,RO1S 8cur= 3 ; DB0S,S *) CO*U0N,>A*UE 8cur- 7- rec)e&pno=; DB0S,S *) CO*U0N,>A*UE 8cur- :- rec)en%&e=; DB0S,OUTPUT)PUT,*INE 8TO,C2AR 8rec)e&pno=

  • 7/21/2019 Advanced Techniques of PL/SQL

    96/269

    PL/SQL Advanced Techniques - page H

    BEGIN FOR e%c(9co$u&n9in9 %.$e *OOP %##9co$u&n9 o9se$ec 9$is ; END *OOP;

    DB0S,S *)PARSE 8cur- se$ec ,s ring- DB0S,S *)NATI>E=;

    FOR e%c(9co$u&n9in9 %.$e *OOP DB0S,S *)DEFINE,CO*U0N 8cur- n (,co$- #% % pe=; END *OOP;

    *OOP fe c(9%9ro/;

    FOR e%c(9co$u&n9in9 %.$e *OOP DB0S,S *)CO*U0N,>A*UE 8cur- n (,co$- +%$=; END *OOP; END *OOP;END;

    inta'"sp

    4er& si$pli(ied

    pseud#-c#de

    ;sing E E3;TE A * ET3MFUNCTION e ecu e,%n#,fe c(

    8 i IN INTEGER

  • 7/21/2019 Advanced Techniques of PL/SQL

    97/269

    PL/SQL Advanced Techniques - page 7

    6a+es it eas& t# execute and (etch a single r#! (r#$ a quer&" 4er& si$ilar t# the i$plicit SELE3T curs#r in native PL/SQL< !hich returns asingle r#!< raises O *ATA O; * #r raises the TOO 6A ? %ODS excepti#n"

    >( exact $atch is T%;E< then E E3;TE A * ET3M !ill raise theTOO 6A ? %ODS excepti#n i( $#re than #ne r#! is (etched '& the SELE3T"

    Even i( the excepti#n is raised< the (irst r#! !ill still 'e (etched and availa'le"

    8cursor,in IN INTEGER- e %c ,&% c( IN BOO*EAN DEFAU*T FA*SE= RETURN INTEGER;

    nu&ro/s 53 DB0S,S *)E4ECUTE,AND,FETC2 8cur=;

    nu&ro/s 53 DB0S,S *)E4ECUTE,AND,FETC2 8cur- TRUE=;

    *&na$ic #r$ula Executi#nSupp#se > a$ 'uilding a user inter(ace that all#!s a user t#select a (#r$ula (#r executi#n< and enter the argu$ents"

  • 7/21/2019 Advanced Techniques of PL/SQL

    98/269

    PL/SQL Advanced Techniques - page 8

    select a (#r$ula (#r executi#n< and enter the argu$ents ;sing static PL/SQL< > !#uld have t# $#di(& $& screen ever& ti$e a

    ne! (#r$ula !as added" Dith *)6S SQL< a single (uncti#n !ill d# the tric+"

    FUNCTION # nc%$c 8 oper,in IN >ARC2AR:- n%rgs,in IN INTEGER 53 - %rg7,in IN >ARC2AR: 53 NU**- %rg:,in IN >ARC2AR: 53 NU**- %rg@,in IN >ARC2AR: 53 NU**- %rg ,in IN >ARC2AR: 53 NU**- %rg ,in IN >ARC2AR: 53 NU**- %rg?,in IN >ARC2AR: 53 NU**- %rg ,in IN >ARC2AR: 53 NU**- %rg ,in IN >ARC2AR: 53 NU**- %rg ,in IN >ARC2AR: 53 NU**- %rg7 ,in IN >ARC2AR: 53 NU** = RETURN >ARC2AR:;

    d&ncalc"s( d&ncalc"p+g

    6#re #n *&na$ic PL/SQLBEGIN cur 53 open,%n#,p%rse

  • 7/21/2019 Advanced Techniques of PL/SQL

    99/269

    PL/SQL Advanced Techniques - page

    ;se )> * 4A%>A)LE t# 'ind an& placeh#lders in the string -- even O;Targu$ents !hich are n#t 'eing '#und t# an& values"

    ;se 4A%>A)LE 4AL;E t# extract a value (r#$ an& varia'le u have '#und"

    ?#u $ust have a )EK> -E * ar#und the c#de"

    P#ssi'ilities inherent in d&na$ic PL/SQL are $ind-'#ggling

    p , ,p 86BEGIN ge ,&% ,s%$ 85#ep in- 5s%$ou =; END;6=;

    DB0S,S *)BIND,>ARIAB*E 8cur- #ep in - +,#ep in=; DB0S,S *)BIND,>ARIAB*E 8cur- s%$ou - & ,s%$%r =;

    f#.k 53 DB0S,S *)E4ECUTE 8cur=;

    DB0S,S *)>ARIAB*E,>A*UE 8cur- s%$ou - & ,s%$%r =;END;

    d&nplsql"sqld&nplsql"spd&nplsql"tst

    >ndirect %e(erencing !ith *&n PL/SQLOracle #r$s #((ers supp#rt (#r indirect re(erencing !ith the

    A6E > and 3OP? 'uilt-ins"

  • 7/21/2019 Advanced Techniques of PL/SQL

    100/269

    PL/SQL Advanced Techniques - page 1FF

    A6E > and 3OP? uilt-ins

    PL/SQL d#es n#t supp#rt indirect re(erencing< 'ut u canacc#$plish $uch #( the sa$e thing !ith d&na$ic PL/SQLexecuti#n" Mere is an exa$ple #( a JPL/SQL A6E > J5

    FUNCTION +%$. n%&e 8n& IN >ARC2AR:= RETURN >ARC2AR: IS +,cur P*S,INTEGER 53 DB0S,S *)OPEN,CURSOR; f#.k P*S,INTEGER; re +%$ P*>)#.&% +c:;BEGIN DB0S,S *)PARSE 8+,cur-

    6BEGIN 5+%$ 53 6 ARIAB*E 8+,cur- 6+%$6- 6%6- : =; f#.k 53 DB0S,S *)E4ECUTE 8+,cur=; DB0S,S *)>ARIAB*E,>A*UE 8+,cur- 6+%$6- re +%$=; DB0S,S *)C*OSE,CURSOR 8+,cur=; RETURN re +%$;END;

    d&nvar"p+gd&nvar"tst

    *)6S SQL Status )uilt-insThe pac+age #((ers a set #( $#dules t# return in(#r$ati#n a'#ut the last-#perated curs#r in ur sessi#n"

  • 7/21/2019 Advanced Techniques of PL/SQL

    101/269

    PL/SQL Advanced Techniques - page 1F1

    p 3all these i$$ediatel& a(ter ur usage t# $a+e sure the& re(er t# ur curs#r"

    >S OPE >s the curs#r alread& #pen

    LAST E%%O% POS>T>O %eturns relative c#lu$n p#siti#n in curs#r #( text causing err#r c#nditi#n"

    LAST %OD 3O; T %eturns the cu$ulative c#unt #( r#!s (etched (r#$ the curs#r"

    LAST %OD >* %eturns the %OD>* #( last r#! pr#cessed"

    LAST SQL ; 3T>O 3O*E %eturns SQL (uncti#n c#de #( curs#r"

    D#r+ing !ith LO K 4alues*)6S SQL pr#vides special pr#cedures s# that u canextract values (r#$ a LO K c#lu$n in a ta'le"

  • 7/21/2019 Advanced Techniques of PL/SQL

    102/269

    PL/SQL Advanced Techniques - page 1F

    extract values (r#$ a LO K c#lu$n in a ta le

    *)6S SQL"*E > E 3OL;6 LO K *)6S SQL"3OL;6 4AL;E LO K

    irst< u de(ine the c#lu$n as a LO K K and

    > TO clauses< th#ugh u c#uld !rite variati#ns (#r th#se as !ell" Encapsulate *)6S SQL"PA%SE s# that u include the trace in that

    pr#gra$"

    ;se the Oracle8i inv#+er rights $#del !henever u !ant t#share ur d&na$ic SQL pr#gra$s a$#ng $ultiplesche$as"

    Other!ise that SQL !ill 'e executed under the auth#rit( the #!ner #( the c#de< n#t the inv#+er #( the c#de" e((dsql"sql#penprse"p+g!hichsch"sql

    *S #r *)6S SQL5 Dhich is )est*&na$ic SQL and PL/SQL is ver& use(ul< 'ut *)6S SQL is hard t# use" )#thi$ple$entati#ns !ill still c#$e in hand&"""

    >(< #( c#urse< u have upgraded t# Oracle8i

  • 7/21/2019 Advanced Techniques of PL/SQL

    109/269

    PL/SQL Advanced Techniques - page 1F

    >(< #( c#urse< u have upgraded t# Oracle8i

    6a.#r Advantages #( *S5 Ease #( use

    Per(#r$ance D#r+s !ith all SQL

    datat&pes 0including user-de(ined #'.ect andc#llecti#n t&pes2

    etch int# rec#rds

    Dhen ?#uId ;se *)6S SQL5 6eth#d B *&na$ic SQL

    *ES3%>)E c#lu$ns #( curs#r SQL state$ents larger than : = %ET;% > K int# an arra& %euse #( parsed SQL state$ents )ul+ d&na$ic SQL

    Availa'le (r#$ client-side PL/SQL

    Achieving PL/SQL Excellence

  • 7/21/2019 Advanced Techniques of PL/SQL

    110/269

    PL/SQL Advanced Techniques - page 11F

    OracleAdvanced Queuing

    The high per(#r$ance< as&nchr#n#usnternet< s&ste$s are n#!heavil& reliant #n the a'ilit& #( c#$p#nents t# c#$$unicate

  • 7/21/2019 Advanced Techniques of PL/SQL

    111/269

    PL/SQL Advanced Techniques - page 111

    ( p

    !ith each #ther in a dependa'le< c#nsistent $anner"

    *istri'uti#n3enter

    3#((ee )eansPr#ducer %etailer

    3#nsu$er

    ShippingService

    Applicati#ns %el&ing #n 6essaging

    St#c+ trading s&ste$

  • 7/21/2019 Advanced Techniques of PL/SQL

    112/269

    PL/SQL Advanced Techniques - page 11

    Airline reservati#n s&ste$

    Aucti#n p#rtals

    An& e-c#$$erce applicati#n

    =e& eatures #( Oracle AQLeverage (ull p#!er #( SQL 6essages are st#red in data'ase ta'les

  • 7/21/2019 Advanced Techniques of PL/SQL

    113/269

    PL/SQL Advanced Techniques - page 11:

    *ata'ase high availa'ilit&< scala'ilit& and relia'ilit& all carrver t# queues Str#ng hist#r& and retenti#n )ac+up and rec#ver& 3#$prehensive .#urnaliing

    %ich $essage c#ntent increases use(ulness #( queueing ;se #'.ect t&pes t# de(ine highl& structured pa&l#ads

    e! t# Oracle8i< AQ n#! #((ers a pu'lish/su'scri'e st&le #($essaging 'et!een applicati#ns" %ule-'ased su'scri'ers< $essage pr#pagati#n< the listen (eature and

    n#ti(icati#n capa'ilities"

    /+e+e Monitor rocess

    AQ architectural #vervie!

  • 7/21/2019 Advanced Techniques of PL/SQL

    114/269

    PL/SQL Advanced Techniques - page 11B

    /+e+e Monitor -rocess

    /+e+e

    Prod+cers

    En ueuedmessages

    N3#nsu$ers

    Message 4

    Message 3

    Message 2

    Message1

    /+e+e table

    e ueuedmessages

    "essages include bothcontrol information and#payload$ %content&

    Oracle AQ Mighlights>n 8"F< AQ supp#rts5 6ultiple queues

  • 7/21/2019 Advanced Techniques of PL/SQL

    115/269

    PL/SQL Advanced Techniques - page 11C

    p q

    %esetting #rder and pri#rit& #( queued ite$s Queue $anage$ent using #nl& SQL PL/SQL 6ultiple $essage recipients Pr#pagati#n #( queue t# re$#te servers

    Oracle8 i adds5 %ules-'ased pu'lish su'scri'e Listening #n $ultiple queues Easier $#nit#ring ative 9ava inter(ace

    AQ 3#$p#nentsThe *)6S AQ pac+age #((ers enqueue anddequeue capa'ilities

  • 7/21/2019 Advanced Techniques of PL/SQL

    116/269

    PL/SQL Advanced Techniques - page 11H

    q p

    The *)6S AQA*6 pac+age pr#videsad$inistrative (uncti#nalit& t# $anage queues andqueue ta'les"

    ;nderl&ing data'ase ta'les and vie!sThe queue $#nit#r 0'ac+gr#und pr#cess2 Set Y #( pr#cesses !ith the AQ T6 P%O3ESSES

    initiali,ati#n para$eter"

    ,B5S_A@A,5 MighlightsCREATE_@0E0E_TABLE Assigns na$e< pa&l#ad t&pe< st#rage clause< s#rt

    c#lu$n< !hether $ultiple c#nsu$ers

  • 7/21/2019 Advanced Techniques of PL/SQL

    117/269

    PL/SQL Advanced Techniques - page 117

    ,ROP_@0E0E_TABLE *r#ps ta'le i( all queues in the ta'le have 'eenst#pped

    CREATE_@0E0E Ass#ciates queue ta'le !ith queue assigns retr&and retenti#n pr#perties t# queue

    ,ROP_@0E0E *r#ps a st#pped queueSTART_@0E0E 3an als# turn #n/#(( enqueue and dequeue

    #perati#nsSTOP_@0E0E St#ps queue< #pti#nall& !aiting (#r #utstanding

    transacti#ns

    A,,_S0BSCRIBER Adds an Nagent as a su'scri'er

    3reating Queue Ta'les and QueuesCREATE TYPE &ess%ge, pe AS OBMECT 8 i $e >ARC2AR:8@ =-

    >ARC2AR 8*e(ine the Jpa&l#adJ

  • 7/21/2019 Advanced Techniques of PL/SQL

    118/269

    PL/SQL Advanced Techniques - page 118

    BEGIN

    DB0S,A AD0)CREATE, UEUE,TAB*E 8Xueue, %.$e 3H 6&sg6-

    Xueue,p% $o%#, pe 3H 6&ess%ge, pe6=;

    DB0S,A AD0)CREATE, UEUE 8Xueue,n%&e 3H 6&sgXueue6- Xueue, %.$e 3H 6&sg6=;

    DB0S,A AD0)START, UEUE 8Xueue,n%&e 3H 6&sgXueue6=;

    END;

    e >ARC2AR:8: ==;!

    3reate the queue ta'le

    *e(ine a queue in thequeue ta'le

    Start the queueaq"p+g

    The J#perati#nal pac+ageJ5 *)6S AQ*)6S AQ is deceptivel& si$ple" Onl& t!# pr#cedures< 'ut l#ts #( c#$plexit& 'uried inside the

    para$eters #( these pr#cedures"

  • 7/21/2019 Advanced Techniques of PL/SQL

    119/269

    PL/SQL Advanced Techniques - page 11

    para$eters #( these pr#cedures

    E Q;E;E puts a $essage int# a speci(ied queue< and returnsa %AD $essage handle

    *EQ;E;E extracts a $essage (r#$ a speci(ied queue

    Para$eters c#ntr#l $essage pr#perties such as5 4isi'ilit& 0 O+_CO55IT #r I55E,IATE 2 Pri#rit& *ela& Expirati#n L#c+ing 'ehavi#r

    a 3s l

    Si$ple Enqueue Exa$pleDEC*ARE Xueueop s DB0S,A )EN UEUE,OPTIONS,T;

    & g DB0S A )0ESSAGE PROPERTIES T*eclare rec#rds t#

    h#ld i#

  • 7/21/2019 Advanced Techniques of PL/SQL

    120/269

    PL/SQL Advanced Techniques - page 1 F

    &sgprops DB0S,A )0ESSAGE,PROPERTIES,T; &sgi# %X)&sgi#, pe; & ,&sg &ess%ge, pe;BEGIN & ,&sg 53

    &ess%ge, pe 8 6Firs EnXueue6-

    60% (ere .e &%n &ore)))6=;

    DB0S,A )EN UEUE 8 6&sgXueue6- Xueueop s- &sgprops-

    & ,&sg- &sgi#=;END;

    h#ld vari#us enqueueand $sg pr#perties"

    Set up the pa&l#ad!ith an #'.ectc#nstruct#r"

    Place the $essage #n thespeci(ied queue and get a

    $sg >* in return"aqenq@"@

    6#re >nteresting Enqueue Exa$ple

    DEC*ARE ))) S%&e se up %s pre+ious p%ge )))BEGIN

  • 7/21/2019 Advanced Techniques of PL/SQL

    121/269

    PL/SQL Advanced Techniques - page 1 1

    & ,&sg 53 &ess%ge, pe 8 6Firs EnXueue6- 60% (ere .e &%n &ore)))6=;

    &sgprops)#e$% 53 @ " ? " ? " : ;

    DB0S,A )EN UEUE 86&sgXueue6-Xueueop s- &sgprops- & ,&sg- &sgi#7=;

    & ,&sg 53 &ess%ge, pe 8 6Secon# EnXueue6-

    6An# (is one goes firs )))6=;

    Xueueop s)seXuence,#e+i% ion 53 DB0S,A )BEFORE; Xueueop s)re$% i+e,&sgi# 53 &sgi#7;

    DB0S,A )EN UEUE 8 6&sgXueue6-

    Xueueop s- &sgprops- & ,&sg- &sgi#:=;END;

    Speci(& a dela&'e(#re the pa&l#ad

    is availa'le"

    6#di(& the dequeue

    sequence '& changing thedeviati#n (ield and relative

    $sg >*"

    *equeue Exa$pleDEC*ARE Xueueop s DB0S,A )DE UEUE,OPTIONS,T; &sgprops DB0S,A )0ESSAGE,PROPERTIES,T;

    & i % )& i*eclare rec#rds t#

    h#ld vari#us dequeue

  • 7/21/2019 Advanced Techniques of PL/SQL

    122/269

    PL/SQL Advanced Techniques - page 1

    &sgi# %X)&sgi#, pe;!" #efine# in %X)pkg "!

    & ,&sg &ess%ge, pe;

    PROCEDURE ge &sg 8&o#e,in IN INTEGER= IS BEGIN Xueueop s)#eXueue,&o#e 53 &o#e,in;

    DB0S,A )DE UEUE 8 6&sgXueue6- Xueueop s-

    &sgprops- & ,&sg- &sgi#=; END;BEGIN ge &sg 8DB0S,A )BRO1SE=; ge &sg 8DB0S,A )RE0O>E=; ge &sg 8DB0S,A )RE0O>E=;END;

    h#ld vari#us dequeueand $sg pr#perties"

    *equeue #perati#n

    is#lated in l#cal$#dule"

    *e$#nstrates destructiveand n#n-destructivedequeuing"

    aqdeq@"@

    Pri#riti,ed Pa&l#ads ?#u can assign pri#rities t# individual pa&l#ads and then dequeueacc#rding t# th#se pri#rities" The l#!er the nu$eric pri#rit& value< the higher the pri#rit&"

  • 7/21/2019 Advanced Techniques of PL/SQL

    123/269

    PL/SQL Advanced Techniques - page 1 :

    p g p

    A stac+ i$ple$entati#n using AQ de$#nstrates this !ell"

    PROCEDURE pus( 8i e& IN >ARC2AR:= IS Xueueop s DB0S,A )EN UEUE,OPTIONS,T; &sgprops DB0S,A )0ESSAGE,PROPERTIES,T;

    &sgi# %X)&sgi#, pe; i e&,o. %Xs k,o. pe;BEGIN i e&,o. 53 %Xs k,o. pe 8i e&=; &sgprops)priori 53 g,priori ; Xueueop s)+isi.i$i 53 DB0S,A )I00EDIATE; g,priori 53 g,priori 9 7;

    DB0S,A )EN UEUE 8 c,Xueue- Xueueop s- &sgprops- i e&,o. - &sgi#=;END;

    aqst+"p+gaqst+ "p+g

    pri#rit&"@

    *e(ining 6essage Su'scri'ers ?#u can speci(& that a $essage is t# 'e enqueued (#r a list #( su'scri'ers"

    The $essage is then n#t re$#ved (r#$ the queue until all

  • 7/21/2019 Advanced Techniques of PL/SQL

    124/269

    PL/SQL Advanced Techniques - page 1 B

    The $essage is then n#t re$#ved (r#$ the queue until allsu'scri'ers have dequeued the $essage"

    Steps t# !#r+ing !ith a su'scri'er list5 1" The queue ta'le $ust 'e de(ined t# supp#rt $ultiple su'scri'ers

    #r c#nsu$ers"

    " Add su'scri'ers (#r the queue"

    BEGIN DB0S,A AD0)CREATE, UEUE,TAB*E 8 Xueue, %.$e 3H 6&% or,X %.$e6- Xueue,p% $o%#, pe 3H 6s u#en ,&% or, 6- &u$ ip$e,consu&ers 3H TRUE=;

    DB0S,A AD0)ADD,SUBSCRIBER 8 c,Xueue- SYS)A ,AGENT 8n%&e,in- NU**- NU**==; aq$ult@"@

    Oracle AQ - Su$$ar&4er& p#!er(ul and (lexi'le architecture" 6uch $#re r#'ust that *)6S P>PE"

  • 7/21/2019 Advanced Techniques of PL/SQL

    125/269

    PL/SQL Advanced Techniques - page 1 C

    Signi(icant enhance$ents in Oracle8i< supp#rting apu'lish-su'scri'e $#del< i$pr#ved securit&< L>STEcapa'ilit&"

    3#nsidered '& Oracle t# 'e a c#re c#$p#nent #( its#verall s#luti#n" 3rucial (#r Oracle t# have a $essage-#riented

    $iddle!are in #rder t# #((er an all-Oracle s#luti#n" Sh#uld 'e str#ngl& supp#rted Jd#!n the r#adJ"

    Achieving PL/SQL Excellence

  • 7/21/2019 Advanced Techniques of PL/SQL

    126/269

    PL/SQL Advanced Techniques - page 1 H

    6anaging

    Large O'.ects!ith *)6S LO)

    LO) Ter$sLO) Large O).ect5 a categ#r& #( datat&pe all#!ingst#rage #( Nunstructured data up t# B giga'&tes

  • 7/21/2019 Advanced Techniques of PL/SQL

    127/269

    PL/SQL Advanced Techniques - page 1 7

    LO) datat&pe can 'e5 3#lu$n in ta'le Attri'ute in #'.ect t&pe Ele$ent in nested ta'le

    P#ssi'le applicati#ns include #((ice d#cu$ents< >$ages

  • 7/21/2019 Advanced Techniques of PL/SQL

    128/269

    PL/SQL Advanced Techniques - page 1 8

    3LO)5 single-'&te (ixed-!idthcharacter data

    3LO)5 $ulti-'&te (ixed-!idthcharacter data 0#r var&ing !idthin 2

    #perating s&ste$ (ile

    =e& pr#gra$$ing di((erences5 >nternal LO)s participate in transacti#ns external d# n#t

    External LO)s are read-#nl& (r#$ !ithin Oracle

    Te$p#rar& LO)s

    >nternal LO)s that d# n#tparticipate in transacti#ns%E3TO%? na$espace is gl#'al

    S&ntax5

    Exa$ple5 C EATE DI ECTO Y ;en a ta'le"""

    CREATE TABLE eb_9'aphic% (

  • 7/21/2019 Advanced Techniques of PL/SQL

    136/269

    PL/SQL Advanced Techniques - page 1:H

    i$a9e_id I+TE ER) i$a9e BFILE!"

    >n an #'.ect t&pe"""

    CREATE TYPE B&%i e%%_ca'd_t AS OB ECT ( a$e +a$e_t) add'e%%e% Add'e%%_tab_t) ph/ e% Ph/ e_tab_t) %ca ed_ca'd_i$a9e BFILE!"

    The 'uilt-in ) >LE A6E (uncti#n%eturns value #( datat&pe BFILE" Spec5

    F2'CTIO' BFILE'A4E# directory_exprn 0 file_exprn &

  • 7/21/2019 Advanced Techniques of PL/SQL

    137/269

    PL/SQL Advanced Techniques - page 1:7

    Exa$ple5

    #tes5

    # aut#$atic (ile chec+ing #r s&nchr#ni,ati#n # participati#n in transacti#ns

    ,ECLARE pict&'e BFILE D BFILE+A5E( EB_PI6 ) p'/dic/ =9i# !"BE I+ I+SERT I+TO eb_9'aphic% VAL0ES (G GM) pict&'e!"E+,"

    ET2 ' BFILE)

    L#ading ile int# *ata'ase )LO)

    ,ECLARE

    CREATE TABLE eb_9'aphic_bl/b% ( i$a9e_id I+TE ER) i$a9e BLOB!"

  • 7/21/2019 Advanced Techniques of PL/SQL

    138/269

    PL/SQL Advanced Techniques - page 1:8

    pic_#ile BFILE D BFILE+A5E( EB_PI6 ) p'/dic/ =9i# !" pic_bl/b_l/c BLOB D E5PTY_BLOB(!"

    BE I+ I+SERT I+TO eb_9'aphic_bl/b% VAL0ES (G) pic_bl/b_l/c! RET0R+I+ i$a9e I+TO pic_bl/b_l/c"

    ,B5S_LOB=FILEOPE+(pic_#ile) ,B5S_LOB=FILE_REA,O+LY!"

    ,B5S_LOB=LOA,FRO5FILE(de%t_l/b D pic_bl/b_l/c) %'c_l/b D pic_#ile) a$/& t D ,B5S_LOB= ETLE+ TH(pic_#ile!!"

    ,B5S_LOB=FILECLOSE(pic_#ile!"E+,"

    l#ad'l#'"sql

    e! *)6S LO) AP>s in 8 i Supp#rt (#r te$p#rar& LO)s # l#gging #r r#ll'ac+ (aster

  • 7/21/2019 Advanced Techniques of PL/SQL

    139/269

    PL/SQL Advanced Techniques - page 1:

    Li(espan5 sessi#n< call< #r transacti#nA'ilit& t# retrieve LO) Nchun+ si,e All#!s pr#gra$$er t# tune %EA*s and D%>TEs

    #r all internal LO)s, OPE+ CLOSE all#! c#ntr#l #ver ti$ing that can $ean less>/O

    Trigger will not 2ire until LOS3 Inde5es will not u%date until LOS3

    , ISOPE+

    HOT

    Large O'.ect - Su$$ar&LO) supp#rt in Oracle is n#! signi(icantl&i$pr#ved"

    $ l d h

  • 7/21/2019 Advanced Techniques of PL/SQL

    140/269

    PL/SQL Advanced Techniques - page 1BF

    COLD

    3#$plete supp#rt in PL/SQL 0and #ther AP>s2 6uch 'etter per(#r$ance and c#ntr#l than !ith

    LO Ks ;sa'le in #'.ect t&pes

    S#$e !ea+nesses""" LO) l#cat#r 'ehavi#r slightl& a'struse >na'ilit& t# $#di(& ) >LEs directl& (r#$ !ithin

    PL/SQL"

    Achieving PL/SQL Excellence

  • 7/21/2019 Advanced Techniques of PL/SQL

    141/269

    PL/SQL Advanced Techniques - page 1B1

    Leveraging 9ava>nside PL/SQL

    Overvie! #( 9ava inter#pera'ilit&9ava inside #r #utside 8 i server can call PL/SQL Standard 9*)3 and SQL9 calls !ith Oracle extensi#ns Sa$e 9ava #n client< $id-tier< #r server

  • 7/21/2019 Advanced Techniques of PL/SQL

    142/269

    PL/SQL Advanced Techniques - page 1B

    ot covered in this seminar

    PL/SQL can call 9ava inside 8 i server 3#$$and-line t##ls l#ad 9ava classes **L extensi#ns pu'lish 9ava classes Driting st#red pr#cedures< (uncti#ns< triggers in 9ava *istinct 9ava PL/SQL na$espaces

    )ut (irst"""a )%>E intr#ducti#n t# 9ava"""

    Questi#n 15 Dhat is 9ava3#uld it 'e""" The end #( pr#gra$$ing hist#r& as !e +n#! it The easiest< (astest< slic+est piece #( s#(t!are ever designed '&

    h $ ' i g

  • 7/21/2019 Advanced Techniques of PL/SQL

    143/269

    PL/SQL Advanced Techniques - page 1B:

    hu$an 'eings 9ust the latest in a series #( Jsilver 'ulletsJ pr#$#ted '& s#(t!are

    vend#rs in #rder t# pr#p up quarterl& sales The (irst and #nl& success(ul O-O language #ne #( the a'#ve

    De d#nIt reall& need t# ta+e a v#te" De .ust need t# +eep a (ir$ grip #n c#$$#n sense and sta& (#cused

    #n delivering s#luti#ns"

    Questi#n 5 Dill 9ava %eplace PL/SQLDhile that scenari# is certainl& p#ssi'le< it is ver& unli+el&and t#tall& unthin+a'le (#r &ears t# c#$e"

    PL/SQL !ill till ' 5

  • 7/21/2019 Advanced Techniques of PL/SQL

    144/269

    PL/SQL Advanced Techniques - page 1BB

    PL/SQL !ill still 'e5 aster and $#re pr#ductive than 9ava (#r data'ase #perati#ns" A language in !hich hundreds #( th#usands #( devel#pers are

    trained" ;'iquit#us in th#usands #( pr#ducti#n applicati#ns and $illi#ns #(

    lines #( c#de" Supp#rted and i$pr#ved '& Oracle -- and ver& aggressivel&< t# '##t"

    S#$e >$p#rtant Things t# %e$e$'er 9ava is a case sensitive language""" string is de(initel& n#t the sa$e as String"

    Ever&thing is a class 0#r an #' ect instantiated (r#$ a

  • 7/21/2019 Advanced Techniques of PL/SQL

    145/269

    PL/SQL Advanced Techniques - page 1BC

    Ever&thing is a class 0#r an #'.ect instantiated (r#$ aclass2""" )e(#re u can call a 0n#n-static2 class $eth#d< u have t#

    instantiate an #'.ect (r#$ that class" Dell< ever&thing excepti#n the pri$itive datat&pes"

    ?#u d#nIt have t# +n#! h#! t# d# ever&thing !ith 9ava t#get l#ts #( value #ut #( it""" *#nIt get #ver!hel$ed '& all the classes and all the strange quir+s"

    )uilding a 9ava 3lass

    p&blic cla%% Hell/ ;

    p&blic %tatic /id $ai (St'i 9:< a'9%! ;S % $ /& 'i l (NH ll/ /'ldJN!"

    LetIs start (r#$ the ver& 'eginning"""

  • 7/21/2019 Advanced Techniques of PL/SQL

    146/269

    PL/SQL Advanced Techniques - page 1BH

    p&blic %tatic -/id $ai (St i 9:< a 9%! ; Sy%te$=/&t=p'i tl (NHell/ /'ldJN!" ??

    CREATE OR REPLACE PROCE,0RE hell/ ISBE I+ ,B5S_O0TP0T=P0T_LI+E ( Hell/ /'ldJ !"E+,"

    Oh< '& the !a&5the PL/SQL

    versi#n

    # $e$'ers< n# $eth#ds< except the JspecialJ $ain $eth#d" ;sed t# test #r run stand-al#ne a classt $eans that i( urclass c#ntains a $eth#d !ith this header5

    p&blic %tatic /id $ai (St'i 9:< a'9%!

  • 7/21/2019 Advanced Techniques of PL/SQL

    148/269

    PL/SQL Advanced Techniques - page 1B8

    p&blic %tatic -/id $ai (St'i 9:< a'9%!

    d a-a a-a Hell/

    d a-a a-a Hell/ $/$

    then u can JrunJ the $ain $eth#d !ith the .avac#$$and5

    ?#u can als# pass #ne #r $#re argu$ents #n thec#$$and line5

    Mell# ".ava

    ;sing a 3lassThe $ain $eth#d is hand& (#r pr#viding a 'uilt-in test$echanis$ #( ur class"

    ;suall&< h#!ever< u !ill use a class '& instantiating$ $

  • 7/21/2019 Advanced Techniques of PL/SQL

    149/269

    PL/SQL Advanced Techniques - page 1B

    ;suall&< h#!ever< u !ill use a class & instantiating#'.ects (r#$ the class and then inv#+ing class $eth#ds #nthe #'.ect"

    LetIs 'uild a per(#r$ance anal&,er class t# expl#re theseideas"

    3#$pare Per(#r$ance #( 6eth#ds;se System>c3**e+tTime4illis t# calculate elapsed ti$e t#nearest th#usandth #( a sec#nd"

    cla%% T$' ;

  • 7/21/2019 Advanced Techniques of PL/SQL

    150/269

    PL/SQL Advanced Techniques - page 1CF

    cla%% T$ ;p'i-ate l/ 9 %ta't D "

    p&blic -/id capt&'e (! ; %ta't D Sy%te$=c&''e tTi$e5illi%(!" ?

    p&blic -/id %h/ Elap%ed (! ; p=l (NElap%ed ti$e N)

    Sy%te$=c&''e tTi$e5illi%(! 8 %ta't!" ?

    p&blic l/ 9 elap%ed (! ; 'et&' (Sy%te$=c&''e tTi$e5illi%(! 8 %ta't!" ?

    p&blic -/id %h/ Elap%ed (St'i 9 c/ te t! ; p=l (NElap%ed ti$e #/' N c/ te t) elap%ed(!!" ??

    p".avaT$r".ava

    >n ile".ava

    T&pes #( 3lassesThere are several di((erent +inds #( classes in 9ava< 'esidesthe JregularJ +ind !e .ust sa!"""

  • 7/21/2019 Advanced Techniques of PL/SQL

    151/269

    PL/SQL Advanced Techniques - page 1C1

    A'stract class The class can c#ntain $e$'ers and $eth#ds< 'ut at least #ne

    $eth#d re$ains uni$ple$ented"

    >nter(ace class The class #nl& c#ntains uni$ple$ented $eth#ds"

    >nner class 3lasses that are de(ined inside #ther classes"

    And #! (#r S#$e Scar& )u,,!#rds>nheritance Su'classes inherit $eth#ds and varia'les (r#$ extended superclasses"

    P#l&$#rphis$ An #'.ect #( a class can have J$ultipleJ (#r$s< either as its #!n class #r as

  • 7/21/2019 Advanced Techniques of PL/SQL

    152/269

    PL/SQL Advanced Techniques - page 1C

    ( $ p ( $an& superclass"

    *&na$ic p#l&$#rphis$5 (#r$ is deter$ined at run-ti$e" Static p#l&$#rphis$5 (#r$ is ch#sen at c#$pile-ti$e 0PL/SQL #verl#ading2"

    $ ,lassierarch!

    Pers#n".ava

    Pers#n

    3iti,en

    3#rp#rati#n

    Dar 3ri$inal

    E$pl#&ee

    supert&pe/J!iderJ su't&pe/Jnarr#!erJ

    M#url& D#r+er

    SalariedD#r+er

    6anage$ent

    #n-6anage$ent

    Language )asics3#$$ents Si 9le li e c/$$e t

    4 Bl/c* c/$$e t 4

    44 /c&$e tati/ c/$$e t 4

  • 7/21/2019 Advanced Techniques of PL/SQL

    153/269

    PL/SQL Advanced Techniques - page 1C:

    Pri$itive datat&pes S# > lied these are n#t #'.ects

    instantiated (r#$ classes"

    44 ,/c&$e tati/ c/$$e t 4

    b//leacha'byte%h/'t

    i tl/ 9#l/atd/&ble

    Strings A String #'.ect is a read-#nl& i( u

    assign a ne! value t# it< u areactuall& all#cating a ne! #'.ect"

    3anIt d# a direct c#$paris#n"

    St'i 9 $y+a$e"$y+a$e D NSte-e N"$y+a$e D NFe&e'%tei N"

    i# ($y+a$e=e7&al%(Y/&'+a$e!!

    #/& dFa$ily(!"

    Driting L##ps in 9avaThree +inds #( l##ps5 !hile

  • 7/21/2019 Advanced Techniques of PL/SQL

    154/269

    PL/SQL Advanced Techniques - page 1CB

    d/ ; l/%t%aSt#? hile (e p'e%%i/ !"

    hile (e p'e%%i/ ! ; l/%t%aSt#

    ?

    #/ (i itialiQe e p e%%i/ %tep! ; l/t%aSt#?

    #/' (i d i d D "i d a'9%=le 9th"i d !

    Sy%te$=/&t=p'i tl (a'9%:i d

    Exa$ples5

    Passing Para$etersS#$e i$p#rtant di((erences (r#$ PL/SQL""" # de(ault values (#r argu$ents u $ust suppl& a value (#r each

    para$eter"

    Onl& p#siti#nal n#tati#n is supp#rted"

  • 7/21/2019 Advanced Techniques of PL/SQL

    155/269

    PL/SQL Advanced Techniques - page 1CC

    p pp >( a $eth#d has n# argu$ents< u still include the #pen and cl#se

    parentheses"

    cla%% P&te'Li 9/ ; p'i-ate St'i 9 $ a$e" p&blic P&te'Li 9/ (St'i 9 a$e! ; $ a$e D a$e" ? p&blic St'i 9 a$e (! ; 'et&' $ a$e" ??cla%% L/t%ALi 9/% ; p&blic %tatic -/id $ai (St'i 9 a'9%:

    Excepti#n Mandling in 9ava4er& si$ilar t# PL/SQL u Jthr#!J and JcatchJ excepti#ns*E

    "+oad int# server using Nl#ad.ava c#$$and-linet##l

  • 7/21/2019 Advanced Techniques of PL/SQL

    161/269

    PL/SQL Advanced Techniques - page 1H1

    :" 1ublish PL/SQL c#ver using AS LA+ 0A EAVA=== rather than BE I+===E+,

    B"2rant privileges as desiredC"*all (r#$ PL/SQL 0#r SQL2 as i( calling PL/SQL

    3reate 9ava class0es2

    t#String $eth#dt#$ ti ll& d '&

    cla%% C/'p/'ati/ e te d% Pe'%/ ; l/ 9 lay/##%" l/ 9 CEOC/$pe %ati/ "

    p&blic C/'p/'ati/ ( St'i 9 P a$e) l/ 9 Play/##%) l/ 9 Pce/C/$p! ;

    a$e D P a$e" lay/##% D Play/##%"CEOC/$pe %ati/ D Pce/C/$p"

    #tes #n 9avaclasses

  • 7/21/2019 Advanced Techniques of PL/SQL

    162/269

    PL/SQL Advanced Techniques - page 1H

    aut#$aticall& used '&S&ste$"#ut"println

    $ain $eth#d is used t#test the class"

    Entr& p#ints $ust 'ep&blic %tatic in $#stcases

    3lasses $a& call #therclasses

    Av#id K;> calls

    CEOC/$pe %ati/ D Pce/C/$p ?

    p&blic St'i 9 t/St'i 9 (! ; 'et&' a$e

    N i% a t'a % ati/ al e tity ith N lay/##% N laid8/## e$pl/yee%) payi 9 it%N

    N Chie# E ec&ti-e O##ice' N CEOC/$pe %ati/ " ?

    p&blic %tatic -/id $ai (St'i 9:< a'9%! ; A -e'y %ca'y c/$pa y C/'p/'ati/ The l/bal5/ %te' D

    e C/'p/'ati/ ( N+/'th'&p8F/'d85attel8Yah//8ATTN)

    M ) M !"

    Sy%te$=/&t=p'i tl (The l/bal5/ %te'!" ?? pers#n".ava

    ;pl#ad using Nl#ad.ava utilit&7racle ) i ser8er

    load=a8a

    a8aclass

    a8areso+rce

    3class

  • 7/21/2019 Advanced Techniques of PL/SQL

    163/269

    PL/SQL Advanced Techniques - page 1H:

    Exa$ple5

    l#ad.ava #pti#ns 0a''reviated2-#ci8 l#ad.ava !ill c#nnect using O3> driver

    -res#lve %es#lves external class re(erences atc#$pile ti$e-res#lver 0sh#!n later2 Search path li+e 3LASSPATM

    l/ad a-a 8&%e' %c/tt ti9e' 8/ci.8'e%/l-e datac'a#t bill Hell/=cla%%

    a8aso+rce

    Pu'lishExa$ple 0t#p-levelcall spec2

    CREATE OR REPLACE F0+CTIO+ hell/_e$p(e$p /_i I+ +05BER!

    RET0R+ VARCHAR2

    AS LA+ 0A E AVA +A5E datac'a#t=bill=Hell/=E$p(i t!

  • 7/21/2019 Advanced Techniques of PL/SQL

    164/269

    PL/SQL Advanced Techniques - page 1HB

    S&ntax 0si$pli(ied2

    'et&' a-a=la 9=St'i 9 "

    CREATE : OR REPLACE < ; PROCE,0RE > F0+CTIO+ ? < name > : RET0R+ < s ltype > ! C0RRE+T_0SER ? P2T_LI'E# hell/_e$p( K ! &)E'D)

  • 7/21/2019 Advanced Techniques of PL/SQL

    165/269

    PL/SQL Advanced Techniques - page 1HC

    6eth#d 5 ;se 8 i SQL 3ALL state$ent (#r exa$ple< (r#$SQL@Plus5

    3ALL av#ids #verhead #( SELECT fn FRO5 ,0AL

    VA IABLE the+ame VA C!A "#6"&CALL hell/_e$p( K ! I+TO the a$e"P I'T @the+ame

    .sp"sql

    Pu'lishing -- $#re c#nceptsShape $apping

    9ava $eth#ds declared Nv#id 'ec#$e PL/SQL procedures

    Signature $is$atches detected #nl& at runti$e

  • 7/21/2019 Advanced Techniques of PL/SQL

    166/269

    PL/SQL Advanced Techniques - page 1HH

    T&pe $apping 0t&pical2 .ava"lang"String 4A%3MA% .ava"sql"Ti$esta$p *ATE .ava"$ath")ig*eci$al ;6)E%#racle"sql"ST%;3T user-de(ined #'.ect t&pe

    'named type( user-de(ined #'.ect t&pe#racle"sql"%E #'.ect %E erence

    #racle"sql"A%%A? user-de(ined c#llecti#n t&pe

    Pu'lish in PL/SQL Pac+age Spec*esignate 9ava $#dule in PL/SQL pac+age spec"""

    C EATE O EPLACE PACKAGE hell-_p .AS

  • 7/21/2019 Advanced Techniques of PL/SQL

    167/269

    PL/SQL Advanced Techniques - page 1H7

    F2'CTIO' hi_emp #emp+-_i+ I' '24BE & ET2 ' VA C!A " AS LA+ 0A E AVA +A5E datac'a#t=&til=Hell/=E$p(i t!

    'et&' a-a=la 9=St'i 9 "E'D)

    >&o -ac(a:e bod! re +ired in this case?

    Pu'lish as $#dule in pac+age '#d&"""#r in pac+age '#d&

    C EATE O EPLACE PACKAGE hell-_p ."AS F2'CTIO' hi_emp #emp+-_i+ I' '24BE &

    ET2 ' VA C!A ")

  • 7/21/2019 Advanced Techniques of PL/SQL

    168/269

    PL/SQL Advanced Techniques - page 1H8

    ET2 VA C!A )E'D)

    C EATE O EPLACE PACKAGE BODY hell-_p ."AS F2'CTIO' hi_emp #emp+-_i+ I' '24BE & ET2 ' VA C!A " IS LA+ 0A E AVA +A5E datac'a#t=&til=Hell/=E$p(i t! 'et&' a-a=la 9=St'i 9 "E'D)

    Pu'lish as #'.ect t&pe $eth#dEither inspec5

    C EATE O EPLACE TYPE f--_t AS OB:ECT #

  • 7/21/2019 Advanced Techniques of PL/SQL

    169/269

    PL/SQL Advanced Techniques - page 1H

    C EATE O EPLACE TYPE f--_t AS OB:ECT #

  • 7/21/2019 Advanced Techniques of PL/SQL

    170/269

    PL/SQL Advanced Techniques - page 17F

    ALTER AVA c/$pile% a-a %/&'ce) 'e%/l-e% a-acla%% 'e#e'e ce%=

    ,ROP AVA d r#ps a na$ed 9ava li'rar& unit

    Several r#les availa'le (#r 9ava #perati#ns5 9A4A;SE%P%>4< 9A4AS?SP%>4 0needed (#r (ile >O #perati#ns2 < 9A4A>*P%>4< 9A4A*E);KP%>4

    ?#u can als# grant speci(ic privileges"

    Passing Oracle8 #'.ects

    7racle) i ser8er

    O'.ectin O %

    a8a a--lication

    a8a a--lication

    9ava#' ect

  • 7/21/2019 Advanced Techniques of PL/SQL

    171/269

    PL/SQL Advanced Techniques - page 171

    Marder than uGd thin+

    Three di((erent inter(acing techniques, /'acle=%7l=STR0CT

    , /'acle=%7l=C&%t/$,at&$, /'acle= dbc2=S@L,ata

    in O-%ta'le

    # .ect

    9Pu'lisher utilit&Dhat d#es it d#

    Kenerates 9ava thatencapsulates t&pe and

    %E

    Th & #( $ i

    @atabase ser8er

    @e

  • 7/21/2019 Advanced Techniques of PL/SQL

    172/269

    PL/SQL Advanced Techniques - page 17

    Three t&pes #( $appingsupp#rted (#r $eth#ds

    NOracle $apping

    N9*)3 $apping NO'.ect 9*)3 $apping

    data dictionar!

    P+blisher

    Kenerated 9ava(ile0s2 (#r use in9ava pr#gra$s

    Passing #'.ect using 9Pu'-generated 9avaA(ter generating and upl#ading classes !ith 9Pu'$p#rt the ile class t# res#lve re(erence" >nstantiate a ile #'.ect (#r the speci(ied na$e" 3all the $eth#d #( ch#ice against that #'.ect and return

    the value"

    9 ile ".ava

    )uild a Pac+age #ver 9ava 6eth#dLetIs put it in a pac+age !e !ill certainl& !ant t#add $#re (uncti#nalit& #ver ti$e" > translate the 9ava l#ng t# a PL/SQL ;6)E%"

    CREATE OR REPLACE PACUA E #ile

  • 7/21/2019 Advanced Techniques of PL/SQL

    177/269

    PL/SQL Advanced Techniques - page 177

    CREATE OR REPLACE PACUA E #ileIS

    F0+CTIO+ le 9th (#ile I+ VARCHAR2! RET0R+ +05BER"E+,"

    CREATE OR REPLACE PACUA E BO,Y #ileIS F0+CTIO+ le 9th (#ile I+ VARCHAR2! RET0R+ +05BER AS LA+ 0A E AVA +A5E File=le 9th ( a-a=la 9=St'i 9! 'et&' l/ 9 "E+,"

    x(ile "p+g

    6apping the )##lean *atat&pe)#th 9ava and PL/SQL supp#rt a native )##leandatat&pe< s# uId expect s$##th sailing" #t s#

    T# pass a )##lean 'ac+ (r#$ 9ava t# PL/SQL< u!ill need t# ta+e these steps5

  • 7/21/2019 Advanced Techniques of PL/SQL

    178/269

    PL/SQL Advanced Techniques - page 178

    p 1" 3#nvert the 9ava '##lean t# a String #r nu$'er and

    return that value"

    " Drite a JhiddenJ PL/SQL !rapper (uncti#n that returnsthe string #r nu$'er" :" Drite a Jpu'licJ PL/SQL !rapper (uncti#n t# c#nvert

    that nu$'er t# a true PL/SQL )##lean"

    Translate )##lean t# u$'er Accept the na$e #( a (ile and return its length"

    i$p/'t a-a=i/=File"

    p&blic cla%% File3 ; p&blic %tatic i t ca Read (St'i 9 #ile+a$e! ;

    $ $

  • 7/21/2019 Advanced Techniques of PL/SQL

    179/269

    PL/SQL Advanced Techniques - page 17

    File $yFile D e File (#ile+a$e!" b//lea 'et-al D $yFile=ca Read(!" i# ('et-al! 'et&' G" el%e 'et&' " ??

    Translate T%;E t# 1 and ALSE t# F" And d#nIt (#rget5 this is a '##lean pri$itive< n#t a )##lean class"

    9 ile:".ava

    Drapper (#r Pseud#-)##lean (uncti#nSi$ple translati#n 'ac+ t# PL/SQL )##lean" Av#id the hard-c#dings !ith na$ed c#nstants"""

    CREATE OR REPLACE PACUA E #ile ISF0+CTIO+ ca Read (#ile I+ VARCHAR2! RET0R+ BOOLEA+"

    E+,"

  • 7/21/2019 Advanced Techniques of PL/SQL

    180/269

    PL/SQL Advanced Techniques - page 18F

    CREATE OR REPLACE PACUA E BO,Y #ileIS F0+CTIO+ Ica Read (#ile I+ VARCHAR2! RET0R+ +05BER

    AS LA+ 0A E AVA +A5E File3=ca Read ( a-a=la 9=St'i 9! 'et&' i t "

    F0+CTIO+ ca Read (#ile I+ VARCHAR2! RET0R+ BOOLEA+ AS BE I+ RET0R+ Ica Read (#ile! D G" E+,"

    E+,"

    x(ile:"p+g9 ileB".ava

    x(ileB"p+g9 ile".avax(ile"p+g

    Passing O'.ects t# 9ava !ith ST%;3T ?#u can pass Oracle #'.ect in(#r$ati#n t# 9ava !ith#ut rel&ing #n 9Pu' '& using the ST%;3Tclass"

    p&blic cla%% 0 i/ B&%te' ;

    p&blic %tatic -/id a9eSt'ate9y (STR0CT e! th'/ % a-a=%7l=S@LE cepti/ ;

    O'tain attri'utes #(the Oracle #'.ect"

  • 7/21/2019 Advanced Techniques of PL/SQL

    181/269

    PL/SQL Advanced Techniques - page 181

    @ p ; et the att'ib&te% /# the lab/'_%/&'ce /b ect= Ob ect:< att'ib% D e=9etAtt'ib&te%(!"

    Acce%% i di-id&al att'ib&te% by a''ay i de ) %ta'ti 9 ith

    St'i 9 lab/'Type D (St'i 9!(att'ib%:

    Sy%te$=/&t=p'i tl ( NPay N lab/'Type N N

    h/&'ly_'ate N pe' h/&'N!" ??

    Extract individualattri'ute values (r#$

    the arra&";ni#n)uster".ava

    pass#'."tst

    Drapping a ST%;3T-'ased 6eth#d ?#u can pass Oracle #'.ect in(#r$ati#


Recommended