Vous êtes sur la page 1sur 18

4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

Teradata SQL

PREV NEXT

Chapter 21 - Data Manipulation Language (DML) Chapter 23 - Trigger Functions

Chapter 22 - Stored Procedure Functions


Freedomfromeffortinthepresentmerelymeansthattherehasbeen
effortstoredupinthepast.

TheodoreRoosevelt

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 1/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

StoredProceduresvs.Macros

Macros StoredProcedures

ContainsSQL ContainsSQL

Maycontain Containscomprehensive
BTEQDot SPL
commands
Parametervaluescanbe
Parameter passedtoit
valuescanbe
Mustuseacursorto
passed
retrieve>than1row
Mayretrieve1
StoredinDATABASEor
ormorerows
USERPERM
StoredinDBC
Mayreturn1ormore
PERMSpace
valuestoclientas
Returnsrowsto parameter
theclient

StoredProceduresarealotlikeMacros.However,theymanipulatedata
arowatatime.StoredProcedurestakeupPERMSpace,unlikeViews
andMacrosthatdoNOT.StoredProceduresareactuallycompiledand
willuseaCursortoretrieveormanipulatemorethanonerow.Although
StoredProceduresutilizeSQL,theyalsoutilizeSPL,whichstandsfor
StoredProcedureLanguage,whichprovidesloops,while,etc.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 2/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

CreatingaStoredProcedure

THEBEGINandENDstatementsarerequiredinallStoredProcedures.
Don'tmissasemicolon.Theyareeverywhere.Ihaveboldedthemfor
yourconvenience.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 3/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

HowyouCALLaStoredProcedure

CREATEPROCEDUREFirst_Procedure()
BEGIN
INSERTINTOCustomer_TableDEFAULT
VALUES
END

SELECT*FROMCustomer_TableORDERBY1

YouSELECTfromaView,EXECUTEaMacro,andyouCALLaStored
Procedure.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 4/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

LabelallBEGINandENDstatementsexceptthefirstones

CALLSecond_Procedure()

WhenyouhavemultipleBEGINandENDstatements,youhavetolabel
themall(exceptforthefirstBEGINandENDstatements).Wehave
labeledournextsetofBEGINandENDSecondSection.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 5/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

HowtoDeclareaVariable

CALLDeclare_Procedure()

WhenyouDECLAREavariable,andthenreferencethatvariablelater,a
colonisalwaysinfrontoftheVariable.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 6/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

HowtoDeclareaVariableandthenSETtheVariable

CALLSetVar_Procedure()

Onceavariableandthedatatypeisdefined,thevaluemustbeassigned.
SETisthemoreflexibleamethodcomparedtoDEFAULT.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 7/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

AnINVariableispassedtotheProcedureduringtheCALL

CALLPassInput_Procedure(31323134)

TheVariableVar1wasnotassignedwiththeDEFAULTortheSET,but
insteadpassedasaparameter.Therearethreetypesofparameters(IN,
OUT,INOUT).Inthisexample,anINisbeingused.Warning:You
cannotadd,subtract,orchangeanINvariable.Yousetitwhenyoucall
theprocedureandthatvalueremainsconstant.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 8/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

TheIN,OUTandINOUTParameters

Msg

Therearethreetypesofparameters(IN,OUT,INOUT).Thisisan
exampleofanINandanOUTparameter.WhatthatmeansisthisStored
Procedurewilltakeaparameterin,andthenspitsomethingout.Notice
thatwenamedtheOUTparameterMsg,andthenweneededtoputthe
nameMsginourCallstatement.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 9/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

UsingIFinsideaStoredProcedure

CREATEPROCEDURETestIF_Proc
(INvar1BYTEINT,INvar2BYTEINT,OUTMsg
CHAR(20))
BEGIN
IFvar1=var2THENSETMsg='Theyare
equal'
ENDIF
IFvar1<var2THENSETMsg='Variable1
less'
ENDIF
IFvar1>var2THENSETMsg='Variable1
greater'
ENDIF
END

CALLTestIF_Proc(2,2,Msg)

Msg

Theyareequal

WhydidtheMsgsayTheyareequal?

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 10/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

ExampleoftwoStoredProcedureswithdifferenttechniques

CREATEPROCEDURETestELSE_Proc
(INvar1BYTEINT,INvar2BYTEINT,
OUTMsgCHAR(20))
BEGIN
IFvar1=var2THEN
SETMsg='Theyareequal'

ELSEIFvar1<var2THEN

SETMsg='Variable1less'

ELSE

SETMsg='Variable1greater'
ENDIF
END

