PL - SQL Quick Guide

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

PL/SQLQuickGuide

Advertisements

PreviousPage

NextPage

ThePL/SQLprogramminglanguagewasdevelopedbyOracleCorporationinthelate1980sasproceduralextensionlanguageforSQLand
theOraclerelationaldatabase.FollowingarenotablefactsaboutPL/SQL:
PL/SQLisacompletelyportable,highperformancetransactionprocessinglanguage.
PL/SQLprovidesabuiltininterpretedandOSindependentprogrammingenvironment.
PL/SQLcanalsodirectlybecalledfromthecommandlineSQL*Plusinterface.
Directcallcanalsobemadefromexternalprogramminglanguagecallstodatabase.
PL/SQL'sgeneralsyntaxisbasedonthatofADAandPascalprogramminglanguage.
ApartfromOracle,PL/SQLisavailableinTimesTeninmemorydatabaseandIBMDB2.

PL/SQLEnvironmentSetup
PL/SQLisnotastandaloneprogramminglanguageitisatoolwithintheOracleprogrammingenvironment.SQL*Plusisaninteractive
toolthatallowsyoutotypeSQLandPL/SQLstatementsatthecommandprompt.Thesecommandsarethensenttothedatabasefor
processing.Oncethestatementsareprocessed,theresultsaresentbackanddisplayedonscreen.
To run PL/SQL programs, you should have Oracle RDBMS Server installed in your machine which will take care of executing SQL
commands.MostrecentversionofOracleRDBMSis11g.YoucandownloadatrialversionofOracle11gfromthefollowinglink:
DownloadOracle11gExpressEdition
Youwillhavetodownloadeither32bitor64bitversionoftheinstallationasperyouroperatingsystem.Usuallytherearetwofiles,asI
havedownloadedfor64bitWindows7.Youwillalsousesimilarstepsonyouroperatingsystem,doesnotmatterifitisLinuxorSolaris.
win64_11gR2_database_1of2.zip
win64_11gR2_database_2of2.zip
Afterdownloadingabovetwofiles,youwillneedtounziptheminasingledirectorydatabaseandunderthatyouwillfindfollowingsub
directories:

Finally,clickonsetupfiletostarttheinstallationandfollowthegivenstepstilltheend.Ifeverythinghasbeendonesuccessfullythenitstimeto
verifyyourinstallation.AtyourcommandpromptusethefollowingcommandifyouareusingWindows:
sqlplus"/assysdba"

Ifeverythingisfine,youshouldhaveSQLpromptwhereyouwillwriteyourPL/SQLcommandsandscripts:

TextEditor
Running large programs from command prompt may land you in inadvertently losing some of the work. So a better option is to use
commandfiles.Tousethecommandfiles:
Typeyourcodeinatexteditor,likeNotepad,Notepad+,orEditPlus,etc.
Savethefilewiththe.sqlextensioninthehomedirectory.
LaunchSQL*PluscommandpromptfromthedirectorywhereyoucreatedyourPL/SQLfile.
Type@file_nameattheSQL*Pluscommandprompttoexecuteyourprogram.
IfyouarenotusingafiletoexecutePL/SQLscripts,thensimplycopyyourPL/SQLcodeandthenrightclickontheblackwindowhaving
SQLpromptandusepasteoptiontopastecompletecodeatthecommandprompt.Finally,justpressentertoexecutethecode,ifitis
notalreadyexecuted.

PL/SQLBasicSyntax
PL/SQL is a blockstructured language, meaning that PL/SQL programs are divided and written in logical blocks of code. Each block
consistsofthreesubparts:
S.N.

Sections&Description

Declarations

ThissectionstartswiththekeywordDECLARE.Itisanoptionalsectionanddefinesallvariables,cursors,subprograms,and
otherelementstobeusedintheprogram.
2

ExecutableCommands

ThissectionisenclosedbetweenthekeywordsBEGINandEND and it is a mandatory section. It consists of the executable


PL/SQLstatementsoftheprogram.Itshouldhaveatleastoneexecutablelineofcode,whichmaybejustaNULLcommandto
indicatethatnothingshouldbeexecuted.
3

ExceptionHandling

ThissectionstartswiththekeywordEXCEPTION.Thissectionisagainoptionalandcontainsexception(s)thathandleerrorsin
theprogram.

Every PL/SQL statement ends with a semicolon (). PL/SQL blocks can be nested within other PL/SQL blocks using BEGIN and END.
HereisthebasicstructureofaPL/SQLblock:
DECLARE
<declarationssection>
BEGIN
<executablecommand(s)>
EXCEPTION
<exceptionhandling>
END;

The'HelloWorld'Example:
DECLARE
messagevarchar2(20):='Hello,World!';
BEGIN
dbms_output.put_line(message);
END;
/

TheendlinesignalstheendofthePL/SQLblock.TorunthecodefromSQLcommandline,youmayneedtotype/atthebeginningof
thefirstblanklineafterthelastlineofthecode.WhentheabovecodeisexecutedatSQLprompt,itproducesfollowingresult:
HelloWorld
PL/SQLproceduresuccessfullycompleted.

PL/SQLDataTypes
PL/SQLvariables,constantsandparametersmusthaveavaliddatatypewhichspecifiesastorageformat,constraints,andvalidrangeof
values.ThistutorialwilltakeyouthroughSCALARandLOBdatatypesavailableinPL/SQLandothertwodatatypeswillbecoveredin
otherchapters.
Category

Description

Scalar

Singlevalueswithnointernalcomponents,suchasaNUMBER,DATE,orBOOLEAN.

LargeObject(LOB)

Pointerstolargeobjectsthatarestoredseparatelyfromotherdataitems,suchastext,graphicimages,
videoclips,andsoundwaveforms.

Composite

Dataitemsthathaveinternalcomponentsthatcanbeaccessedindividually.Forexample,collectionsand
records.

Reference

Pointerstootherdataitems.

PL/SQLScalarDataTypesandSubtypes
PL/SQLScalarDataTypesandSubtypescomeunderthefollowingcategories:
DateType

Description

Numeric

Numericvaluesonwhicharithmeticoperationsareperformed.

Character

Alphanumericvaluesthatrepresentsinglecharactersorstringsofcharacters.

Boolean

Logicalvaluesonwhichlogicaloperationsareperformed.

Datetime

Datesandtimes.

PL/SQLprovidessubtypesofdatatypes.Forexample,thedatatypeNUMBERhasasubtypecalledINTEGER.Youcanusesubtypesin
yourPL/SQLprogramtomakethedatatypescompatiblewithdatatypesinotherprogramswhileembeddingPL/SQLcodeinanother
program,suchasaJavaprogram.

PL/SQLNumericDataTypesandSubtypes
FollowingisthedetailofPL/SQLpredefinednumericdatatypesandtheirsubtypes:
DataType

Description

PLS_INTEGER

Signedintegerinrange2,147,483,648through2,147,483,647,representedin32bits

BINARY_INTEGER

Signedintegerinrange2,147,483,648through2,147,483,647,representedin32bits

BINARY_FLOAT

SingleprecisionIEEE754formatfloatingpointnumber

BINARY_DOUBLE

DoubleprecisionIEEE754formatfloatingpointnumber

NUMBER(prec,scale)

Fixedpointorfloatingpointnumberwithabsolutevalueinrange1E130to(butnotincluding)1.0E126.
ANUMBERvariablecanalsorepresent0.

DEC(prec,scale)

ANSIspecificfixedpointtypewithmaximumprecisionof38decimaldigits.

DECIMAL(prec,scale)

IBMspecificfixedpointtypewithmaximumprecisionof38decimaldigits.

NUMERIC(pre,secale)

Floatingtypewithmaximumprecisionof38decimaldigits.

DOUBLEPRECISION

ANSIspecificfloatingpointtypewithmaximumprecisionof126binarydigits(approximately38decimal
digits)

FLOAT

ANSIandIBMspecificfloatingpointtypewithmaximumprecisionof126binarydigits(approximately38
decimaldigits)

INT

ANSIspecificintegertypewithmaximumprecisionof38decimaldigits

INTEGER

ANSIandIBMspecificintegertypewithmaximumprecisionof38decimaldigits

SMALLINT

ANSIandIBMspecificintegertypewithmaximumprecisionof38decimaldigits

REAL

Floatingpointtypewithmaximumprecisionof63binarydigits(approximately18decimaldigits)

Followingisavaliddeclaration:
DECLARE
num1INTEGER;
num2REAL;
num3DOUBLEPRECISION;
BEGIN
null;
END;
/

Whentheabovecodeiscompiledandexecuted,itproducesthefollowingresult:
PL/SQLproceduresuccessfullycompleted

PL/SQLCharacterDataTypesandSubtypes
FollowingisthedetailofPL/SQLpredefinedcharacterdatatypesandtheirsubtypes:
DataType

Description

CHAR

Fixedlengthcharacterstringwithmaximumsizeof32,767bytes

VARCHAR2

Variablelengthcharacterstringwithmaximumsizeof32,767bytes

RAW

Variablelengthbinaryorbytestringwithmaximumsizeof32,767bytes,notinterpretedbyPL/SQL

NCHAR

Fixedlengthnationalcharacterstringwithmaximumsizeof32,767bytes

NVARCHAR2

