View
217
Download
0
Category
Preview:
Citation preview
8/14/2019 Embedded Sql.rtf
1/20
ERIS (EMBEDDED SQL)
What SQL provides to AS/400 ?
0* SQL run-time support
1* SQL pre-compilers
2* SQL interactive interface
3* Run SQL Statements CL command
4* DB2/ !! Quer" Mana#er
SQL run-time support
SQL run-time parses SQL statements and runs an" SQL statements$ %S/ !!
supports t&is in a'sence of DB2 Quer" Mana#er SQL Development it$ *t is an
inte#ral part of %S/ !!$
SQL pre-compilers
SQL pre-compilers support pre-compilin# em'edded SQL statements in &ost
lan#ua#es$ +&e SQL &ost lan#ua#e pre-compilers prepare an application
pro#ram containin# SQL statements$ +&e &ost lan#ua#e compilers t&en compile t&e
pre-compiled &ost source pro#rams$
8/14/2019 Embedded Sql.rtf
2/20
SQL interactive interface
SQL interactive interface allo,s "ou to create and run SQL statements$
Run SQL statements CL command
R .SQLS+M allo,s "ou to run a series of SQL statements ,&ic& are stored in a
source file$ +&e R .SQLS+M command is part of t&e DB2/ !! Quer" Mana#er and
SQL Development it$
D !/400 Quer" #ana$er
DB2/ !! Quer" Mana#er provides a prompt-driven interactive interface t&at allo,s
"ou to create data add data maintain data and run reports on t&e data'ases$
Quer" Mana#er is part of t&e DB2/ !! Quer" Mana#er and SQL Development it$
Steps in Creatin$ Application pro$ram
5* %ser source code mem&er
0 source code is ,ritten in R1 LE alon# ,it& SQL statements in a source mem'er
usin# SE $
6* 'emporar" Source (ile #em&er
+&is source is pre-compiled usin# CR+SQLR1 * command ,&ic& creates a
8/14/2019 Embedded Sql.rtf
3/20
temporar" source file QSQL+EM13 in Q+EM1 ,&ic& is deleted after 4o' completion$+&is mem'er t&en contains 5
Calls to the SQL run-time support, which have replaced embedded SQLstatements
Parsed and syntax checked SQL statements
7* # odule
0 module is created usin# command CR+R1 M%D$
Modules contain information a'out t&e SQL statements6 &o,ever t&e SQLaccess plans are not created until t&e modules are 'ound into eit&er a
pro#ram or service pro#ram$
8/14/2019 Embedded Sql.rtf
4/20
8* )ro$ram
0 pro#ram is t&e o'4ect ,&ic& "ou can run t&at is created as a result of t&ecompile process for non-*LE compiles or as a result of t&e 'ind process for
*LE compiles$
0n Access plan is a set of internal structures and information t&at tells SQL&o, to run an em'edded SQL statement most effectivel"$ *t is created onl",&en t&e pro#ram &as 'een created successfull"$
0ccess plans are stored and maintained in t&e pro#ram o'4ect for non-distri'uted SQL pro#rams and in t&e SQL pac7a#e for distri'uted SQLpro#rams$
Cursors
9* 0 cursor provides a ,a" to access a ta'le$
10* *t is used ,it&in an SQL pro#ram to maintain a position in t&e ta'le$
11* SQL uses a cursor to ,or7 ,it& t&e ro,s in t&e ta'le and to ma7e t&emavaila'le to t&e pro#ram$
12*+&e pro#ram can &ave several cursors alt&ou#& eac& must &ave a uni8uename$
'"pes of Cursors
13* Serial Cursor Scrolla'le Cursor
Serial Cursor
14* 0 Serial cursor is one defined ,it&out t&e SCR%LL 7e",ord$
15* 9or a Serial cursor eac& ro, of t&e ta'le can 'e fetc&ed onl" once per %1E.of t&e cursor$
8/14/2019 Embedded Sql.rtf
5/20
16* :&en t&e Serial cursor is opened it is positioned 'efore t&e first ro, in t&eta'le$
17* :&en a 9E+C; is issued t&e Serial cursor is moved to t&e ne
8/14/2019 Embedded Sql.rtf
6/20
NEXT 1ositions t&e cursor on t&e ne n ?
in relations&ip to t&e cursor@s current position$
%sin$ Cursors
Step 35 Define t&e Cursor
Step 25 %pen t&e Cursor
Step A5 Specif" :&at to Do :&en End-of-Data *s
Reac&ed
Step 5 Retrieve a Ro, sin# a Cursor
Step 5 pdate t&e Current Ro,
Step 5 Delete t&e Current Ro,
Step 5 Close t&e Cursor
Step *+ Define the Cursor
E EC SQL (Serial)
DECL0RE cursor-name C RS%R 9%R
SELEC+ column-3 column-2 $$$
9R%M ta'le-name $$$
9%R 1D0+E %9 column-2 $$$
8/14/2019 Embedded Sql.rtf
7/20
E.D-E EC$
--------------------------------------------------------------
E EC SQL (Scrollable)
DECL0RE cursor-name DF.0M*C SCR%LL
C RS%R 9%R
SELEC+ column-3 column-2 $$$
9R%M ta'le-name $$$
:;ERE column-3 G e
8/14/2019 Embedded Sql.rtf
8/20
Step 4+ Retrieve a ro2 usin$ Cursor
E EC SQL (Serial)
9E+C; cursor-name
*.+% 5&ost varia'le-3H 5&ost varia'le-2I $$$
E.D-E EC$
--------------------------------------------------------------
E EC SQL (Scrollable)
9E+C; REL0+*=E inte#er
9R%M cursor-name
*.+% 5&ost varia'le-3H 5&ost varia'le-2I $$$
E.D-E EC$
Step 3+ %pdate the Current Ro2
E EC SQL (Serial/Scrollable)
1D0+E ta'le-name
SE+ column-3 G value H column-2 G valueI $$$
:;ERE C RRE.+ %9 cursor-name
E.D-E EC$
Step + Delete the Current Ro2
E EC SQL (Serial/Scrollable)
DELE+E 9R%M ta'le-name
:;ERE C RRE.+ %9 cursor-name
E.D-E EC$
8/14/2019 Embedded Sql.rtf
9/20
Step 5+ Close the Cursor
E EC SQL (Serial/Scrollable)
CL%SE cursor-name
E.D-E EC$
+&e cursor is closed '" t&e s"stem automaticall" ,&en
23* 0 C%MM*+ ,it&out ;%LD statement is issued and t&e cursor is not declaredusin# t&e :*+; ;%LD clause
24* A R,LL AC6 2ithout 7,LD statement is issued
25* +&e 4o' ends
26* +&e activation #roup ends CL%SQLCSR(JE.D0C+ R1) ,as specified on t&epre-compile
27* +&e first SQL pro#ram in t&e call stac7 ends and neit&erCL%SQLCSR(JE.DK%B) or CL%SQLCSR(JE.D0C+ R1) ,as specified ,&en t&epro#ram ,as pre-compiled$
28* 0n JR : C%..EC+ occurred$
Because an open cursor still &olds loc7s on referred-to-ta'les or vie,s "ou s&ould
e
8/14/2019 Embedded Sql.rtf
10/20
#ultiple-Ro2 (.'C7
29* Sin#le 9E+C; statement to retrieve multiple ro,s (Ma
8/14/2019 Embedded Sql.rtf
11/20
#ultiple-Ro2 (.'C7
%sin$ a 7ost Structure Arra"
33* 1ro#ram &as a ;ost Structure 0rra" and an associated *ndicator 0rra"
34* +&e ;ost Structure 0rra" consists of an arra" of structures eac& structurecorresponds to one ro, of t&e ta'le
35* Bot& t&e arra"s &ave same dimension
%sin$ a Ro2 Stora$e Area
36* 1ro#ram &as a Ro, Stora#e 0rea and an associated D escriptor Area(SQLDA)
37* Ro, Stora#e 0rea is a &ost varia'le can 'e JC;0R t"pe enou#& to &old allt&e ro,s fetc&ed
38* SQLD0 contains SQL+F1E SQLLE.
)ro$rammin$ considerations
39* eneral uidelines
40* *nde< Considerations
41* Codin# 0lternatives
42* Loc7in# 0nd Concurrenc"
8eneral 8uidelines
43* 0void .umeric Conversions
SELEC+ 0 *.+% 5B
8/14/2019 Embedded Sql.rtf
12/20
:;ERE 0 G 5B
44* Select %nl" t&e columns .eeded
0void SELEC+ J
se 9%R 1D0+E %9 $$$
45* Se8uence %nl" :&en .ecessar"
0void %RDER BF$$
Consider an *.DE
nde9 Considerations
46* SQL maintains t&e *.DE
47* SQL decides ,&et&er to use t&e *.DE
48* *.DE 'ased retrieval is usuall" more efficient t&an +0BLE SC0.
49* *nde
8/14/2019 Embedded Sql.rtf
13/20
Codin$ Alternatives
58* R%: SELEC+*%. is faster t&an pro#rammin#
59* C% .+ M0 M*. 0= S M and R% 1 BF are more efficient t&anpro#rammin#
60* SQL ,ill not use an inde< for +:% 1RED*C0+ES ,&ic& use an %R
0 G %R BG (no inde< used)
(0G or 0GA) and BN (inde0BCP? (inde< :*LL 'e used)
L* E >P0BC? (inde< :*LL .%+ 'e used)
62* SELEC+ :;ERE C3G 5;= 3!!
CANN ! use index, but ;= G ;= 3!!
SELEC+ :;ERE C3 G 5;=
CAN use "N#$%
63* SELEC+ 9R%M +EM1L :;ERE DE1+ G >032?
%R DE1+ N >B32?
CANN ! use "N#$% , &'!
SELEC+ 9R%M +EM1L :;ERE DE1+ G >032?
.*%. 0LL
SELEC+ 9R%M +EM1L :;ERE DE1+ G >B32?
CAN use "N#$%
Loc:in$ ; Concurrenc"
8/14/2019 Embedded Sql.rtf
14/20
64* 0 mec&anism to ensure data inte#rit"
65* 1revents access to inconsistent data
66* 0n" data 'ein# c&an#ed '" "our pro#ram is not availa'le to ot&ers until "ourelease it
+&ree levels of loc7in# in SQL/ !!
REC%RD - C%MM*+(J.%.E)
+R0.S0C+*%. - C%MM*+(J0LL)
9*LE - L%C +0BLE E CL S*=E
LOCK TABLE
>>-------- LOCK TABLE ---table_name --IN-----SHARE------MODE----->< [EXCLUSIVE
Codin$ SQL statements in L. R)8
8/14/2019 Embedded Sql.rtf
15/20
67* SQLCA SQL Communication Area1
68* +&e SQL pre-compiler automaticall" places t&e SQLC0 in t&e definitionspecifications of t&e *LE R1 / !! pro#ram prior to t&e first calculationspecification
69* %nl" 3 SQLC0 per pro#ram (3A len#t&)
70* SQLC0 #ives SQLC%DE of last statement retrieved
71* SQLC%DE G ! successful operation
72* SQLC%DE ! error
73* SQLC%DE N ! ,arnin#
74* SQLC%DE G 3!! end of data
75* SQLDA SQL Descriptor Area1
76* Multiple SQLD0s per pro#ram ( ! len#t&)
77* SQL 'uilds SQLD0 at runtime
78* SQL pre-compiler allocates static stora#e to SQLD0
79* *f t&e called pro#ram also contains SQL statements it s&ould 'e ended ,it&SE+%. R+ and not SE+%. LR as t&e later initialiTes t&e static stora#e and SQLD0
9ollo,in# statements re8uire SQLD0
E EC +E$$$ S*. DESCR*1+%R descriptor-name
9E+C;$$$ S*. DESCR*1+%R descriptor-name
%1E.$$$ S*. DESCR*1+%R descriptor-name
DESCR*BE statement-name *.+% descriptor-name
DESCR*BE +0BLE &ost-varia'le *.+% descriptor-name
1RE10RE statement-name *.+% descriptor-name
8/14/2019 Embedded Sql.rtf
16/20
7ost varia&les
80* 0ll &ost varia'les must 'e preceded '" a colon (5)
81* Date and +ime &ost varia'les are assi#ned to correspondin# Date and +imesu'-fields in su'structures #enerated '" SQL pre-compiler$ enerated su'-fieldsare declared usin# some date format$ *f D0+9M+ is in s"stem format(JMDF JFMD JDMF JK L) valid values for &ost varia'les are 'et,een 3U ! and2!AU$ 9or an" value outside t&is limit use *BM SQL format (J*S% J S0 JE RJK*S)$
82* 9ollo,in# t"pe of varia'les are not allo,ed as ;ost varia'les$
Pointer, !ables, '#A!$, '#A(, ') N!*, '($A+, Named constants, )ulti-dimension arrays
8/14/2019 Embedded Sql.rtf
17/20
Data structure as 7ost varia&le
!"""#""""$""""%""""$""""&""""$""""'""""$
D EM(LO)EE DS
D EM(NO *# *+A
D ,NAME * #.A
D MIDINT #/ #/A
D LNAME %* &'A
D DE(TNO &0 & A
C MOVE 1***%%*1 EM(NO
C2EXEC S3L
C$ SELECT ! INTO 4 EM(LO)EE
C$ ,ROM COR(DATA"EM(DATA
C$ 5HERE EM(NO 6 4EM(NO
C2END-EXEC
Data structure Arra" as 7ost varia&le
!"""#""""$""""%""""$""""&""""$""""'""""$
DDE(ARTMENT DS OCCURS7#*8
D DE(TNO *# *&A
D DE(TNM *' &%A
D M9RNO && &.A
DIND_ARRA) DS OCCURS7#*8
D INDS &B * DIM7&8
"""
C2EXEC S3L
C$ DECLARE C# ,OR
8/14/2019 Embedded Sql.rtf
18/20
C$ SELECT ! ,ROM COR(DATA"DE(ARTMENT
C2END-EXEC
"""
C2EXEC S3L C$ ,ETCH C# ,OR #* RO5S
C$ INTO 4DE(ARTMENT4IND_ARRA)
C2END-EXEC
8/14/2019 Embedded Sql.rtf
19/20
%sin$ .9ternal (ile Descriptions
!"""#""""$""""%""""$""""&""""$""""'""""$""""0""""$""""+""""$"""" """
,DE(ARTMENTI( E DISK RENAME7ORI9REC4DE(TREC8
Code t&e file on 9-Specs onl" if it is to 'e used '" R1 LE
statements else define file(ta'le) as e
8/14/2019 Embedded Sql.rtf
20/20
SQLC%DE #ives t&e status of last operation$
/E EC SQL
:;E.E=ER condition action
/E.D-E EC
Conditions 5SQLERR%R
SQL:0R.*.
.%+ 9% .D
0ctions 5 %+% 5ERR
C%.+*. E
*n case ,e use %+% +0 statement can precede an SQL statement$
Recommended