Anda di halaman 1dari 16

PL/SQL BLOCK DECLARATION SECTION

PL/SQL BLOCK DECLARATION SECTION

Objectives
At the end of this unit you will e ! le to" De#l!$e lo#!l %!$i! les fo$ the PL/SQL lo#&' Assi(n %!lues to the PL/SQL %!$i! les' In)ut !nd Out)ut %!lues f$o* the PL/SQL lo#&'

Topics
Int$odu#tion D!t!ty)es S#o)e of identifie$s Assi(n %!lues to %!$i! les In)ut !nd Out)ut %!lues Re%iew Questions

PL/SQL BLOCK DECLARATION SECTION

Introduction
The de#l!$!tion lo#& of PL/SQL is used to de#l!$e !ll identifie$s efo$e $efe$en#in( the* within the e,e#ut! le )!$t' -!$i! les. #u$so$s !nd e,#e)tions !$e de#l!$ed in this lo#&' In PL/SQL identifie$s !$e used !s n!*es' Identifie$s de#l!$ed in ! lo#& !$e #onside$ed lo#!l to th!t lo#& !nd (lo !l to !ll its su / lo#&s'

Declare an Identifier De#l!$e n!*e of identifie$' De#l!$e whethe$ identifie$ is %!$i! le o$ #onst!nt' De#l!$e d!t!ty)e of identifie$' De#l!$e whethe$ identifie$ fo$ ids null %!lues' De#l!$e identifie$ initi!l %!lue if !ny'

Syntax

Identifie$ 3CONSTANT4 d!t!ty)e 3NOTN5LL4 3" 6DE7A5LT )ls8l9e,)$ession4

Where Identifier CONSTANT Datatype NOT NU is the n!*e of the identifie$' #onst$!ins the identifie$ so th!t its %!lue #!nnot #h!n(e'0ust e initi!li1ed' is its s#!l!$ o$ #o*)osite d!t!ty)e' #onst$!ins the %!$i! le so th!t it *ust #ont!in ! %!lue' 0ust e initi!li1ed

PL/SQL BLOCK DECLARATION SECTION

Data types PL/SQL !llows de#l!$!tion of %!$i! les !nd #onst!nts' E%e$y %!$i! le !nd #onst!nt h!s ! d!t!ty)e to s)e#ify ! sto$!(e fo$*!t. #onst$!ints !nd %!lid $!n(e of %!lues' PL/SQL )$o%ides ! %!$iety of )$edefined s#!l!$ !nd #o*)osite d!t!ty)es'

S#!l!$ D!t!ty)e A s#!l!$ d!t! ty)e is the !si# d!t! ty)e with no inte$n!l #o*)onents'

S#!l!$ Ty)es

C;AR POSITI-E RO<ID NAT5RAL

N50BER 7LOAT DECI0AL REAL INTE=ER

LON=

-ARC;AR OR -ARC;AR+ BOOLEAN DATE

S0ALL INT

RA<

PL/SQL BLOCK DECLARATION SECTION

LON= RA<

!xa"ples To de#l!$e identifie$s of s#!l!$ d!t!ty)es' %9#ount %9s!l %9n!*e %9food #9n!*e Lo(i#D@ %9o$d9d!te %9%!lid Attributes In PL/SQL #onst!nts !nd %!$i! les h!%e !tt$i utes whi#h !$e )$o)e$ties th!t lets the use$ $efe$en#e the d!t!ty)e !nd the st$u#tu$e of !n o Ee#t without $e)e!tin( its definition' # T$%! Attribute The FT>PE !tt$i ute )$o%ides ! f!#ility to de#l!$e ! %!$i! le !##o$din( to !nothe$ !l$e!dy de#l!$ed %!$i! le o$ d!t! !se #olu*n' Ad%!nt!(es of usin( the FT>PE !tt$i ute !$e" The e,!#t d!t!ty)e of the unde$lyin( d!t! !se #olu*n need not e &nown' The definition of %!$i! le #h!n(es !##o$din(ly if the d!t!ty)e of the unde$lyin( d!t! !se #olu*n !t $unti*e' !xa"ples To de#l!$e identifie$s usin( FT>PE !tt$i ute' %9 en!*e %9s!l %9tot!l %9($oss #&OWT$%! Attribute e*)'en!*eFT>PE@ e*)'s!lFT>PE@ N50BERAG.+C@ %9tot!lFT>PE@ DATE NOT N5LL "6S>SDATE@ BOOLEAN NOT N5LL "6TR5E@ BINAR>9INTE=ER "6 ?@ N50BER AB.+C "6?@ -ARC;AR+A+?C@ C;ARA1C@ CONSTANT -ARC;AR+A11C"6DSyste*