Variablelengthnationalcharacterstringwithmaximumsizeof32,767bytes

LONG

Variablelengthcharacterstringwithmaximumsizeof32,760bytes

LONGRAW

Variablelengthbinaryorbytestringwithmaximumsizeof32,760bytes,notinterpretedbyPL/SQL

ROWID

Physicalrowidentifier,theaddressofarowinanordinarytable

UROWID

Universalrowidentifier(physical,logical,orforeignrowidentifier)

PL/SQLBooleanDataTypes
TheBOOLEANdatatypestoreslogicalvaluesthatareusedinlogicaloperations.ThelogicalvaluesaretheBooleanvaluesTRUEand
FALSEandthevalueNULL.
However,SQLhasnodatatypeequivalenttoBOOLEAN.Therefore,Booleanvaluescannotbeusedin:
SQLstatements
BuiltinSQLfunctions(suchasTO_CHAR)
PL/SQLfunctionsinvokedfromSQLstatements

PL/SQLDatetimeandIntervalTypes
The DATE datatype to store fixedlength datetimes, which include the time of day in seconds since midnight. Valid dates range from
January1,4712BCtoDecember31,9999AD.
ThedefaultdateformatissetbytheOracleinitializationparameterNLS_DATE_FORMAT.Forexample,thedefaultmightbe'DDMON
YY',whichincludesatwodigitnumberforthedayofthemonth,anabbreviationofthemonthname,andthelasttwodigitsoftheyear,
forexample,01OCT12.
EachDATEincludesthecentury,year,month,day,hour,minute,andsecond.Thefollowingtableshowsthevalidvaluesforeachfield:
FieldName

ValidDatetimeValues

ValidIntervalValues

YEAR

4712to9999(excludingyear0)

Anynonzerointeger

MONTH

01to12

0to11

DAY

01to31(limitedbythevaluesofMONTHandYEAR,accordingtothe
rulesofthecalendarforthelocale)

Anynonzerointeger

HOUR

00to23

0to23

MINUTE

00to59

0to59

SECOND

00to59.9(n),where9(n)istheprecisionoftimefractionalseconds

0to59.9(n),where9(n)istheprecision
ofintervalfractionalseconds

TIMEZONE_HOUR

12to14(rangeaccommodatesdaylightsavingstimechanges)

Notapplicable

TIMEZONE_MINUTE

00to59

Notapplicable

TIMEZONE_REGION

FoundinthedynamicperformanceviewV$TIMEZONE_NAMES

Notapplicable

TIMEZONE_ABBR

FoundinthedynamicperformanceviewV$TIMEZONE_NAMES

Notapplicable

PL/SQLLargeObject(LOB)DataTypes
Largeobject(LOB)datatypesreferlargetodataitemssuchastext,graphicimages,videoclips,andsoundwaveforms.LOBdatatypes
allowefficient,random,piecewiseaccesstothisdata.FollowingarethepredefinedPL/SQLLOBdatatypes:
DataType

Description

Size

BFILE

Usedtostorelargebinaryobjectsinoperatingsystemfilesoutsidethe
database.

Systemdependent.Cannotexceed4
gigabytes(GB).

BLOB

Usedtostorelargebinaryobjectsinthedatabase.

8to128terabytes(TB)

CLOB

Usedtostorelargeblocksofcharacterdatainthedatabase.

8to128TB

NCLOB

UsedtostorelargeblocksofNCHARdatainthedatabase.

8to128TB

PL/SQLUserDefinedSubtypes
Asubtypeisasubsetofanotherdatatype,whichiscalleditsbasetype.Asubtypehasthesamevalidoperationsasitsbasetype,but
onlyasubsetofitsvalidvalues.
PL/SQLpredefinesseveralsubtypesinpackageSTANDARD.Forexample,PL/SQLpredefinesthesubtypesCHARACTERandINTEGERas
follows:
SUBTYPECHARACTERISCHAR;
SUBTYPEINTEGERISNUMBER(38,0);

Youcandefineanduseyourownsubtypes.Thefollowingprogramillustratesdefiningandusingauserdefinedsubtype:
DECLARE
SUBTYPEnameISchar(20);
SUBTYPEmessageISvarchar2(100);
salutationname;
greetingsmessage;
BEGIN
salutation:='Reader';
greetings:='WelcometotheWorldofPL/SQL';
dbms_output.put_line('Hello'||salutation||greetings);
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
HelloReaderWelcometotheWorldofPL/SQL
PL/SQLproceduresuccessfullycompleted.

NULLsinPL/SQL
PL/SQLNULLvaluesrepresentmissingorunknowndataandtheyarenotaninteger,acharacter,oranyotherspecificdatatype.Note
thatNULLisnotthesameasanemptydatastringorthenullcharactervalue'\0'.Anullcanbeassignedbutitcannotbeequatedwith
anything,includingitself.

PL/SQLVariables
ThenameofaPL/SQLvariableconsistsofaletteroptionallyfollowedbymoreletters,numerals,dollarsigns,underscores,andnumber
signs and should not exceed 30 characters. By default, variable names are not casesensitive. You cannot use a reserved PL/SQL
keywordasavariablename.

VariableDeclarationinPL/SQL
PL/SQLvariablesmustbedeclaredinthedeclarationsectionorinapackageasaglobalvariable.Whenyoudeclareavariable,PL/SQL
allocatesmemoryforthevariable'svalueandthestoragelocationisidentifiedbythevariablename.
Thesyntaxfordeclaringavariableis:

variable_name[CONSTANT]datatype[NOTNULL][:=|DEFAULTinitial_value]

Where,variable_nameisavalididentifierinPL/SQL,datatypemustbeavalidPL/SQLdatatypeoranyuserdefineddatatypewhichwe
alreadyhavediscussedinlastchapter.Somevalidvariabledeclarationsalongwiththeirdefinitionareshownbelow:
salesnumber(10,2);
piCONSTANTdoubleprecision:=3.1415;
namevarchar2(25);
addressvarchar2(100);

Whenyouprovideasize,scaleorprecisionlimitwiththedatatype,itiscalledaconstraineddeclaration. Constrained declarations


requirelessmemorythanunconstraineddeclarations.Forexample:
salesnumber(10,2);
namevarchar2(25);
addressvarchar2(100);

InitializingVariablesinPL/SQL
Wheneveryoudeclareavariable,PL/SQLassignsitadefaultvalueofNULL.Ifyouwanttoinitializeavariablewithavalueotherthanthe
NULLvalue,youcandosoduringthedeclaration,usingeitherofthefollowing:
TheDEFAULTkeyword
Theassignmentoperator
Forexample:
counterbinary_integer:=0;
greetingsvarchar2(20)DEFAULT'HaveaGoodDay';

YoucanalsospecifythatavariableshouldnothaveaNULLvalueusingtheNOTNULLconstraint.IfyouusetheNOTNULLconstraint,
youmustexplicitlyassignaninitialvalueforthatvariable.
Itisagoodprogrammingpracticetoinitializevariablesproperlyotherwise,sometimeprogramwouldproduceunexpectedresult.Trythe
followingexamplewhichmakesuseofvarioustypesofvariables:
DECLARE
ainteger:=10;
binteger:=20;
cinteger;
freal;
BEGIN
c:=a+b;
dbms_output.put_line('Valueofc:'||c);
f:=70.0/3.0;
dbms_output.put_line('Valueoff:'||f);
END;
/

Whentheabovecodeisexecuted,itproducesthefollowingresult:
Valueofc:30
Valueoff:23.333333333333333333
PL/SQLproceduresuccessfullycompleted.

VariableScopeinPL/SQL
PL/SQLallowsthenestingofBlocks,i.e.,eachprogramblockmaycontainanotherinnerblock.Ifavariableisdeclaredwithinaninner
block,itisnotaccessibletotheouterblock.However,ifavariableisdeclaredandaccessibletoanouterBlock,itisalsoaccessibletoall
nestedinnerBlocks.Therearetwotypesofvariablescope:
Localvariablesvariablesdeclaredinaninnerblockandnotaccessibletoouterblocks.
Globalvariablesvariablesdeclaredintheoutermostblockorapackage.
FollowingexampleshowstheusageofLocalandGlobalvariablesinitssimpleform:
DECLARE
Globalvariables
num1number:=95;
num2number:=85;
BEGIN
dbms_output.put_line('OuterVariablenum1:'||num1);
dbms_output.put_line('OuterVariablenum2:'||num2);
DECLARE
Localvariables
num1number:=195;
num2number:=185;
BEGIN
dbms_output.put_line('InnerVariablenum1:'||num1);
dbms_output.put_line('InnerVariablenum2:'||num2);

END;
END;
/

Whentheabovecodeisexecuted,itproducesthefollowingresult:
OuterVariablenum1:95
OuterVariablenum2:85
InnerVariablenum1:195
InnerVariablenum2:185
PL/SQLproceduresuccessfullycompleted.

PL/SQLConstantsandLiterals
Aconstantholdsavaluethatoncedeclared,doesnotchangeintheprogram.Aconstantdeclarationspecifiesitsname,datatype,and
value,andallocatesstorageforit.ThedeclarationcanalsoimposetheNOTNULLconstraint.

