0% found this document useful (0 votes)
59 views

(Embedded SQL) : What SQL Provides To AS/400 ?

This document provides information on SQL and how it is used with AS/400 systems. It discusses SQL run-time support, pre-compilers, and interactive interfaces that are provided. It also covers defining and using cursors to retrieve and manipulate data, including serial and scrollable cursors. Indexing considerations for SQL statements are described. The document provides examples of embedding SQL statements in RPG programs using the SQLCA.

Uploaded by

dumbledoreaaaa
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

(Embedded SQL) : What SQL Provides To AS/400 ?

This document provides information on SQL and how it is used with AS/400 systems. It discusses SQL run-time support, pre-compilers, and interactive interfaces that are provided. It also covers defining and using cursors to retrieve and manipulate data, including serial and scrollable cursors. Indexing considerations for SQL statements are described. The document provides examples of embedding SQL statements in RPG programs using the SQLCA.

Uploaded by

dumbledoreaaaa
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 20

ERIS

0* 1* 2* 3* 4*

(EMBEDDED SQL)

What SQL provides to AS/400 ? SQL run-time support SQL pre-compilers SQL interactive interface Run SQL Statements CL command 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$

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 R12LE alon# ,it& SQL statements in a source mem'er usin# SE-$

6*

'emporar" Source (ile #em&er

+&is source is pre-compiled usin# CR+SQLR12* command/ ,&ic& creates a

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 SQL
statements

Parsed and syntax checked SQL statements 7*


#odule

0 module is created usin# command CR+R12M%D$ Modules contain information a'out t&e SQL statements6 &o,ever/ t&e SQL
access plans are not created until t&e modules are 'ound into eit&er a pro#ram or service pro#ram$

8*

)ro$ram

0 pro#ram is t&e o'4ect ,&ic& "ou can run t&at is created as a result of t&e
compile 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 0ccess plans are stored and maintained in t&e pro#ram o'4ect for nondistri'uted SQL pro#rams and in t&e SQL pac7a#e for distri'uted SQL pro#rams$

&o, to run an em'edded SQL statement most effectivel"$ *t is created onl" ,&en t&e pro#ram &as 'een created successfull"$

Cursors

9* 10* 11*

0 cursor provides a ,a" to access a ta'le$ *t is used ,it&in an SQL pro#ram to maintain a position in t&e ta'le$

SQL uses a cursor to ,or7 ,it& t&e ro,s in t&e ta'le and to ma7e t&em availa'le to t&e pro#ram$ +&e pro#ram can &ave several cursors/ alt&ou#& eac& must &ave a uni8ue name$

12*

'"pes of Cursors

13*

