Reierenum;i*,u^i'dto<Ki"es\Hinfl-t'nle-nLfrom<linllerMini.Synl.isI l>leleierue.111elementisas follows:
colljiame [subscript):
Inlliii <ynl.ix:
U
C 1_nd[ne io|Mftt-ntsHit'nriinuof atollcs linn.u
iju^jct.pt
i ropreseiustht?elementthat h.attobepronged,
rherc i&some
specified r.innc for iuoscript. which van0*1 jeeordm^ to the iolleUion lype. Following are the sijIjmnptr.Jnjie^ .uinrdinj"to[heir1ulletLion\\pi's:* Forv,irrr,ivs,therongt1ij.1 toupperImundof(lievarr.iy.
* E-ornesledtable.[lieranjieisIin2N74JJlf>4 7.
* *issutraTke.nra\^uithnuiiieru kev,therangeis-2I474HS647(n+2
M74H
164 7tl~or.i^isih.\.iU\\<.irr.iy^.withY-.PCSJAR/v.itue/iskey,iheran#'depends onthelengthof thevaluespetiiieilduringivpe.drtl.ic.ition.inddatabaseMhleerection
Listing6.6show;
how
10perform rderentinj>in atnllection.Listing6.6:PL/SQL programlousereierennngin aillijeiioris
Theoutputoif.NinfiU 6i*.nsfollow1-:
InIisling 6.Ej.
we
h.iveused,1turn tiun."CN"" fL,w
1 1ith1uunlsthenumber(ilelementsin tile nestedt<iblcandmoves(heloopupto lhalcount.116
6: Colfcctiowa<dRecords
this
we
concludediscussiononreferencing (hecollections.Now, sometimes youmay
need (oassignonecollection intoanothercollection,how
youcandothisisexplainednext.Collections
assignacollection intoanothercollection, theelement typesandthedatatypenamesofboth collectionsmustbesame.Thereare variouswaystoassignacollection intoanothercollection, suchas using
INSERT
statementUPDATE
statement,SELECT
statement,assignment statement, andalso the subprogramcall. Listing6.7 showshow
lo assign one collectioninto another6.7:PL/SQL programto assign collections
outputof Listing6.7isasfollows:
PL/SQL procedure succesfully completed.
InListing 6.7:
First statement,
A
:-B, is allowed, becauseIxithA
and B have thesameelement type (NUMBER) anddatatype(eid)name.Secondstatement.B:=C,isnotallowed,becausealthough theyhavethe
same
element type buttheirdatatypenamesaredifferent,thai is.tliedatatypename
offtiseid
butthat ofC
is esal.of Listing6.7showsthattheprocedureiscompletedsuccessfully.It isbecause
we
given theillegalassignment asacomment. However,ifyoutry torunListing6.7without theillegalassignment acomment,then the followingerrormessageappears:
117
You
canalso assignanullvalueloanothercollodion orcanalso assignvaluesin acollectionby u^ingexpressions. SecListingfer8toknow how
toassignanexpressionandnullintoanother collection.Listingfi,6:PL/SQL programtoassignexpressionandnullvalue
3nListing(tJi,
we
have.l^sigrii'dan expreviirmtoanelement presentattheirrstpositioninthe varMy,andthen displayedIhevalue assignedlo[heelementM
firsl|Hisiticin.Allerih.irwe
have assignedllic nestedtable3(inilializedasnullito ncslt'dtableC,andilllastvvc h*ivcusedihcCOUNT
method toknow
the number ni dements in ihe nested table C alterassigning thel-xpredion.
OulputofListingGilisastollers;
Thisisal[.itnmlaligningQ>llc*clions.Iet's
now
learnhow
loeompiirecollecljonsComparing
CollectionsSometimesvnu
mav
requireloknow
uhellu't,\culiec-Iioriisnullornot;orwhethertheelements in(wo collections are similaror notrInPL/SQL you cancompare twocolleclionslofindoi_r|whetherthey.ir**nlil 3or similar. Listingtt.'Jshows
how
locomparetwocollections.Listing b.9:PL/SQLprogramtocornparecollections
118
Qmptcr6:Understanding
PUSQL
ColiecliomandRecordsInListing 6.9,
we
havedeclaredtwovariablesof nestedtablesbulinitializedonlyonevariable, thatisC,andkeptB
asblank. Afterthat,we
havecompared BforNull, andthenB and Cto check whethertheyhavesimilarelementsor not.Theoutput ofListing6.9isasfollows:Thisisallaboutcomparingcollections.Let'ssee
how
tousecollectionswithdatabasetablesby performingoperations,suchas create,insert,update,onatableinthedatabase.Usinga
VARRAY
Toperform operationsonadatabasetableusingvarrays,you mustdefine,declare,andinitialize thevan-ay.Theprocessto define, declare,andinitializea varrayhas alreadybeendiscussed.
Let'sfeam
how
tocreateatable thathas acolumnof the varraycollection type.SQL> CREATETABLE
V.jrny
(ENAMEUABCHJKI(30J, EAOOBiruddl);Theoutput ofthisstatementisasfollows:
"7 Table created.
In thepreceding statement,
we
havecreated a [ablenamed V
array. This tablehastwo columns;oneofthemisofVARCHAR2
datatypewhile [he otherisofvarray
datatype.Lei'snow
take thetable,emp
addl, thatwe
have createdearlier (describedunder theheading 'Definingvarrays'l,andinsertarccoid inthat table. Listing6.10showsthecodeto inserta recordintheerop addltable.listing6.10:
PUSQL
programtoaddrecordsinemp_addl table119
Quiputof Listing6-10isas lollows.
PL/sqL procedurecompleted suceessf jlly.
Afler understanding
how
to insert rcinrd> in,1 daUkisetable, let'*. urxleNandhow
to use i;?tar~.,indif.:~rr
^.ltemem^on,1i!,il,th.i^e(ablehavingroluimv;ofrvpevarr.iy Listingb11
uses
itta
:? and^"".F""'i.iti'mcnson.i<Ln,ib.i*irlableListing b.11:ML/SQL programiouM1 iiD-\ I'Kand£"L.lLC:istalornentonr.rp idallable.
SQ.L> SET SERVE ROUT PUTON DECLARE
add„varray enp_add_; Declaring, variable of vaprav
iemrj_addl;
—
Declaring variable of varray BEGINadd_varray ;- emp_addl ("Street no 12', 'H.no20'};
—
Initializing varray variable--Updating values in thetable V_array
UPDATE V_array set EADD* add_varrayWHERE ENAME- 'Anrit';
-- selecting the updated field from v_array SELECT EADD intok fromv_array inhere ENAME = 'Amith:
fori in«,FIflST
„
k.laST loop DBM5_0uTPLiT.PUT_L.lNE (ati));end loop;
END;
/
In Ii<-tmgb 1I,
we
h,i\e performedluoOfM'r.Hinr^ 1ir-stwe
li.iv<> irfniJ^le-rIthe d.tt.ib.i'iet.ilile\ -iz ljvriinf1thenwe?have
cucutcd
(he ..:\L^-"Lstatementtovic^-theupdatedvalue:*;.OiiiputofIidlingF h11 ,jk[nllnws:
Street no 12 ii.no20
PL/SQL procedure completed successfully.
[iithesame
wav
asv<irray\ne^ledlables<.inalsolieusedin.idatabase Hul there\badifferente intredlirijT[hedatabasetablewithcolumnas nestedtable (\pe.Let'screatea database(able\viththecolumnsoftvpenestedtabIf.
5£}L>CREATE TABLE ne5t_dema (ENAME VARCHAR2 (30)T EADD emp_addZ) NESTED TABLE EADD STORE AS
EJDDl
;
In the. precedingstatement
we
h.nocreated ,t tabbnamed
nnc: doro. Thi, cablehas(wo columns: one ol them isof YAi-aiAr^ tipe while(lie other isof nested table lv|X\ The i^^_^;lnrhas alreadvl>rt-riire.ilE'dunder ihoheading 'DefiningNestedTable'.Oulpul of ihe preceding stalcmentisas follows:Table created.
Tlii1. is.ill about usingvarray andnested tabicwilh databasetables. Now,
wo
continueour di^tussionbyespI.liningnilIntioumethods In ]isling6.1.we
li.ncusedamethodCO'JKTtoknow
thenumbernlelemenl'.inanestedtable.Pt/SQI hdsminimsoiliermethodsk)rerJmeIhe progranimingtasks. Lot's stuclvi'L'SCJL built-inmethodstlnatcm
beu^ed with tollotUons,120
Chapter6:Understanding