DeclaringaConstant
A constant is declared using the CONSTANT keyword. It requires an initial value and does not allow that value to be changed. For
example:
PICONSTANTNUMBER:=3.141592654;
DECLARE
constantdeclaration
piconstantnumber:=3.141592654;
otherdeclarations
radiusnumber(5,2);
dianumber(5,2);
circumferencenumber(7,2);
areanumber(10,2);
BEGIN
processing
radius:=9.5;
dia:=radius*2;
circumference:=2.0*pi*radius;
area:=pi*radius*radius;
output
dbms_output.put_line('Radius:'||radius);
dbms_output.put_line('Diameter:'||dia);
dbms_output.put_line('Circumference:'||circumference);
dbms_output.put_line('Area:'||area);
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Radius:9.5
Diameter:19
Circumference:59.69
Area:283.53
Pl/SQLproceduresuccessfullycompleted.

ThePL/SQLLiterals
A literal is an explicit numeric, character, string, or Boolean value not represented by an identifier. For example, TRUE, 786, NULL,
'tutorialspoint'areallliteralsoftypeBoolean,number,orstring.PL/SQL,literalsarecasesensitive.PL/SQLsupportsthefollowingkinds
ofliterals:
NumericLiterals
CharacterLiterals
StringLiterals
BOOLEANLiterals
DateandTimeLiterals
Thefollowingtableprovidesexamplesfromallthesecategoriesofliteralvalues.
LiteralType

Example:

NumericLiterals

05078140+32767
6.66670.012.03.14159+7800.00
6E51.0E83.14159e01E389.5e3

CharacterLiterals