Serial Cursor ( Scrolla'le Cursor

Serial Cursor

14* 15*

0 Serial cursor is one defined ,it&out t&e SCR%LL 7e",ord$

9or a Serial cursor/ eac& ro, of t&e ta'le can 'e fetc&ed onl" once per %1E. of t&e cursor$

16* 17* 18*

:&en t&e Serial cursor is opened/ it is positioned 'efore t&e first ro, in t&e ta'le$ :&en a 9E+C; is issued/ t&e Serial cursor is moved to t&e ne<t ro, in t&e ta'le$ +o use t&e Serial cursor a#ain/ "ou must first close it and t&en re-issue t&e %1E. statement$

Scrolla&le Cursor

19* 20* 21* 22*

0 Scrolla'le cursor is one defined ,it& t&e SCR%LL 7e",ord$ 9or a Scrolla'le cursor/ t&e ro,s of t&e ta'le can 'e fetc&ed man" times$

:&en t&e Scrolla'le cursor is opened/ it is positioned 'efore t&e first ro, of t&e ta'le$ :&en a 9E+C; is issued/ t&e cursor is positioned to t&e ro, of t&e ta'le t&at is specified '" t&e 1%S*+*%. option$

9ollo,in# scroll options are used to position t&e cursor ,&en issuin# a 9E+C; statement$ +&ese positions are relative to t&e current cursor location in t&e ta'le5

NEXT 1R*%R 9*RS+ L0S+ BE9%RE 09+ER C-RRE.+ REL0+*=E n

1ositions t&e cursor on t&e ne<t ro,$ (Default value) 1ositions t&e cursor on previous ro,$ 1ositions t&e cursor on t&e first ro,$ 1ositions t&e cursor on t&e last ro,$ 1ositions t&e cursor 'efore t&e first ro,$ 1ositions t&e cursor after t&e last ro,$ Does not c&an#e t&e cursor position$ Evaluates a &ost varia'le or inte#er >n? in relations&ip to t&e cursor@s current position$

%sin$ Cursors Step 35 Step 25 Step A5 Define t&e Cursor %pen t&e Cursor Specif" :&at to Do :&en End-of-Data *s Reac&ed Step 5 Step B5 Step C5 Step D5 Retrieve a Ro, -sin# a Cursor -pdate t&e Current Ro, Delete t&e Current Ro, Close t&e Cursor

Step *+

Define the Cursor

EEEC 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 /$$$

E.D-EEEC$ -------------------------------------------------------------EEEC 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<pression $$$ E.D-EEEC$

Step !+

,pen the Cursor

EEEC SQL (Serial/Scrollable) %1E. cursor-name E.D-EEEC$

Step -+

Specif" What to Do When .nd-of-Data /s Reached 0Serial/Scrolla&le1

*9 SQLC%DE G3!! 2%+% 5E%9 or *9 SQLS+0+E G@!2!!!@ 2%+% 5E%9 Alternatel" 2e can use EEEC SQL :;E.E=ER .%+ 9%-.D 2%+% symbolic-address E.D-EEEC$

Step 4+

Retrieve a ro2 usin$ Cursor

EEEC SQL (Serial) 9E+C; cursor-name *.+% 5&ost varia'le-3H/ 5&ost varia'le-2I $$$ E.D-EEEC$ -------------------------------------------------------------EEEC SQL (Scrollable) 9E+C; REL0+*=E inte#er 9R%M cursor-name *.+% 5&ost varia'le-3H/ 5&ost varia'le-2I $$$ E.D-EEEC$

Step 3+

%pdate the Current Ro2

EEEC 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-EEEC$

Step 4+

Delete the Current Ro2

EEEC SQL (Serial/Scrollable) DELE+E 9R%M ta'le-name :;ERE C-RRE.+ %9 cursor-name E.D-EEEC$

Step 5+

Close the Cursor

EEEC SQL (Serial/Scrollable) CL%SE cursor-name E.D-EEEC$

+&e cursor is closed '" t&e s"stem automaticall" ,&en

23* 24* 25* 26* 27* 28*

0 C%MM*+ ,it&out ;%LD statement is issued and t&e cursor is not declared usin# t&e :*+; ;%LD clause A R,LL AC6 2ithout 7,LD statement is issued +&e 4o' ends

+&e activation #roup ends ( CL%SQLCSR(JE.D0C+2R1) ,as specified on t&e pre-compile +&e first SQL pro#ram in t&e call stac7 ends and neit&er CL%SQLCSR(JE.DK%B) or CL%SQLCSR(JE.D0C+2R1) ,as specified ,&en t&e pro#ram ,as pre-compiled$ 0n JR-: C%..EC+ occurred$

Because an open cursor still &olds loc7s on referred-to-ta'les or vie,s/ "ou s&ould e<plicitl" close an" open cursors

#ultiple-Ro2 (.'C7

29* 30* 31* 32*

Sin#le 9E+C; statement to retrieve multiple ro,s (Ma<$ A2DCD) 0fter retrieval/ cursor is positioned at last ro, read Can 'e used ,it& Serial ( Scrolla'le cursors

0fter eac& multiple-ro, 9E+C;/ information is returned to pro#ram t&rou#& SQLCA (SQL Communication Area)

SQLCA contains SQLC%DE/ SQLS+0+E ( SQLERRDA SQLERRD SQLERRDB 5 .o of ro,s retrieved 5 Len#t& of eac& ro, 5 0n indication t&at t&e last ro, in t&e ta'le ,as fetc&ed

+o define t&e stora#e/ ,&ere fetc&ed ro,s are placed a &ost structure arra" or a ro, stora#e area ,it& an associated descriptor is used$

#ultiple-Ro2 (.'C7

%sin$ a 7ost Structure Arra"

33* 34* 35*

1ro#ram &as a ;ost Structure 0rra" and an associated *ndicator 0rra"

+&e ;ost Structure 0rra" consists of an arra" of structures ( eac& structure corresponds to one ro, of t&e ta'le Bot& t&e arra"s &ave same dimension

%sin$ a Ro2 Stora$e Area

36* 37* 38*

1ro#ram &as a Ro, Stora#e 0rea and an associated Descriptor Area (SQLDA) Ro, Stora#e 0rea is a &ost varia'le ( can 'e JC;0R t"pe ( enou#& to &old all t&e ro,s fetc&ed SQLD0 contains SQL+F1E ( SQLLE.

)ro$rammin$ considerations

39* 2eneral 2uidelines 40* *nde< Considerations 41* Codin# 0lternatives 42* Loc7in# 0nd Concurrenc"

8eneral 8uidelines

43*

0void .umeric Conversions SELEC+ 0 *.+% 5B

L:;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 *.DEE

/nde9 Considerations

46* 47* 48* 49* 50*

SQL maintains t&e *.DEE SQL decides ,&et&er to use t&e *.DEE *.DEE 'ased retrieval is usuall" more efficient t&an +0BLE SC0. *nde<es add to s"stem over&ead 9or eac& statement/ SQL determines5
Do inde<(es) e<ist for t&e ta'leM *s an inde< usa'le for t&is access to t&e dataM :ill t&e use of an inde< improve performanceM

51* 52* 53*

54*

SQL t&en decides to eit&er5


-se an e<istin# inde< Create a temporar" inde< Scan t&e entire ta'le

55* 56* 57*

Codin$ Alternatives

58* 59* 60*

R%: SELEC+*%. is faster t&an pro#rammin#

C%-.+/M0E/M*./0=2/S-M/ and 2R%-1 BF are more efficient t&an pro#rammin# SQL ,ill not use an inde< for +:% 1RED*C0+ES ,&ic& use an %R

0 G B %R BGD (no inde< used) (0GB or 0GA) and BND (inde<ed on B) (0GB or 0GA) %R 0ND (no inde< used)

61*

SQL ,ill not use an inde< for L*)E

1RED*C0+ES ,it& leadin# O or P L*)E >0BCP? (inde< :*LL 'e used) L*)E >P0BC? (inde< :*LL .%+ 'e used)

62*

SELEC+ L :;ERE C3G 5;= Q 3!! CANN ! use index, but ;= G ;= Q3!!

SELEC+ L :;ERE C3 G 5;= CAN use "N#$%

63*

SELEC+ L 9R%M +EM1L :;ERE DE1+ G >032?

%R DE1+ N >B32? CANN ! use "N#$% , &'! SELEC+ L 9R%M +EM1L :;ERE DE1+ G >032? -.*%. 0LL SELEC+ L 9R%M +EM1L :;ERE DE1+ G >B32? CAN use "N#$%

Loc:in$ ; Concurrenc"

64* 65* 66*

0 mec&anism to ensure data inte#rit" 1revents access to inconsistent data

0n" data 'ein# c&an#ed '" "our pro#ram is not availa'le to ot&ers until "ou release it

+&ree levels of loc7in# in SQL/ !! REC%RD +R0.S0C+*%. 9*LE LOCK TABLE >>-------- LOCK TABLE ---table_name --IN-----SHARE------MODE----->< [EXCLUSIVE - C%MM*+(J.%.E) - C%MM*+(J0LL) - L%C) +0BLEL EECL-S*=EL

Codin$ SQL statements in /L. R)8