PL/SQL BLOCK DECLARATION SECTION

The FRO<T>PE is used to de#l!$e ! %!$i! le !##o$din( to the d!t! st$u#tu$e of ! d!t! !se t! le' It )$o%ides ! $e#o$d ty)e th!t $e)$esents ! $ow in the t! le' The $e#o$d #!n sto$e !n enti$e $ow of d!t! sele#ted f$o* ! t! le' Ad%!nt!(es of usin( the FRO<T>PE !tt$i ute !$e" The nu* e$ of #olu*ns !nd the d!t!ty)es of the unde$lyin( d!t! !se t! le need not e &nown' If the nu* e$ !nd the d!t!ty)e of the unde$lyin( #olu*ns #h!n(e the definition of the %!$i! les !lso #h!n(es !t $unti*e' !xa"ple To de#l!$e !n identifie$/usin( FRO<T>PE !tt$i ute' de)t9$e#o$d Co"posite Datatype Co*)osite d!t!ty)e h!s inte$n!l #o*)onents th!t #!n e *!ni)ul!ted' de)tFRO<T>PE

Co*)osite Ty)es RECORD TABLE

RECORD !nd TABLE PL/SQL d!t!ty)es !llows use$s to #$e!te thei$ own use$9defined d!t!ty)es' % 'S( &!CO&D Is used to sto$e lo(i#!lly $el!ted d!t! !s ! sin(le unit' Is not the s!*e !s ! $ow in ! d!t! !se t! le' Is si*il!$ to ! st$u#tu$e o$ $e#o$d in 2=L' 0ust #ont!in one o$ *o$e #o*)onents of !ny s#!l!$ o$ RECORD d!t!ty)es #!lled fields of the PL/SQL RECORD whi#h sto$e dissi*il!$ ut lo(i#!lly $el!ted )ie#es of info$*!tion.

% 'S( TA) !

PL/SQL BLOCK DECLARATION SECTION

Is used to sto$e !n un#onst$!ined !$$!y of %!lues' Is not the s!*e !s the d!t! !se t! le' Is si*il!$ to one/di*ension!l !$$!y in 2=L' 0ust #ont!in ! #o*)onent of d!t!ty)e BINAR>9INTE=ER #!lled the )$i*!$y &ey of the PL/SQL TABLE. whi#h inde,es the !$$!y' 0ust #ont!in ! se#ond #o*)onent of !ny s#!l!$ d!t!ty)e #!lled the #olu*n of the PL/SQL TABLE. whi#h sto$es the !$$!y ele*ents'

RECORD !nd TABLE d!t!ty)es !$e useful fo$ )!ssin( d!t! sets !s ! sin(le !$(u*ent in )$o#edu$es !nd fun#tions' To de#l!$e the #o*)osite d!t!ty)e fi$st de#l!$e the #o*)osite d!t!ty)e with ! ty)e de#l!$!tion then de#l!$e the %!$i! le !##o$din( to the )$e%iously de#l!$ed ty)e' Datatype Syntax PL/SQL RECORD T>PE ty)e9n!*e IS RECORD Afield9n!*e1 Js#!l!$9d!t!ty)e %!$i! leFT>PE t! le'#olu*nFT>PE t! leFRO<T>PEK 3NOT N5LL4. field9n!*e+ Js#l!$9d!t!ty)e %!$i! leFT>PE t! le'#olu*nFT>PE t! leFRO<T>PEK 3NOT N5LL4. L''C@ L' identifie$ Where Ty)e9n!*e 7ield9n!*e S#!l!$9d!t!ty)e !xa"ples is the n!*e of the RECORD ty)e' is the n!*e of the field within the PL/SQL RECORD' is the d!t!ty)e of the field' ty)e9n!*e@

To de#l!$e !n identifie$ of RECORD d!t!ty)e' DECLARE T>PE de)9$e#9ty)e IS RECORD Ade)tno nu* e$A+C NOT N5LL "6+?.

PL/SQL BLOCK DECLARATION SECTION

dn!*e de)t'dn!*eFT>PE. lo# de)t'lo#FT>PEC@ de)9$e# de)9$e#9ty)e@

Datatype Syntax

PL/SQL TABLE T>PE ty)e9n!*e IS TABLE O7 Js#!l!$9d!t!ty)e %!$i! leFT>PE t! le'#olu*nFty)eK 3NOT N5LL4 INDEM B> BINAR>9INTE=ER@ L'' identifie$ ty)e9n!*e@