'A''%''9''''z''('

StringLiterals

'Hello,world!'

'TutorialsPoint'
'19NOV12'
BOOLEANLiterals

TRUE,FALSE,andNULL.

DateandTimeLiterals

DATE'19781225'

TIMESTAMP'2012102912:01:01'

Toembedsinglequoteswithinastringliteral,placetwosinglequotesnexttoeachotherasshownbelow:
DECLARE
messagevarchar2(20):='That''stutorialspoint.com!';
BEGIN
dbms_output.put_line(message);
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
That'stutorialspoint.com!
PL/SQLproceduresuccessfullycompleted.

PL/SQLOperators
Anoperatorisasymbolthattellsthecompilertoperformspecificmathematicalorlogicalmanipulation.PL/SQLlanguageisrichinbuilt
inoperatorsandprovidesthefollowingtypeofoperators:
Arithmeticoperators
Relationaloperators
Comparisonoperators
Logicaloperators
Stringoperators
Thistutorialwillexplainthearithmetic,relational,comparisonandlogicaloperatorsonebyone.TheStringoperatorswillbediscussed
underthechapter:PL/SQLStrings.

ArithmeticOperators
FollowingtableshowsallthearithmeticoperatorssupportedbyPL/SQL.AssumevariableAholds10andvariableBholds5then:
Operator

Description

Example

Addstwooperands

A+Bwillgive15

Subtractssecondoperandfromthefirst

ABwillgive5

Multipliesbothoperands

A*Bwillgive50

Dividesnumeratorbydenumerator

A/Bwillgive2

**

Exponentiationoperator,raisesoneoperandtothepowerofother

A**Bwillgive100000

RelationalOperators
RelationaloperatorscomparetwoexpressionsorvaluesandreturnaBooleanresult.Followingtableshowsalltherelationaloperators
supportedbyPL/SQL.AssumevariableAholds10andvariableBholds20,then:
Operator

Description

Example

Checksifthevaluesoftwooperandsareequalornot,ifyesthencondition
becomestrue.

(A=B)isnottrue.

!=
<>
~=

Checksifthevaluesoftwooperandsareequalornot,ifvaluesarenotequal
thenconditionbecomestrue.

(A!=B)istrue.

>

Checksifthevalueofleftoperandisgreaterthanthevalueofrightoperand,
ifyesthenconditionbecomestrue.

(A>B)isnottrue.

<

Checksifthevalueofleftoperandislessthanthevalueofrightoperand,if

(A<B)istrue.

yesthenconditionbecomestrue.
>=

Checksifthevalueofleftoperandisgreaterthanorequaltothevalueof
rightoperand,ifyesthenconditionbecomestrue.

(A>=B)isnottrue.

<=

Checksifthevalueofleftoperandislessthanorequaltothevalueofright
operand,ifyesthenconditionbecomestrue.

(A<=B)istrue.

ComparisonOperators
Comparisonoperatorsareusedforcomparingoneexpressiontoanother.TheresultisalwayseitherTRUE,FALSEORNULL.
Operator

Description

Example

LIKE

TheLIKEoperatorcomparesacharacter,string,orCLOBvaluetoapattern
andreturnsTRUEifthevaluematchesthepatternandFALSEifitdoesnot.

If'ZaraAli'like'Z%A_i'returnsaBooleantrue,
whereas,'NuhaAli'like'Z%A_i'returnsa
Booleanfalse.

BETWEEN

TheBETWEENoperatortestswhetheravalueliesinaspecifiedrange.x
BETWEENaANDbmeansthatx>=aandx<=b.

Ifx=10then,xbetween5and20returnstrue,
xbetween5and10returnstrue,butxbetween
11and20returnsfalse.

IN

TheINoperatortestssetmembership.xIN(set)meansthatxisequalto
anymemberofset.

Ifx='m'then,xin('a','b','c')returnsboolean
falsebutxin('m','n','o')returnsBooleantrue.

ISNULL

TheISNULLoperatorreturnstheBOOLEANvalueTRUEifitsoperandisNULL
orFALSEifitisnotNULL.ComparisonsinvolvingNULLvaluesalwaysyield
NULL.

Ifx='m',then'xisnull'returnsBooleanfalse.

LogicalOperators
FollowingtableshowstheLogicaloperatorssupportedbyPL/SQL.AlltheseoperatorsworkonBooleanoperandsandproducesBoolean
results.AssumevariableAholdstrueandvariableBholdsfalse,then:
Operator

Description

Example

and

CalledlogicalANDoperator.Ifboththeoperandsaretruethencondition
becomestrue.

(AandB)isfalse.

or

CalledlogicalOROperator.Ifanyofthetwooperandsistruethencondition
becomestrue.

(AorB)istrue.

not

CalledlogicalNOTOperator.Usedtoreversethelogicalstateofitsoperand.
IfaconditionistruethenLogicalNOToperatorwillmakeitfalse.

not(AandB)istrue.

PL/SQLOperatorPrecedence
Operatorprecedencedeterminesthegroupingoftermsinanexpression.Thisaffectshowanexpressionisevaluated.Certainoperators
havehigherprecedencethanothersforexample,themultiplicationoperatorhashigherprecedencethantheadditionoperator:
Forexamplex=7+3*2here,xisassigned13,not20becauseoperator*hashigherprecedencethan+,soitfirstgetsmultiplied
with3*2andthenaddsinto7.
Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an
expression,higherprecedenceoperatorswillbeevaluatedfirst.
Operator

Operation

**

exponentiation

+,

identity,negation

*,/

multiplication,division

+,,||

addition,subtraction,concatenation

=,<,>,<=,>=,<>,!=,~=,^=,
ISNULL,LIKE,BETWEEN,IN

comparison

NOT

logicalnegation

AND

conjunction

OR

inclusion

PL/SQLConditions

PL/SQLConditions
Decisionmakingstructuresrequirethattheprogrammerspecifyoneormoreconditionstobeevaluatedortestedbytheprogram,along
withastatementorstatementstobeexecutediftheconditionisdeterminedtobetrue,andoptionally,otherstatementstobeexecuted
iftheconditionisdeterminedtobefalse.

IFTHENStatement
It is the simplest form of IF control statement, frequently used in decision making and changing the control flow of the program
execution.
TheIFstatementassociatesaconditionwithasequenceofstatementsenclosedbythekeywordsTHENandENDIF.Ifthecondition
isTRUE,thestatementsgetexecuted,andiftheconditionisFALSEorNULL,thentheIFstatementdoesnothing.

Syntax:
SyntaxforIFTHENstatementis:
IFconditionTHEN
S;
ENDIF;

WhereconditionisaBooleanorrelationalconditionandSisasimpleorcompoundstatement.ExampleofanIFTHENstatementis:
IF(a<=20)THEN
c:=c+1;
ENDIF;

If the Boolean expression condition evaluates to true, then the block of code inside the if statement will be executed. If Boolean
expressionevaluatestofalse,thenthefirstsetofcodeaftertheendoftheifstatement(aftertheclosingendif)willbeexecuted.

FlowDiagram:

IFTHENELSEStatement
AsequenceofIFTHENstatementscanbefollowedbyanoptionalsequenceofELSEstatements,whichexecutewhentheconditionis
FALSE.

Syntax:
SyntaxfortheIFTHENELSEstatementis:
IFconditionTHEN
S1;
ELSE
S2;
ENDIF;

Where, S1 and S2 are different sequence of statements. In the IFTHENELSE statements, when the test condition is TRUE, the
statementS1isexecutedandS2isskippedwhenthetestconditionisFALSE,thenS1isbypassedandstatementS2isexecuted.For
example,
IFcolor=redTHEN
dbms_output.put_line('Youhavechosenaredcar')
ELSE
dbms_output.put_line('Pleasechooseacolorforyourcar');
ENDIF;

IftheBooleanexpressionconditionevaluatestotrue,thentheifthenblockofcodewillbeexecuted,otherwisetheelseblockofcodewill
beexecuted.

FlowDiagram:

IFTHENELSIFStatement
The IFTHENELSIF statement allows you to choose between several alternatives. An IFTHEN statement can be followed by an
optionalELSIF...ELSEstatement.TheELSIFclauseletsyouaddadditionalconditions.
WhenusingIFTHENELSIFstatementstherearefewpointstokeepinmind.
It'sELSIF,notELSEIF
AnIFTHENstatementcanhavezerooroneELSE'sanditmustcomeafteranyELSIF's.
AnIFTHENstatementcanhavezerotomanyELSIF'sandtheymustcomebeforetheELSE.
OnceanELSIFsucceeds,noneoftheremainingELSIF'sorELSE'swillbetested.

Syntax:
ThesyntaxofanIFTHENELSIFStatementinPL/SQLprogramminglanguageis:
IF(boolean_expression1)THEN
S1;Executeswhenthebooleanexpression1istrue
ELSIF(boolean_expression2)THEN
S2;Executeswhenthebooleanexpression2istrue
ELSIF(boolean_expression3)THEN
S3;Executeswhenthebooleanexpression3istrue
ELSE
S4;executeswhenthenoneoftheaboveconditionistrue
ENDIF;

CASEStatement
Like the IF statement, the CASE statement selects one sequence of statements to execute. However, to select the sequence, the
CASEstatementusesaselectorratherthanmultipleBooleanexpressions.Aselectorisanexpression,whosevalueisusedtoselectone
ofseveralalternatives.

Syntax:
ThesyntaxforcasestatementinPL/SQLis:
CASEselector
WHEN'value1'THENS1;
WHEN'value2'THENS2;
WHEN'value3'THENS3;
...
ELSESn;defaultcase
ENDCASE;

FlowDiagram:

SearchedCASEStatement
ThesearchedCASEstatementhasnoselectoranditsWHENclausescontainsearchconditionsthatgiveBooleanvalues.

Syntax:
ThesyntaxforsearchedcasestatementinPL/SQLis:
CASE
WHENselector='value1'THENS1;
WHENselector='value2'THENS2;
WHENselector='value3'THENS3;
...
ELSESn;defaultcase
ENDCASE;

FlowDiagram:

NestedIFTHENELSEStatements
ItisalwayslegalinPL/SQLprogrammingtonestIFELSEstatements,whichmeansyoucanuseoneIForELSEIF statement inside
anotherIForELSEIFstatement(s).

Syntax:
IF(boolean_expression1)THEN
executeswhenthebooleanexpression1istrue
IF(boolean_expression2)THEN
executeswhenthebooleanexpression2istrue
sequenceofstatements;
ENDIF;
ELSE
executeswhenthebooleanexpression1isnottrue
elsestatements;
ENDIF;

PL/SQLLoops

PL/SQLLoops
There may be a situation when you need to execute a block of code several number of times. In general, statements are executed
sequentially:Thefirststatementinafunctionisexecutedfirst,followedbythesecond,andsoon.
Programminglanguagesprovidevariouscontrolstructuresthatallowformorecomplicatedexecutionpaths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop
statementinmostoftheprogramminglanguages:

BasicLoopStatement
Basic loop structure encloses sequence of statements in between the LOOP and END LOOP statements. With each iteration, the
sequenceofstatementsisexecutedandthencontrolresumesatthetopoftheloop.

Syntax:
ThesyntaxofabasicloopinPL/SQLprogramminglanguageis:
LOOP
Sequenceofstatements;
ENDLOOP;

Here,sequenceofstatement(s)maybeasinglestatementorablockofstatements.AnEXITstatementoranEXITWHENstatementis
requiredtobreaktheloop.

WHILELOOPStatement
AWHILELOOP statement in PL/SQL programming language repeatedly executes a target statement as long as a given condition is
true.

Syntax:
WHILEconditionLOOP
sequence_of_statements
ENDLOOP;

FORLOOPStatement
AFORLOOPisarepetitioncontrolstructurethatallowsyoutoefficientlywritealoopthatneedstoexecuteaspecificnumberoftimes.

Syntax:
FORcounterINinitial_value..final_valueLOOP
sequence_of_statements;
ENDLOOP;

FollowingaresomespecialcharacteristicsofPL/SQLforloop:
Theinitial_valueandfinal_value of the loop variable or counter can be literals, variables, or expressions but must evaluate to
numbers.Otherwise,PL/SQLraisesthepredefinedexceptionVALUE_ERROR.
Theinitial_valueneednottobe1however,theloopcounterincrement(ordecrement)mustbe1.
PL/SQLallowsdeterminethelooprangedynamicallyatruntime.

NestedLoops
PL/SQLallowsusingoneloopinsideanotherloop.Followingsectionshowsfewexamplestoillustratetheconcept.

ThesyntaxforanestedbasicLOOPstatementinPL/SQLisasfollows:
LOOP
Sequenceofstatements1
LOOP
Sequenceofstatements2
ENDLOOP;
ENDLOOP;

ThesyntaxforanestedFORLOOPstatementinPL/SQLisasfollows:
FORcounter1INinitial_value1..final_value1LOOP
sequence_of_statements1
FORcounter2INinitial_value2..final_value2LOOP
sequence_of_statements2
ENDLOOP;
ENDLOOP;

ThesyntaxforanestedWHILELOOPstatementinPascalisasfollows:
WHILEcondition1LOOP
sequence_of_statements1
WHILEcondition2LOOP
sequence_of_statements2
ENDLOOP;
ENDLOOP;

EXITStatement
TheEXITstatementinPL/SQLprogramminglanguagehasfollowingtwousages:
WhentheEXITstatementisencounteredinsidealoop,theloopisimmediatelyterminatedandprogramcontrolresumesatthe
nextstatementfollowingtheloop.
Ifyouareusingnestedloops(i.e.oneloopinsideanotherloop),theEXITstatementwillstoptheexecutionoftheinnermostloop
andstartexecutingthenextlineofcodeaftertheblock.

Syntax:
ThesyntaxforaEXITstatementinPL/SQLisasfollows:
EXIT;

FlowDiagram:

CONTINUEStatement
TheCONTINUEstatementcausesthelooptoskiptheremainderofitsbodyandimmediatelyretestitsconditionpriortoreiterating.In
otherwords,itforcesthenextiterationofthelooptotakeplace,skippinganycodeinbetween.

Syntax:
ThesyntaxforaCONTINUEstatementisasfollows:
CONTINUE;

FlowDiagram:

GOTOStatement
AGOTOstatementinPL/SQLprogramminglanguageprovidesanunconditionaljumpfromtheGOTOtoalabeledstatementinthesame
subprogram.
NOTE:UseofGOTOstatementishighlydiscouragedinanyprogramminglanguagebecauseitmakesdifficulttotracethecontrolflowof
a program, making the program hard to understand and hard to modify. Any program that uses a GOTO can be rewritten so that it
doesn'tneedtheGOTO.

Syntax:
ThesyntaxforaGOTOstatementinPL/SQLisasfollows:
GOTOlabel;
..
..
<<label>>
statement;

FlowDiagram:

PL/SQLStrings
The string in PL/SQL is actually a sequence of characters with an optional size specification. The characters could be numeric, letters,
blank,specialcharactersoracombinationofall.PL/SQLoffersthreekindsofstrings:
Fixedlengthstrings:Insuchstrings,programmersspecifythelengthwhiledeclaringthestring.Thestringisrightpaddedwith
spacestothelengthsospecified.
Variablelength strings: In such strings, a maximum length up to 32,767, for the string is specified and no padding takes
place.
Characterlargeobjects(CLOBs):Thesearevariablelengthstringsthatcanbeupto128terabytes.
PL/SQLstringscouldbeeithervariablesorliterals.Astringliteralisenclosedwithinquotationmarks.Forexample,
'Thisisastringliteral.'Or'helloworld'

Toincludeasinglequoteinsideastringliteral,youneedtotypetwosinglequotesnexttooneanother,like:
'thisisn''twhatitlookslike'

DeclaringStringVariables

Oracledatabaseprovidesnumerousstringdatatypes,like,CHAR,NCHAR,VARCHAR2,NVARCHAR2,CLOB,andNCLOB.Thedatatypes
prefixedwithan'N'are'nationalcharacterset'datatypes,thatstoreUnicodecharacterdata.
Ifyouneedtodeclareavariablelengthstring,youmustprovidethemaximumlengthofthatstring.Forexample,theVARCHAR2data
type.Thefollowingexampleillustratesdeclaringandusingsomestringvariables:
DECLARE
namevarchar2(20);
companyvarchar2(30);
introductionclob;
choicechar(1);
BEGIN
name:='JohnSmith';
company:='Infotech';
introduction:='Hello!I''mJohnSmithfromInfotech.';
choice:='y';
IFchoice='y'THEN
dbms_output.put_line(name);
dbms_output.put_line(company);
dbms_output.put_line(introduction);
ENDIF;
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
JohnSmith
InfotechCorporation
Hello!I'mJohnSmithfromInfotech.
PL/SQLproceduresuccessfullycompleted

Todeclareafixedlengthstring,usetheCHARdatatype.Hereyoudonothavetospecifyamaximumlengthforafixedlengthvariable.If
youleaveoffthelengthconstraint,OracleDatabaseautomaticallyusesamaximumlengthrequired.Sofollowingtwodeclarationsbelow
areidentical:
red_flagCHAR(1):='Y';
red_flagCHAR:='Y';

PL/SQLArrays
PL/SQL programming language provides a data structure called the VARRAY, which can store a fixedsize sequential collection of
elementsofthesametype.Avarrayisusedtostoreanorderedcollectionofdata,butitisoftenmoreusefultothinkofanarrayasa
collectionofvariablesofthesametype.
Allvarraysconsistofcontiguousmemorylocations.Thelowestaddresscorrespondstothefirstelementandthehighestaddresstothe
lastelement.

An array is a part of collection type data and it stands for variablesize arrays. We will study other collection types in a later chapter
'PL/SQLCollections'.
Eachelementinavarrayhasanindexassociatedwithit.Italsohasamaximumsizethatcanbechangeddynamically.

CreatingaVarrayType
AvarraytypeiscreatedwiththeCREATETYPEstatement.Youmustspecifythemaximumsizeandthetypeofelementsstoredinthe
varray.
ThebasicsyntaxforcreatingaVRRAYtypeattheschemalevelis:
CREATEORREPLACETYPEvarray_type_nameISVARRAY(n)of<element_type>

Where,
varray_type_nameisavalidattributename,
nisthenumberofelements(maximum)inthevarray,
element_typeisthedatatypeoftheelementsofthearray.
MaximumsizeofavarraycanbechangedusingtheALTERTYPEstatement.
Forexample,
CREATEOrREPLACETYPEnamearrayASVARRAY(3)OFVARCHAR2(10);
/

Typecreated.

ThebasicsyntaxforcreatingaVRRAYtypewithinaPL/SQLblockis:
TYPEvarray_type_nameISVARRAY(n)of<element_type>

Forexample:
TYPEnamearrayISVARRAY(5)OFVARCHAR2(10);
TypegradesISVARRAY(5)OFINTEGER;

PL/SQLProcedures
Asubprogram is a program unit/module that performs a particular task. These subprograms are combined to form larger programs.
Thisisbasicallycalledthe'Modulardesign'.Asubprogramcanbeinvokedbyanothersubprogramorprogramwhichiscalledthecalling
program.
Asubprogramcanbecreated:
Atschemalevel
Insideapackage
InsideaPL/SQLblock
Aschemalevelsubprogramisastandalonesubprogram.ItiscreatedwiththeCREATEPROCEDUREorCREATEFUNCTIONstatement.
ItisstoredinthedatabaseandcanbedeletedwiththeDROPPROCEDUREorDROPFUNCTIONstatement.
A subprogram created inside a package is a packaged subprogram. It is stored in the database and can be deleted only when the
packageisdeletedwiththeDROPPACKAGEstatement.Wewilldiscusspackagesinthechapter'PL/SQLPackages'.
PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of parameters. PL/SQL provides two kinds of
subprograms:
Functions:thesesubprogramsreturnasinglevalue,mainlyusedtocomputeandreturnavalue.
Procedures:thesesubprogramsdonotreturnavaluedirectly,mainlyusedtoperformanaction.
ThischapterisgoingtocoverimportantaspectsofaPL/SQLprocedureandwewillcoverPL/SQLfunctioninnextchapter.

PartsofaPL/SQLSubprogram
Each PL/SQL subprogram has a name, and may have a parameter list. Like anonymous PL/SQL blocks and, the named blocks a
subprogramswillalsohavefollowingthreeparts:
S.N.

Parts&Description

DeclarativePart

It is an optional part. However, the declarative part for a subprogram does not start with the DECLARE keyword. It contains
declarations of types, cursors, constants, variables, exceptions, and nested subprograms. These items are local to the
subprogramandceasetoexistwhenthesubprogramcompletesexecution.
2

ExecutablePart

Thisisamandatorypartandcontainsstatementsthatperformthedesignatedaction.
3

Exceptionhandling

Thisisagainanoptionalpart.Itcontainsthecodethathandlesruntimeerrors.

CreatingaProcedure
A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The simplified syntax for the CREATE OR REPLACE
PROCEDUREstatementisasfollows:
CREATE[ORREPLACE]PROCEDUREprocedure_name
[(parameter_name[IN|OUT|INOUT]type[,...])]
{IS|AS}
BEGIN
<procedure_body>
ENDprocedure_name;

Where,
procedurenamespecifiesthenameoftheprocedure.

[ORREPLACE]optionallowsmodifyinganexistingprocedure.
The optional parameter list contains name, mode and types of the parameters. IN represents that value will be passed from
outsideandOUTrepresentsthatthisparameterwillbeusedtoreturnavalueoutsideoftheprocedure.
procedurebodycontainstheexecutablepart.
TheASkeywordisusedinsteadoftheISkeywordforcreatingastandaloneprocedure.

PL/SQLFunctions
APL/SQLfunctionissameasaprocedureexceptthatitreturnsavalue.

CreatingaFunction
AstandalonefunctioniscreatedusingtheCREATEFUNCTIONstatement.ThesimplifiedsyntaxfortheCREATEORREPLACEPROCEDURE
statementisasfollows:
CREATE[ORREPLACE]FUNCTIONfunction_name
[(parameter_name[IN|OUT|INOUT]type[,...])]
RETURNreturn_datatype
{IS|AS}
BEGIN
<function_body>
END[function_name];

Where,
functionnamespecifiesthenameofthefunction.
[ORREPLACE]optionallowsmodifyinganexistingfunction.
The optional parameter list contains name, mode and types of the parameters. IN represents that value will be passed from
outsideandOUTrepresentsthatthisparameterwillbeusedtoreturnavalueoutsideoftheprocedure.
RETURNclausespecifiesthatdatatypeyouaregoingtoreturnfromthefunction.
functionbodycontainstheexecutablepart.
functionbodymustcontainaRETURNstatement.
TheASkeywordisusedinsteadoftheISkeywordforcreatingastandalonefunction.

PL/SQLCursors
Oracle creates a memory area, known as context area, for processing an SQL statement, which contains all information needed for
processingthestatement,forexample,numberofrowsprocessed,etc.
A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A cursor holds the rows (one or more)
returnedbyaSQLstatement.Thesetofrowsthecursorholdsisreferredtoastheactiveset.
YoucannameacursorsothatitcouldbereferredtoinaprogramtofetchandprocesstherowsreturnedbytheSQLstatement,oneata
time.Therearetwotypesofcursors:
Implicitcursors
Explicitcursors

ImplicitCursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there is no explicit cursor for the
statement.Programmerscannotcontroltheimplicitcursorsandtheinformationinit.
WheneveraDMLstatement(INSERT,UPDATEandDELETE)isissued,animplicitcursorisassociatedwiththisstatement.ForINSERT
operations,thecursorholdsthedatathatneedstobeinserted.ForUPDATEandDELETEoperations,thecursoridentifiestherowsthat
wouldbeaffected.
InPL/SQL,youcanrefertothemostrecentimplicitcursorastheSQLcursor,whichalwayshastheattributeslike%FOUND,%ISOPEN,
%NOTFOUND,and%ROWCOUNT.TheSQLcursorhasadditionalattributes,%BULK_ROWCOUNTand%BULK_EXCEPTIONS,designedfor
usewiththeFORALLstatement.

ExplicitCursors
Explicitcursorsareprogrammerdefinedcursorsforgainingmorecontroloverthecontextarea.Anexplicitcursorshouldbedefinedin
thedeclarationsectionofthePL/SQLBlock.ItiscreatedonaSELECTStatementwhichreturnsmorethanonerow.

Thesyntaxforcreatinganexplicitcursoris:
CURSORcursor_nameISselect_statement;

Workingwithanexplicitcursorinvolvesfoursteps:
Declaringthecursorforinitializinginthememory
Openingthecursorforallocatingmemory
Fetchingthecursorforretrievingdata
Closingthecursortoreleaseallocatedmemory

DeclaringtheCursor
DeclaringthecursordefinesthecursorwithanameandtheassociatedSELECTstatement.Forexample:
CURSORc_customersIS
SELECTid,name,addressFROMcustomers;

OpeningtheCursor
OpeningthecursorallocatesmemoryforthecursorandmakesitreadyforfetchingtherowsreturnedbytheSQLstatementintoit.For
example,wewillopenabovedefinedcursorasfollows:
OPENc_customers;

FetchingtheCursor
Fetchingthecursorinvolvesaccessingonerowatatime.Forexamplewewillfetchrowsfromtheaboveopenedcursorasfollows:
FETCHc_customersINTOc_id,c_name,c_addr;

ClosingtheCursor
Closingthecursormeansreleasingtheallocatedmemory.Forexample,wewillcloseaboveopenedcursorasfollows:
CLOSEc_customers;

Example:
Followingisacompleteexampletoillustratetheconceptsofexplicitcursors:
DECLARE
c_idcustomers.id%type;
c_namecustomers.name%type;
c_addrcustomers.address%type;
CURSORc_customersis
SELECTid,name,addressFROMcustomers;
BEGIN
OPENc_customers;
LOOP
FETCHc_customersintoc_id,c_name,c_addr;
dbms_output.put_line(c_id||''||c_name||''||c_addr);
EXITWHENc_customers%notfound;
ENDLOOP;
CLOSEc_customers;
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
1RameshAhmedabad
2KhilanDelhi
3kaushikKota
4ChaitaliMumbai
5HardikBhopal
6KomalMP
PL/SQLproceduresuccessfullycompleted.

PL/SQLRecords
APL/SQLrecordisadatastructurethatcanholddataitemsofdifferentkinds.Recordsconsistofdifferentfields,similartoarowofa
databasetable.
PL/SQLcanhandlethefollowingtypesofrecords:
Tablebased

Cursorbasedrecords
Userdefinedrecords

TableBasedRecords
The%ROWTYPEattributeenablesaprogrammertocreatetablebasedandcursorbasedrecords.
Thefollowingexamplewouldillustratetheconceptoftablebasedrecords.WewillbeusingtheCUSTOMERStablewehadcreatedand
usedinthepreviouschapters:
DECLARE
customer_reccustomers%rowtype;
BEGIN
SELECT*intocustomer_rec
FROMcustomers
WHEREid=5;
dbms_output.put_line('CustomerID:'||customer_rec.id);
dbms_output.put_line('CustomerName:'||customer_rec.name);
dbms_output.put_line('CustomerAddress:'||customer_rec.address);
dbms_output.put_line('CustomerSalary:'||customer_rec.salary);
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
CustomerID:5
CustomerName:Hardik
CustomerAddress:Bhopal
CustomerSalary:9000
PL/SQLproceduresuccessfullycompleted.

CursorBasedRecords
Thefollowingexamplewouldillustratetheconceptofcursorbasedrecords.WewillbeusingtheCUSTOMERStablewehadcreatedand
usedinthepreviouschapters:
DECLARE
CURSORcustomer_curis
SELECTid,name,address
FROMcustomers;
customer_reccustomer_cur%rowtype;
BEGIN
OPENcustomer_cur;
LOOP
FETCHcustomer_curintocustomer_rec;
EXITWHENcustomer_cur%notfound;
DBMS_OUTPUT.put_line(customer_rec.id||''||customer_rec.name);
ENDLOOP;
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
1Ramesh
2Khilan
3kaushik
4Chaitali
5Hardik
6Komal
PL/SQLproceduresuccessfullycompleted.

UserDefinedRecords
PL/SQL provides a userdefined record type that allows you to define different record structures. Records consist of different fields.
Supposeyouwanttokeeptrackofyourbooksinalibrary.Youmightwanttotrackthefollowingattributesabouteachbook:
Title
Author
Subject
BookID

DefiningaRecord
Therecordtypeisdefinedas:

TYPE
type_nameISRECORD
(field_name1datatype1[NOTNULL][:=DEFAULTEXPRESSION],
field_name2datatype2[NOTNULL][:=DEFAULTEXPRESSION],
...
field_nameNdatatypeN[NOTNULL][:=DEFAULTEXPRESSION);
recordnametype_name;

HereisthewayyouwoulddeclaretheBookrecord:
DECLARE
TYPEbooksISRECORD
(titlevarchar(50),
authorvarchar(50),
subjectvarchar(100),
book_idnumber);
book1books;
book2books;

AccessingFields
To access any field of a record, we use the dot (.) operator. The member access operator is coded as a period between the record
variablenameandthefieldthatwewishtoaccess.Followingistheexampletoexplainusageofrecord:
DECLARE
typebooksisrecord
(titlevarchar(50),
authorvarchar(50),
subjectvarchar(100),
book_idnumber);
book1books;
book2books;
BEGIN
Book1specification
book1.title:='CProgramming';
book1.author:='NuhaAli';
book1.subject:='CProgrammingTutorial';
book1.book_id:=6495407;
Book2specification
book2.title:='TelecomBilling';
book2.author:='ZaraAli';
book2.subject:='TelecomBillingTutorial';
book2.book_id:=6495700;
Printbook1record
dbms_output.put_line('Book1title:'||book1.title);
dbms_output.put_line('Book1author:'||book1.author);
dbms_output.put_line('Book1subject:'||book1.subject);
dbms_output.put_line('Book1book_id:'||book1.book_id);

Printbook2record
dbms_output.put_line('Book2title:'||book2.title);
dbms_output.put_line('Book2author:'||book2.author);
dbms_output.put_line('Book2subject:'||book2.subject);
dbms_output.put_line('Book2book_id:'||book2.book_id);
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Book1title:CProgramming
Book1author:NuhaAli
Book1subject:CProgrammingTutorial
Book1book_id:6495407
Book2title:TelecomBilling
Book2author:ZaraAli
Book2subject:TelecomBillingTutorial
Book2book_id:6495700
PL/SQLproceduresuccessfullycompleted.

PL/SQLExceptions
AnerrorconditionduringaprogramexecutioniscalledanexceptioninPL/SQL.PL/SQLsupportsprogrammerstocatchsuchconditions
using EXCEPTION block in the program and an appropriate action is taken against the error condition. There are two types of
exceptions:
Systemdefinedexceptions
Userdefinedexceptions

SyntaxforExceptionHandling
TheGeneralSyntaxforexceptionhandlingisasfollows.Here,youcanlistdownasmanyasexceptionsyouwanttohandle.Thedefault
exceptionwillbehandledusingWHENothersTHEN:

DECLARE
<declarationssection>
BEGIN
<executablecommand(s)>
EXCEPTION
<exceptionhandlinggoeshere>
WHENexception1THEN
exception1handlingstatements
WHENexception2THEN
exception2handlingstatements
WHENexception3THEN
exception3handlingstatements
........
WHENothersTHEN
exception3handlingstatements
END;

Example
Letuswritesomesimplecodetoillustratetheconcept.WewillbeusingtheCUSTOMERStablewehadcreatedandusedintheprevious
chapters:
DECLARE
c_idcustomers.id%type:=8;
c_namecustomers.name%type;
c_addrcustomers.address%type;
BEGIN
SELECTname,addressINTOc_name,c_addr
FROMcustomers
WHEREid=c_id;
DBMS_OUTPUT.PUT_LINE('Name:'||c_name);
DBMS_OUTPUT.PUT_LINE('Address:'||c_addr);
EXCEPTION
WHENno_data_foundTHEN
dbms_output.put_line('Nosuchcustomer!');
WHENothersTHEN
dbms_output.put_line('Error!');
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Nosuchcustomer!
PL/SQLproceduresuccessfullycompleted.

TheaboveprogramdisplaysthenameandaddressofacustomerwhoseIDisgiven.SincethereisnocustomerwithIDvalue8inour
database,theprogramraisestheruntimeexceptionNO_DATA_FOUNDwhichiscapturedinEXCEPTIONblock.

PL/SQLTriggers
Triggers are stored programs, which are automatically executed or fired when some events occur. Triggers are, in fact, written to be
executedinresponsetoanyofthefollowingevents:
Adatabasemanipulation(DML)statement(DELETE,INSERT,orUPDATE).
Adatabasedefinition(DDL)statement(CREATE,ALTER,orDROP).
Adatabaseoperation(SERVERERROR,LOGON,LOGOFF,STARTUP,orSHUTDOWN).
Triggerscouldbedefinedonthetable,view,schema,ordatabasewithwhichtheeventisassociated.

CreatingTriggers
Thesyntaxforcreatingatriggeris:
CREATE[ORREPLACE]TRIGGERtrigger_name
{BEFORE|AFTER|INSTEADOF}
{INSERT[OR]|UPDATE[OR]|DELETE}
[OFcol_name]
ONtable_name
[REFERENCINGOLDASoNEWASn]
[FOREACHROW]
WHEN(condition)
DECLARE
Declarationstatements
BEGIN
Executablestatements
EXCEPTION
Exceptionhandlingstatements
END;

Where,
CREATE[ORREPLACE]TRIGGERtrigger_name:Createsorreplacesanexistingtriggerwiththetrigger_name.

{BEFORE | AFTER | INSTEAD OF}: This specifies when the trigger would be executed. The INSTEAD OF clause is used for
creatingtriggeronaview.
{INSERT[OR]|UPDATE[OR]|DELETE}:ThisspecifiestheDMLoperation.
[OFcol_name]:Thisspecifiesthecolumnnamethatwouldbeupdated.
[ONtable_name]:Thisspecifiesthenameofthetableassociatedwiththetrigger.
[REFERENCING OLD AS o NEW AS n]: This allows you to refer new and old values for various DML statements, like INSERT,
UPDATE,andDELETE.
[FOREACHROW]:Thisspecifiesarowleveltrigger,i.e.,thetriggerwouldbeexecutedforeachrowbeingaffected.Otherwise
thetriggerwillexecutejustoncewhentheSQLstatementisexecuted,whichiscalledatableleveltrigger.
WHEN (condition): This provides a condition for rows for which the trigger would fire. This clause is valid only for row level
triggers.

Example:
Tostartwith,wewillbeusingtheCUSTOMERStable:
Select*fromcustomers;
++++++
|ID|NAME|AGE|ADDRESS|SALARY|
++++++
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|3|kaushik|23|Kota|2000.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22|MP|4500.00|
++++++

ThefollowingprogramcreatesarowleveltriggerforthecustomerstablethatwouldfireforINSERTorUPDATEorDELETEoperations
performedontheCUSTOMERStable.Thistriggerwilldisplaythesalarydifferencebetweentheoldvaluesandnewvalues:
CREATEORREPLACETRIGGERdisplay_salary_changes
BEFOREDELETEORINSERTORUPDATEONcustomers
FOREACHROW
WHEN(NEW.ID>0)
DECLARE
sal_diffnumber;
BEGIN
sal_diff:=:NEW.salary:OLD.salary;
dbms_output.put_line('Oldsalary:'||:OLD.salary);
dbms_output.put_line('Newsalary:'||:NEW.salary);
dbms_output.put_line('Salarydifference:'||sal_diff);
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Triggercreated.

Herefollowingtwopointsareimportantandshouldbenotedcarefully:
OLDandNEWreferencesarenotavailablefortableleveltriggers,ratheryoucanusethemforrecordleveltriggers.
Ifyouwanttoquerythetableinthesametrigger,thenyoushouldusetheAFTERkeyword,becausetriggerscanquerythetable
orchangeitagainonlyaftertheinitialchangesareappliedandthetableisbackinaconsistentstate.
AbovetriggerhasbeenwritteninsuchawaythatitwillfirebeforeanyDELETEorINSERTorUPDATEoperationonthetable,but
youcanwriteyourtriggeronasingleormultipleoperations,forexampleBEFOREDELETE,whichwillfirewheneverarecordwill
bedeletedusingDELETEoperationonthetable.

PL/SQLPackages
PL/SQLpackagesareschemaobjectsthatgroupslogicallyrelatedPL/SQLtypes,variablesandsubprograms.
Apackagewillhavetwomandatoryparts:
Packagespecification
Packagebodyordefinition

PackageSpecification

Thespecificationistheinterfacetothepackage.ItjustDECLARESthetypes,variables,constants,exceptions,cursors,andsubprograms
that can be referenced from outside the package. In other words, it contains all information about the content of the package, but
excludesthecodeforthesubprograms.
All objects placed in the specification are called public objects. Any subprogram not in the package specification but coded in the
packagebodyiscalledaprivateobject.
The following code snippet shows a package specification having a single procedure. You can have many global variables defined and
multipleproceduresorfunctionsinsideapackage.
CREATEPACKAGEcust_salAS
PROCEDUREfind_sal(c_idcustomers.id%type);
ENDcust_sal;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Packagecreated.

PackageBody
The package body has the codes for various methods declared in the package specification and other private declarations, which are
hiddenfromcodeoutsidethepackage.
The CREATE PACKAGE BODY Statement is used for creating the package body. The following code snippet shows the package body
declaration for the cust_sal package created above. I assumed that we already have CUSTOMERS table created in our database as
mentionedinPL/SQLVariables

chapter.

CREATEORREPLACEPACKAGEBODYcust_salAS
PROCEDUREfind_sal(c_idcustomers.id%TYPE)IS
c_salcustomers.salary%TYPE;
BEGIN
SELECTsalaryINTOc_sal
FROMcustomers
WHEREid=c_id;
dbms_output.put_line('Salary:'||c_sal);
ENDfind_sal;
ENDcust_sal;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Packagebodycreated.

UsingthePackageElements
Thepackageelements(variables,proceduresorfunctions)areaccessedwiththefollowingsyntax:
package_name.element_name;

Consider, we already have created above package in our database schema, the following program uses the find_sal method of the
cust_salpackage:
DECLARE
codecustomers.id%type:=&cc_id;
BEGIN
cust_sal.find_sal(code);
END;
/

When the above code is executed at SQL prompt, it prompt to enter customer ID, and when you enter an ID, it displays the
correspondingsalaryasfollows:
Entervalueforcc_id:1
Salary:3000
PL/SQLproceduresuccessfullycompleted.

PL/SQLCollections
Acollectionisanorderedgroupofelementshavingthesamedatatype.Eachelementisidentifiedbyauniquesubscriptthatrepresents
itspositioninthecollection.
PL/SQLprovidesthreecollectiontypes:
IndexbytablesorAssociativearray
Nestedtable
VariablesizearrayorVarray

IndexByTable
An indexby table (also called an associative array) is a set of keyvalue pairs. Each key is unique, and is used to locate the
correspondingvalue.Thekeycanbeeitheranintegerorastring.
Anindexbytableiscreatedusingthefollowingsyntax.Herewearecreatinganindexbytablenamedtable_namewhosekeyswillbe
ofsubscript_typeandassociatedvalueswillbeofelement_type
TYPEtype_nameISTABLEOFelement_type[NOTNULL]INDEXBYsubscript_type;
table_nametype_name;

Example:
Followingexamplehowtocreateatabletostoreintegervaluesalongwithnamesandlateritprintsthesamelistofnames.
DECLARE
TYPEsalaryISTABLEOFNUMBERINDEXBYVARCHAR2(20);
salary_listsalary;
nameVARCHAR2(20);
BEGIN
addingelementstothetable
salary_list('Rajnish'):=62000;
salary_list('Minakshi'):=75000;
salary_list('Martin'):=100000;
salary_list('James'):=78000;
printingthetable
name:=salary_list.FIRST;
WHILEnameISNOTnullLOOP
dbms_output.put_line
('Salaryof'||name||'is'||TO_CHAR(salary_list(name)));
name:=salary_list.NEXT(name);
ENDLOOP;
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
SalaryofJamesis78000
SalaryofMartinis100000
SalaryofMinakshiis75000
SalaryofRajnishis62000
PL/SQLproceduresuccessfullycompleted.

NestedTables
Anestedtableislikeaonedimensionalarraywithanarbitrarynumberofelements.However,anestedtablediffersfromanarrayin
thefollowingaspects:
Anarrayhasadeclarednumberofelements,butanestedtabledoesnot.Thesizeofanestedtablecanincreasedynamically.
Anarrayisalwaysdensei.e.,italwayshasconsecutivesubscripts.Anestedarrayisdenseinitially,butitcanbecomesparse
whenelementsaredeletedfromit.
Annestedtableiscreatedusingthefollowingsyntax:
TYPEtype_nameISTABLEOFelement_type[NOTNULL];
table_nametype_name;

Thisdeclarationissimilartodeclarationofanindexbytable,butthereisnoINDEXBYclause.
AnestedtablecanbestoredinadatabasecolumnandsoitcouldbeusedforsimplifyingSQLoperationswhereyoujoinasinglecolumn
tablewithalargertable.Anassociativearraycannotbestoredinthedatabase.

Example:
Thefollowingexamplesillustratetheuseofnestedtable:
DECLARE
TYPEnames_tableISTABLEOFVARCHAR2(10);
TYPEgradesISTABLEOFINTEGER;
namesnames_table;
marksgrades;
totalinteger;
BEGIN
names:=names_table('Kavita','Pritam','Ayan','Rishav','Aziz');
marks:=grades(98,97,78,87,92);

total:=names.count;
dbms_output.put_line('Total'||total||'Students');
FORiIN1..totalLOOP
dbms_output.put_line('Student:'||names(i)||',Marks:'||marks(i));
endloop;
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Total5Students
Student:Kavita,Marks:98
Student:Pritam,Marks:97
Student:Ayan,Marks:78
Student:Rishav,Marks:87
Student:Aziz,Marks:92
PL/SQLproceduresuccessfullycompleted.

PL/SQLTransactions
AdatabasetransactionisanatomicunitofworkthatmayconsistofoneormorerelatedSQLstatements.Itiscalledatomicbecause
thedatabasemodificationsbroughtaboutbytheSQLstatementsthatconstituteatransactioncancollectivelybeeithercommitted,i.e.,
madepermanenttothedatabaseorrolledback(undone)fromthedatabase.
AsuccessfullyexecutedSQLstatementandacommittedtransactionarenotsame.EvenifanSQLstatementisexecutedsuccessfully,
unlessthetransactioncontainingthestatementiscommitted,itcanberolledbackandallchangesmadebythestatement(s)canbe
undone.

StartinganEndingaTransaction
Atransactionhasabeginningandanend.Atransactionstartswhenoneofthefollowingeventstakeplace:
ThefirstSQLstatementisperformedafterconnectingtothedatabase.
AteachnewSQLstatementissuedafteratransactioniscompleted.
Atransactionendswhenoneofthefollowingeventstakeplace:
ACOMMIToraROLLBACKstatementisissued.
ADDLstatement,likeCREATETABLEstatement,isissuedbecauseinthatcaseaCOMMITisautomaticallyperformed.
ADCLstatement,suchasaGRANTstatement,isissuedbecauseinthatcaseaCOMMITisautomaticallyperformed.
Userdisconnectsfromthedatabase.
UserexitsfromSQL*PLUSbyissuingtheEXITcommand,aCOMMITisautomaticallyperformed.
SQL*Plusterminatesabnormally,aROLLBACKisautomaticallyperformed.
ADMLstatementfailsinthatcaseaROLLBACKisautomaticallyperformedforundoingthatDMLstatement.

CommittingaTransaction
AtransactionismadepermanentbyissuingtheSQLcommandCOMMIT.ThegeneralsyntaxfortheCOMMITcommandis:
COMMIT;

RollingBackTransactions
ChangesmadetothedatabasewithoutCOMMITcouldbeundoneusingtheROLLBACKcommand.
ThegeneralsyntaxfortheROLLBACKcommandis:
ROLLBACK[TOSAVEPOINT<savepoint_name>];

When a transaction is aborted due to some unprecedented situation, like system failure, the entire transaction since a commit is
automaticallyrolledback.Ifyouarenotusingsavepoiny,thensimplyusethefollowingstatementtorollbackallthechanges:
ROLLBACK;

Savepoints
Savepoints are sort of markers that help in splitting a long transaction into smaller units by setting some checkpoints. By setting
savepointswithinalongtransaction,youcanrollbacktoacheckpointifrequired.ThisisdonebyissuingtheSAVEPOINTcommand.

ThegeneralsyntaxfortheSAVEPOINTcommandis:
SAVEPOINT<savepoint_name>;

AutomaticTransactionControl
To execute a COMMIT automatically whenever an INSERT, UPDATE or DELETE command is executed, you can set the AUTOCOMMIT
environmentvariableas:
SETAUTOCOMMITON;

Youcanturnoffautocommitmodeusingthefollowingcommand:
SETAUTOCOMMITOFF;

PL/SQLDate&Time
PL/SQLprovidestwoclassesofdateandtimerelateddatatypes:
Datetimedatatypes
Intervaldatatypes
TheDatetimedatatypesare:
DATE
TIMESTAMP
TIMESTAMPWITHTIMEZONE
TIMESTAMPWITHLOCALTIMEZONE
TheIntervaldatatypesare:
INTERVALYEARTOMONTH
INTERVALDAYTOSECOND

TheDatetimeDataTypes
FollowingaretheDatetimedatatypes:
DATEitstoresdateandtimeinformationinbothcharacterandnumberdatatypes.Itismadeofinformationoncentury,year,
month,date,hour,minute,andsecond.Itisspecifiedas:
TIMESTAMPitisanextensionoftheDATEdatatype.Itstorestheyear,month,anddayoftheDATEdatatype,alongwithhour,
minute,andsecondvalues.Itisusefulforstoringprecisetimevalues.
TIMESTAMPWITHTIMEZONEitisavariantofTIMESTAMPthatincludesatimezoneregionnameoratimezoneoffsetin
itsvalue.Thetimezoneoffsetisthedifference(inhoursandminutes)betweenlocaltimeandUTC.Thisdatatypeisusefulfor
collectingandevaluatingdateinformationacrossgeographicregions.
TIMESTAMPWITHLOCALTIMEZONEitisanothervariantofTIMESTAMPthatincludesatimezoneoffsetinitsvalue.

Examples:
SELECTSYSDATEFROMDUAL;

Output:
08/31/20125:25:34PM
SELECTTO_CHAR(CURRENT_DATE,'DDMMYYYYHH:MI:SS')FROMDUAL;

Output:
3108201205:26:14
SELECTADD_MONTHS(SYSDATE,5)FROMDUAL;

Output:
01/31/20135:26:31PM
SELECTLOCALTIMESTAMPFROMDUAL;

Output:
8/31/20125:26:55.347000PM

TheIntervalDataTypes
FollowingaretheIntervaldatatypes:
INTERVALYEARTOMONTHitstoresaperiodoftimeusingtheYEARandMONTHdatetimefields.
INTERVALDAYTOSECONDitstoresaperiodoftimeintermsofdays,hours,minutes,andseconds.

PL/SQLDBMSOutput
TheDBMS_OUTPUTisabuiltinpackagethatenablesyoutodisplayoutput,displaydebugginginformation,andsendmessagesfrom
PL/SQLblocks,subprograms,packages,andtriggers.

Example:
DECLARE
linesdbms_output.chararr;
num_linesnumber;
BEGIN
enablethebufferwithdefaultsize20000
dbms_output.enable;

dbms_output.put_line('HelloReader!');
dbms_output.put_line('Hopeyouhaveenjoyedthetutorials!');
dbms_output.put_line('Haveagreattimeexploringpl/sql!');

num_lines:=3;

dbms_output.get_lines(lines,num_lines);

FORiIN1..num_linesLOOP
dbms_output.put_line(lines(i));
ENDLOOP;
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
HelloReader!
Hopeyouhaveenjoyedthetutorials!
Haveagreattimeexploringpl/sql!
PL/SQLproceduresuccessfullycompleted.

PL/SQLObjectOriented
PL/SQLallowsdefininganobjecttype,whichhelpsindesigningobjectorienteddatabaseinOracle.Anobjecttypeallowsyoutocrate
compositetypes.Usingobjectsallowyouimplementingrealworldobjectswithspecificstructureofdataandmethodsforoperatingit.
Objectshaveattributesandmethods.Attributesarepropertiesofanobjectandareusedforstoringanobject'sstateandmethodsare
usedformodelingitsbehaviors.
ObjectsarecreatedusingtheCREATE[ORREPLACE]TYPEstatement.Belowisanexampletocreateasimpleaddressobjectconsisting
offewattributes:
CREATEORREPLACETYPEaddressASOBJECT
(house_novarchar2(10),
streetvarchar2(30),
cityvarchar2(20),
statevarchar2(10),
pincodevarchar2(10)
);
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Typecreated.

Let'screateonemoreobjectcustomerwherewewillwrapattributesandmethodstogethertohaveobjectorientedfeeling:
CREATEORREPLACETYPEcustomerASOBJECT
(codenumber(5),
namevarchar2(30),
contact_novarchar2(12),
addraddress,
memberproceduredisplay
);
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:

Typecreated.

InstantiatinganObject
Defininganobjecttypeprovidesablueprintfortheobject.Tousethisobject,youneedtocreateinstancesofthisobject.Youcanaccess
theattributesandmethodsoftheobjectusingtheinstancenameandtheaccessoperator(.)asfollows:
DECLARE
residenceaddress;
BEGIN
residence:=address('103A','M.G.Road','Jaipur','Rajasthan','201301');
dbms_output.put_line('HouseNo:'||residence.house_no);
dbms_output.put_line('Street:'||residence.street);
dbms_output.put_line('City:'||residence.city);
dbms_output.put_line('State:'||residence.state);
dbms_output.put_line('Pincode:'||residence.pincode);
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
HouseNo:103A
Street:M.G.Road
City:Jaipur
State:Rajasthan
Pincode:201301
PL/SQLproceduresuccessfullycompleted.

InheritanceforPL/SQLObjects:
PL/SQLallowscreatingobjectfromexistingbaseobjects.Toimplementinheritance,thebaseobjectsshouldbedeclaredasNOTFINAL.
ThedefaultisFINAL.
ThefollowingprogramsillustrateinheritanceinPL/SQLObjects.LetuscreateanotherobjectnamedTableTopwhichisinheritingfromthe
Rectangleobject.Creatingthebaserectangleobject:
CREATEORREPLACETYPErectangleASOBJECT
(lengthnumber,
widthnumber,
memberfunctionenlarge(incnumber)returnrectangle,
NOTFINALmemberproceduredisplay)NOTFINAL
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Typecreated.

Creatingthebasetypebody:
CREATEORREPLACETYPEBODYrectangleAS
MEMBERFUNCTIONenlarge(incnumber)returnrectangleIS
BEGIN
returnrectangle(self.length+inc,self.width+inc);
ENDenlarge;
MEMBERPROCEDUREdisplayIS
BEGIN
dbms_output.put_line('Length:'||length);
dbms_output.put_line('Width:'||width);
ENDdisplay;
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Typebodycreated.

Creatingthechildobjecttabletop:
CREATEORREPLACETYPEtabletopUNDERrectangle
(
materialvarchar2(20),
OVERRIDINGmemberproceduredisplay
)
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Typecreated.

Creatingthetypebodyforthechildobjecttabletop:
CREATEORREPLACETYPEBODYtabletopAS
OVERRIDINGMEMBERPROCEDUREdisplayIS

BEGIN
dbms_output.put_line('Length:'||length);
dbms_output.put_line('Width:'||width);
dbms_output.put_line('Material:'||material);
ENDdisplay;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Typebodycreated.

Usingthetabletopobjectanditsmemberfunctions:
DECLARE
t1tabletop;
t2tabletop;
BEGIN
t1:=tabletop(20,10,'Wood');
t2:=tabletop(50,30,'Steel');
t1.display;
t2.display;
END;
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Length:20
Width:10
Material:Wood
Length:50
Width:30
Material:Steel
PL/SQLproceduresuccessfullycompleted.

AbstractObjectsinPL/SQL
The NOT INSTANTIABLE clause allows you to declare an abstract object. You cannot use an abstract object as it is you will have to
createasubtypeorchildtypeofsuchobjectstouseitsfunctionalities.
Forexample,
CREATEORREPLACETYPErectangleASOBJECT
(lengthnumber,
widthnumber,
NOTINSTANTIABLENOTFINALMEMBERPROCEDUREdisplay)
NOTINSTANTIABLENOTFINAL
/

WhentheabovecodeisexecutedatSQLprompt,itproducesthefollowingresult:
Typecreated.

PreviousPage

NextPage
Advertisements

StopLivinginaPG
FullyFurnishedHomefromRs.5000No
Brokerage,Only2MonthsDeposit
nestaway.com/PG_Rent

Write for us

FAQ's

Helping

Contact

Copyright 2016. All Rights Reserved.


Enter email for newsletter

go

You might also like