67* 68* 69* 70*

SQLCA 0SQL Communication Area1

+&e SQL pre-compiler automaticall" places t&e SQLC0 in t&e definition specifications of t&e *LE R12/ !! pro#ram prior to t&e first calculation specification %nl" 3 SQLC0 per pro#ram (3AC len#t&) SQLC0 #ives SQLC%DE of last statement retrieved
SQLC%DE G !/ successful operation SQLC%DE R !/ error SQLC%DE N !/ ,arnin# SQLC%DE G 3!!/ end of data

71* 72* 73* 74*

75* 76* 77* 78* 79*

SQLDA 0SQL Descriptor Area1 Multiple SQLD0s per pro#ram (S! len#t&) SQL 'uilds SQLD0 at runtime SQL pre-compiler allocates static stora#e to SQLD0

*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 EEEC-+E$$$ 9E+C;$$$ %1E.$$$ DESCR*BE -S*.2 DESCR*1+%R descriptor-name -S*.2 DESCR*1+%R descriptor-name -S*.2 DESCR*1+%R descriptor-name statement-name *.+% descriptor-name

DESCR*BE +0BLE &ost-varia'le *.+% descriptor-name 1RE10RE statement-name *.+% descriptor-name

7ost varia&les

80* 81*

0ll &ost varia'les must 'e preceded '" a colon (5)