Where Ty)e9n!*e S#!l!$9d!t!ty)e Identifie$ !xa"ple is the n!*e of the TABLE ty)e is the d!t!ty)e of the PL/SQL TABLE ele*ents' is the n!*e of the identifie$'

To de#l!$e !n identifie$ of TABLE d!t!ty)e' DECLARE T>PE en!*e9t! IS TABLE O7 -ARC;AR+ A1?C INDEM B> BINAR>9INTE=ER@ LL e9t! en!*e9t! @

PL/SQL BLOCK DECLARATION SECTION

Scope of identifiers
A PL/SQL lo#& *!y #ont!in !ny nu* e$ of su lo#&s' Blo#&s #!n !lso e nested' The$efo$e ! few $ules !$e to e followed efo$e the de#l!$!tion of the identifie$s in diffe$ent su lo#&s' Rule 1" An identifie$ #!n e de#l!$ed only on#e in the s!*e lo#&. ut #!n e de#l!$ed on#e !(!in in diffe$ent lo#&s' The s#o)e o$ %isi ility of the identifie$ is ound y" Rule +" An identifie$ is %isi le in the !ll its su lo#&s unless the identifie$ $ede#l!$ed in su lo#&s the o$i(in!l lo#&s' But this new identifie$ is !(!in !xa"ple lo#& in whi#h it is de#l!$ed !nd in is $ede#l!$ed in su lo#&s' If it is identifie$ is not %isi le in the su ound y $ule 1'

In the followin( e,!*)le !n!ly1e !nd dete$*ine the %!lues fo$ e!#h identifie$ !t the s)e#ified )oints' DECLARE Code nu* e$ A:C "6 BBBB@ 0s( %!$#h!$+ A+?C" 6D=OOD 0ORNIN=D@ BE=IN /O S5B BLOCK 1 O/ DECLARE Code %!$#h!$+ AIC" 6 P;ELLOQD@ BE=IN Inse$t into t1 A*ess!(eC -!lues APA R P // P'D // *s(C@ END@ /O S5B BLOCK + O/ DECLARE My1 nu* e$ AHC" 6IIII@ 0s( %!$#h!$+ A+?C" 6 Pthis is ! #odeD@ BE=IN Inse$t into t1 A*ess!(eC -!lues APB R P // to9#h!$ A#odeC // P'D // *s(C@ END@

PL/SQL BLOCK DECLARATION SECTION

