InVersion9.1,DB2 issuesdifferentmessagesforthenewnativeSQLprocedures thanitdoesforexternal SQLprocedures.ForexternalSQLprocedures,DB2 continuestoissue DSNHxxxxmessages.FornativeSQLprocedures,DB2 issues SQLreturncodes.Therelationshipbetweenthesemessages isshowninthe followingtable:
Table1.RelationshipbetweenDSNHxxxxmessagesthatareissuedforexternalSQL proceduresandSQLCODEsthatareissuedfornativeSQLprocedures
DSNHxxxxmessage1 SQLCODE2
DSNH051I -051
DSNH385I +385
DSNH590I -590
DSNH4408I -408
DSNH4777I -777
DSNH4778I -778
DSNH4779I -779
DSNH4780I -780
DSNH4781I -781
DSNH4782I -782
DSNH4785I -785
DSNH4787I -787
Notes:
1. ThesemessagesareusedforexternalSQLprocedures,whichcanbedefinedby specifyingEXTERNALorFENCED inVersion9.1.
2. ThesemessagesareusedfornativeSQLproceduresinVersion9.1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
||
||
||
||
||
||
||
||
||
||
||
||
||
|
|
|
|
|
Part 1. SQL queries
Chapter1.RetrievingdatabyusingtheSELECT statement . . . 3 Resulttables . . . 3 Datatypes . . . 4 Selectingcolumns:SELECT . . . 6 Selectingallcolumns:SELECT* . . . 6 Selectingsomecolumns:SELECTcolumn-name . 7 Selectingderivedcolumns:SELECTexpression . . 7 SelectingXMLdata . . . 8 Eliminatingredundantduplicaterows:DISTINCT 8 Namingresultcolumns:AS . . . 9 Selectingrowsusingsearchconditions:WHERE . . 10
CheckingwhetheranXMLcolumncontainsa certainvalue . . . 11 Puttingtherowsinorder:ORDERBY . . . 12 SpecifyingthesortkeyintheORDERBYclause 12 ReferencingderivedcolumnsintheORDERBY clause . . . 13 Returningfullselectresultsintheorderofthe subqueryresulttable . . . 13 Summarizinggroupvalues:GROUPBY . . . 14 Subjectinggroupstoconditions:HAVING . . . . 14 CombiningresulttablesfrommultipleSELECT
statements:UNION,EXCEPT,andINTERSECT . . 15 Eliminatingredundantduplicaterowswhen
combiningresulttables . . . 17 Keepingallduplicaterowswhencombining
resulttables . . . 18 Findingtherowsthatwerechangedwithina
specifiedperiodoftime . . . 20 Determiningwhenarowwaschanged . . . 20 Rankingtherows:RANKandDENSE_RANK. . . 21 Numberingtherowsinaresulttable:
ROW_NUMBER. . . 23 Commontableexpressions:WITH. . . 24 Usingcommontableexpressionsinsteadofviews 24 Usingcommontableexpressionswithviews . . 25 Usingcommontableexpressionswhenyouuse INSERT. . . 25 RecursiveSQL . . . 25 DB2datathatisnotinatable . . . 26 Precisionforoperationswithdecimalnumbers . . 26 Avoidingdecimalarithmeticerrors . . . 27 ControllinghowDB2roundsdecimalfloating pointnumbers . . . 28 FindinginformationintheDB2catalog . . . 28 Displayingalistoftablesthatyoucanuse . . . 28 Displayingalistofcolumnsinatable . . . . 28
Chapter2.WorkingwithDB2tablesand
modifyingdata. . . 31 Workingwithtables . . . 31 Creatingyourowntables:CREATETABLE. . . 31
Identifyingcolumndefaultsandconstraining columninputs . . . 31 Worktables . . . 32
Temporarytables . . . 33 Workingwithcreatedtemporarytables . . . 34 Creatingdeclaredtemporarytables . . . . 35 Droppingtables:DROPTABLE. . . 37 Workingwithviews . . . 37 Definingaview:CREATEVIEW . . . 37 Restrictionswhenchangingdatathroughaview 39 Droppingviews:DROPVIEW . . . 39 ModifyingDB2data . . . 39 Insertingrows:INSERT . . . 39 Insertingasinglerow . . . 40 Insertingrowsintoatablefromanothertable 41 Otherwaystoinsertdata. . . 42 InsertingdataintoaROWIDcolumn. . . . 42 Insertingdataintoanidentitycolumn . . . 43 Updatingcurrentvalues:UPDATE . . . 44 Updatingexistingdataandinsertingnewdata:
MERGE . . . 45 Deletingrows:DELETEandTRUNCATE . . . 46 Deletingeveryrowinatable . . . 46 SelectingmodifiedDB2data. . . 47
Selectingvaluesasyouinsert:SELECTfrom
INSERT. . . 47 ResulttableoftheINSERToperation. . . . 48 Selectingvalueswhenyouinsertasinglerow 49 Selectingvalueswhenyouinsertdataintoa view. . . 49 Selectingvalueswhenyouinsertmultiple
rows. . . 49 Selectinganadditionalcolumnwhenyou
insertdata. . . 50 Resulttableofthecursorwhenyouinsert multiplerows . . . 50 Whathappensifanerroroccurs . . . 52 Selectingvaluesasyouupdate:SELECTFROM UPDATE . . . 53 Selectingvaluesasyoumerge:SELECTFROM MERGE . . . 53 Selectingvaluesasyoudelete:SELECTFROM DELETE . . . 55
Chapter3.Howtojoindatafrommorethanone DB2table. . . 57 Sampledataforjoins . . . 57 Innerjoin . . . 57 Outerjoin . . . 59 Fullouterjoin . . . 60 Leftouterjoin . . . 61 Rightouterjoin . . . 62 SQLrulesforstatementscontainingjoinoperations 63 Joiningmorethantwotables . . . 64
Joiningmorethantwotablesbyusingmorethan onejointype . . . 64 Nestedtableexpressionsanduser-definedtable
functionsinjoins . . . 65 Correlatedreferencesintablespecificationsinjoins 67
||
|
||
|
||
|
||
|
||
|
||
|
||
||
||
|
||
|
||
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Chapter4.Subqueries . . . 69 Conceptualoverviewofsubqueries . . . 69 Correlatedanduncorrelatedsubqueries . . . . 70 Subqueriesandpredicates . . . 70 Thesubqueryresulttable. . . 71 Howtocodeasubquery . . . 71 Basicpredicateinasubquery . . . 71 Quantifiedpredicateinasubquery:ALL,ANY, orSOME . . . 71
ALLpredicate . . . 72 ANYorSOMEpredicate . . . 72 INpredicateinasubquery . . . 72 EXISTSpredicateinasubquery . . . 72 Correlatedsubqueries . . . 73 Anexampleofacorrelatedsubquery. . . 73 Correlationnamesinreferences. . . 74 UsingcorrelatedsubqueriesinanUPDATE
statement . . . 75 UsingcorrelatedsubqueriesinaDELETE
statement . . . 75 Usingtableswithnoreferentialconstraints. . 76 Usingasingletable. . . 76 Usingtableswithreferentialconstraints . . . 76
Chapter5.ExecutingSQLbyusingSPUFI . . . 79 OpeningSPUFIandspecifyinginitialoptions . . . 79 TheSPUFIpanel . . . 80 ChangingSPUFIdefaults. . . 82 CURRENTSPUFIDEFAULTSpanel . . . 84 CURRENTSPUFIDEFAULTS-PANEL2panel . . 87 EnteringSQLstatementsinSPUFI. . . 88 UnicodeUTF-16graphicdatainSPUFI . . . . 89 CommentsinSPUFI . . . 89 SettingtheSQLterminatorcharacterinaSPUFI inputdataset. . . 89 ControllingtolerationofwarningsinSPUFI . . 90 ProcessingSQLstatementswithSPUFI . . . 91
SQLstatementsthatexceedresourcelimit
thresholds . . . 91 OutputfromSPUFI. . . 93
FormattingrulesforSELECTstatementresultsin SPUFI . . . 93 ContentofthemessagesfromSPUFI . . . 94
Chapter 1. Retrieving data by using the SELECT statement
YoucanretrievedatausingtheSQLSELECT statementtospecifya resulttable.
Thissectiondescribes howtointeractivelyuseSELECTstatementstoretrievedata fromDB2 tables.Itincludesthefollowingtopics:
v