Date and +ime &ost varia'les are assi#ned to correspondin# Date and +ime su'-fields in su'structures #enerated '" SQL pre-compiler$ 2enerated su'-fields are 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 ! and 2!AU$ 9or an" value outside t&is limit/ use *BM SQL format (J*S%/ J-S0/ JE-R/ JK*S)$ 9ollo,in# t"pe of varia'les are not allo,ed as ;ost varia'les$

82*

Pointer, !ables, '#A!$, '#A(, ') N!*, '($A+, Named constants, )ultidimension arrays

Data structure as 7ost varia&le


!"""#""""$""""%""""$""""&""""$""""'""""$ D EM(LO)EE D EM(NO D ,NAME D MIDINT D LNAME D DE(TNO C C2EXEC S3L C$ SELECT ! C$ ,ROM C$ 5HERE C2END-EXEC INTO 4 EM(LO)EE DS *# *#/ %* &0 *+A #.A #/A &'A &-A EM(NO

MOVE 1***%%*1

COR(DATA"EM(DATA EM(NO 6 4EM(NO

Data structure Arra" as 7ost varia&le


!"""#""""$""""%""""$""""&""""$""""'""""$ DDE(ARTMENT D DE(TNO D DE(TNM D M9RNO DS *# *' && OCCURS7#*8 *&A &%A &.A

DIND_ARRA) D INDS """ C2EXEC S3L C$ DECLARE C# ,OR

DS

OCCURS7#*8 &B * DIM7&8

C$

SELECT ! ,ROM

COR(DATA"DE(ARTMENT

C2END-EXEC """ C2EXEC S3L C$ C$ ,ETCH C# ,OR #* RO5S INTO 4DE(ARTMENT4IND_ARRA)

C2END-EXEC

%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 '" R12LE statements/ else define file(ta'le) as e<ternal Data structure$

!"""#""""$""""%""""$""""&""""$""""'""""$""""0""""$""""+""""$""""-""""$"" D TDE(T D DE(TNM D MANA9ER E DS E E EXTNAME7DE(ARTMENT8 EXT,LD7DE(TNAME8 EXT,LD7DE(TM9R8

Codin$ SQL statements in /L. R)8

83* 84*

Ever" SQL statement starts ,it& EEEC SQL and ends ,it& E.D-EEEC EEEC SQL must occup" positions S t&rou#& 3C ( ma"

continue 'e"ond t&at 'ut 3D t&rou#& S! positions after E.D-EEEC must 'e 'lan7 *""#""""$""""%""""$""""&""""$""""'""""$ C/EXEC S3L UPDATE DEPARTMENT C+ C+ C/END-EXEC SET MANA9ER 6 4M9RNUM 5HERE DE(TNO 6 4INTDE(

.9ception handlin$

SQLC%DE #ives t&e status of last operation$ /EEEC SQL :;E.E=ER condition action /E.D-EEEC Conditions 5SQLERR%R SQL:0R.*.2 .%+ 9%-.D 0ctions 5 2%+% 5ERR C%.+*.-E *n case ,e use 2%+%/ +02 statement can precede an SQL statement$

You might also like