Inse$t into t1 A*ess!(eC -!lues AP C R P // to9#h!$ A#odeC // D.D //*s(C@ Inse$t into t1 A*ess!(eC -!lues APD RP // to9#h!$ A,y1C// P"D //*s(C@ END@

!xa"ples

The followin( shows so*e of the le(!l !nd ille(!l de#l!$!tions' e*al declarations N!*e Nu* 1 A*ount Do1en City Bi$thd!y Dued!te Re!dy Insto#& Ille*al declarations 7i$stn!*e. l!stn!*e #h!$ A+?C@ Tot!l nu* e$ NOT N5LL@ Pl!#e #h!$ A+?C "6 de)t'lo#!tion@ *o$e th!n one identifie$ on one line' !ssi(n*ent $e8ui$ed !s the %!$i! le h!s een de#l!$ed !s NOT N5LL identifie$ #!nnot e !ssi(ned to ! t! le #olu*n' #h!$ A+?C@ nu* e$@ nu* e$ AN.+C@ #onst!nt nu* e$" 6 1+@ #h!$ A+?C NOT N5LL" 6 PBAN=ALORED d!te@ d!te NOT N5LL" 6 P2?/DEC/BID@ Boole!n NOT N5LL" 6 t$ue@ Boole!n" 6 N5LL@

Note Only one %!$i! le o$ identifie$ #!n e de#l!$ed )e$ line' No !$$!ys !$e !llowed' If %!$i! le is de#l!$ed !s NOT N5LL then it *ust e initi!li1ed' T! le o$ #olu*n $efe$en#es !$e not !llowed on eithe$ side of !ssi(n*ent o)e$!to$' Assi(nin( %!lues to %!$i! les in the e,e#ut! le se#tion'

1?

PL/SQL BLOCK DECLARATION SECTION

Assi*n values to variables Assi(nin( %!lues to %!$i! les in the e,e#ut! le se#tion' Syntax

Pls8l9%!$i! le "6 )ls8l9e,)$ession@ Pls8l9t! le9n!*eA)$i*!$y9&ey9%!lueC "6 )ls8l9e,)$ession@ Re#o$d9n!*e'field9n!*e "6 )ls8l9e,)$ession@

!xa"ple The followin( shows so*e of the le(!l !ssi(n*ents' DECLARE A*ount Newn!*e Dued!te S!l 7n!*e E,d!ys -9fl!( BE=IN A*ount S!l Newn!*e Dued!te E,#essd!ys Newn!*e -9fl!( END@ nu* e$ AN.+C@ %!$#h!$+ A1?C@ d!te@ !*ountFty)e" 6+H@ newn!*eFty)e" 6D=ood d!yD@ nu* e$ ANC@ Boole!n@ "6 $ound As!l.?C@ "6 so$t A!*ountC@ "6 u))e$n!*e Afn!*eC@ "6 P+H/OCT/B:D "6 sysd!te R dued!te@ "6S5BSTR Af!n*e.1.HC@ "6Asysd!te 6 dued!teC@

11

PL/SQL BLOCK DECLARATION SECTION

Note

Do not uild PL/SQL e,)$essions usin( SQL ($ou) fun#tions su#h !s S50. A-= o$ CO5NT' Alw!ys te$*in!te ! PL/SQL lo#& within ! SQL O )lus s#$i)t with ! sl!sh on its own ti*e to distin(uish it f$o* othe$ SQL st!te*ents !nd SQL O )lus #o**!nds Do not use #olu*n o$ t! le $efe$en#es on eithe$ side of !ssi(n*ent o)e$!to$ A"6C' E(' E*)'s!l "6 +222@ N!*e "6 de)t'lo#@

The followin( o)e$!to$s !nd fun#tions #!n e used to uild ! PL/SQL e,)$ession' Ty)e of o)e$!to$ Nu*e$i# Ch!$!#te$ D!te E,!*)les OO. O. /. S. / // S. / IS

Co*)!$ison 6.Q6.T.U.T6.U6. N5LL.LIKE.BET<EEN.IN Ty)e of 7un#tion Nu*e$i# Ch!$!#te$ D!te Con%e$sion E,!*)les RO5ND'TR5NC.SQRT 5PPER.LO<ER.INTICAP.S5BSTR ADD90ONT;S.0ONT;S9BET<EEN N-L.=REATEST.LEAST

1+

PL/SQL BLOCK DECLARATION SECTION

Input and Output values In)ut ! %!lue into PL/SQL Blo#& A##e)t -9n!*e PRO0T PPle!se ente$ you$ n!*e" DECLARE d99n!*e %!$#h!$+A1?C"6DV%9n!*eD@ BE=IN N5LL@ END@ Output a value fro" % 'S( bloc+, -ARIABLE n!*e -ARC;AR+A1?C DECLARE d99n!*e "6 P0ICKE>D@ BE=IN "n!*e "6 d9n!*e@ END@ P$int n!*e

12

PL/SQL BLOCK DECLARATION SECTION