CREATEPROCEDURETestIF_Proc
(INvar1BYTEINT,INvar2
BYTEINT,
OUTMsgCHAR(20))
BEGIN

IFvar1=var2THEN
SETMsg='Theyareequal'
ENDIF

IFvar1<var2THEN
SETMsg='Variable1less'
ENDIF
IFvar1>var2THEN
SETMsg='Variable1greater'
ENDIF

END

ThesequeriesdotheSAMEthing.However,thefirstoneismore
efficientbecauseitonlydoesTWOcalculationsinsteadofthree.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 11/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

UsingLoopsinStoredProcedures

CREATETableMy_Log_Tbl
(
CntrInteger
,TheTimeTime
)PrimaryIndex(Cntr)

CALLInserter_Five()

LOOPsrequireLabeling.Muchlikewhenyouhavemorethanone
BEGIN/END.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 12/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

YoucanNametheFirstBeginandEndifyouchoose

CREATETableMy_Log_Tbl
(
CntrInteger
,TheTimeTime
)PrimaryIndex(Cntr)

Firstwehaveto
CREATEtheTable.

CREATEPROCEDUREInserter_Five()
LOOPER:BEGIN
DECLARECntrINTEGERDEFAULT0
Loopit:LOOP

SETCntr=Cntr+1
IFCntr>5THENLEAVELoopit
ENDIF
INSERTINTOMy_Log_Tbl
VALUES(:Cntr,TIME)
ENDLOOPLoopit

ENDLOOPER

Thenwebuildthe
StoredProcedure

Nowwecallthe
StoredProcedure

CALLInserter_Five()

Thisloops5times!Wedidn'thavetolabelLooperbecauseit'sthefirst
BeginandEnd.TheLEAVEstatementishowtheLOOPistolditisdone
looping.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 13/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

UsingKeywordsLEAVEvs.UNTILforLEAVEvs.REPEAT

ProcedureOne

CREATEPROCEDUREIns5()
LOOPER:BEGIN
DECLARECntrINTEGER

DEFAULT0

Loopit:LOOP
SETCntr=Cntr+1
IFCntr>5THENLEAVELoopit
ENDIF
INSERTINTOMy_Log_Tbl

VALUES(:Cntr,TIME)

ENDLOOPLoopit
ENDLOOPER

BothProceduresabovedothesamething.TheUNTILkeywordin
ProcedureTwojumpsitoutoftheREPEATLooponceitreachestheCntr
,andtheproceduremoveson.Therearesomedifferencesintheabove.
Thefirstexample(ProcedureOne),testsCntrbeforetheINSERT.But
Proceduretwodoesnot,soProceduretwowillalwaysdoatleastone
INSERT,nomatterwhatCntrissetat.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 14/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

StoredProcedureBasicAssignment

CreatethetablebelowandsubstitutetheXYZwithyourinitials.

CREATEMULTISETTableSQL01.InsProcXYZ
(Col1INTEGER
,Col2INTEGER
)PrimaryIndex(Col1)

Now,createastoredprocedurecalledInsertXYZthatplaces1,000rows
insidethetable.Col1shouldhave1000uniquevalues,andCol2should
have250differentvalues.

Turnthepageifyouneedsomehelp.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 15/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

AnswerStoredProcedureBasicAssignment

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 16/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

StoredProcedureAdvancedAssignment

CreatebothtablesbelowandsubstitutetheXYZwithyourinitials.

CREATEMULTISET CREATEMULTISET
Table Table
SQL01.InsProc2XYZ SQL01.InsProc
(Col1INTEGER (Col1INTEGER
,Col2INTEGER ,Col2INTEGER
)PrimaryIndex )PrimaryIndex
(Col1) (Col1)

Then,createastoredprocedurecalledAdvInsXYZthatplaces1,000rows
insidebothtables.

InInsProc2XYZ,thecolumnCol1shouldhave500differentvalues,and
Col2shouldhave100differentvalues.

InInsProc3XYZ,thecolumnCol1shouldhave200differentvalues,and
Col2shouldhave40differentvalues.

Turnthepageifyouneedsomehelp.

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 17/18
4/7/2017 Chapter22StoredProcedureFunctionsTeradataSQL

AnswerStoredAdvancedAssignment

PREV NEXT

Recommended / Queue
Chapter 21 - Data / History / Language
Manipulation Topics / Tutorials
(DML) / Settings / Blog / Get the App / Sign Out Chapter 23 - Trigger Functions
2017 Safari. Terms of Service / Privacy Policy

Enjoy Safari? Subscribe Today

https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter22.xhtml 18/18

Vous aimerez peut-être aussi