SU--A&$ ./
PL/SQL is the )$o#edu$!l e,tension to SQL' 0!ny o)e$!tions whi#h #!nnot e done th$ou(h ! non/)$o#edu$!l l!n(u!(e. #!n e done usin( PL/SQL' PL/SQL f!#ilit!tes %!$ious )$o($!**in( fe!tu$es su#h !s %!$i! le *!ni)ul!tions. #ont$ol st$u#tu$es !nd su /)$o($!**in(' The$e !$e %!$ious !d%!nt!(es of PL/SQL' It su))o$ts SQL. it is ! lo#& st$u#tu$ed l!n(u!(e. it in#$e!ses )$odu#ti%ity y usin( ette$ tools. it i*)$o%es )e$fo$*!n#e y $edu#in( #o**uni#!tion etween !))li#!tion !nd O$!#le se$%e$. it uses #ont$ol st$u#tu$e to #ont$ol the )$o#edu$!l flow of the )$o($!*' A PL/SQL lo#& !$e of two ty)es R An !nony*ous lo#& whi#h #onsists of th$ee se#tions. de#l!$!tion. the e,e#ut! le !nd the E,#e)tion h!ndle$. whi#h #!n e nested' A su )$o($!* PL/SQL )$o%ides ! %!$iety of d!t!ty)es su#h !s )$edefined s#!l!$. #o*)osite !nd LOB d!t!ty)es PL/SQL uses !tt$i utes FT>PE to de#l!$e ! %!$i! le of si*il!$ d!t!ty)e of !ny #olu*n of ! t! le' PL/SQL uses !tt$i ute FRO<T>PE to de#l!$e ! $e#o$d ty)e th!t $e)$esents ! $ow in ! t! le o$ %iew The PL/SQL !ssi(n*ent st!te*ent !llows ! %!lue to e !ssi(ned o$ $e!ssi(ned to ! %!$i! le !fte$ the DECLARE se#tion of the lo#& Non PL/SQL %!$i! les !$e $efe$en#ed y )$efi,in( the %!$i! le y ! #olon The only SQL #o**!nds !llowed in PL/SQL )$o($!* !$e D0L !nd TCL st!te*ents D *s9Out)ut'Put9Line is ! )$o#edu$e whi#h )$odu#es the out)ut on the s#$een

1:

PL/SQL BLOCK DECLARATION SECTION

&evie0 (uestions
In e!#h of the followin( de#l!$!tions' Dete$*ine whi#h of the* !$e not le(!l !nd e,)l!in why' 1' DECLARE -9e*)no N50BER A:C@ +' DECLARE -9>. -9W %!$#h!$+ A1?C@ 2' DECLARE Dued!te DATE NOT N5LL@ :' DECLARE De)9$e# de)9$e#/ty)@ H' DECLARE T>PE e*)9t! 9ty) IS TABLE O7 -ARC;AR+ A+?C INDEM B> BINAR>9INTE=ER" E*)9t! e*)9t! 9ty)@ I' DECLARE -9fl!( BOOLEAN" 61@ G' DECLARE -9n!*e CONSTANT -ARC;AR+ A+?C@ N' DECLARE -9#ount BINAR>9INTE=ER" 6 /++@ B' DECLARE -!$1 NAT5RAL "6 ?@ 1?' DECLARE -!$+ POSITI-E" 6?@

1H

PL/SQL BLOCK DECLARATION SECTION

In e!#h of the followin( !ssi(n*ents w$ite the d!t!ty)e of $esultin( if it is le(!l' =i%e $e!sons fo$ it' 1' %9nu* e$9d!ys +' %9fl!( 2' %9one :' %9#o**ent H' %9tot!l I' Dued!te G' %9s!l N' N!*e9t! le A2C B' %9*!,9s!l 1?' %9#ount "6dued!te R sysd!te@ "6N5LL@ "6 %9twoU+O%9th$ee@ "6use$ // P"D// to9#h!$A%9de)tnoC@ "6su*Ae*)'s!lC@ "6 sysd!te S 1?@ "6e*)'s!l@ " 6%9s!l@ "6-9s!l@ "6 %9%!$OO+"

1I

Anda mungkin juga menyukai