Www.PrismaTech.net Jump-Start Embedded SQL into RPG Presented by: Robert Arce.
-
Upload
erin-stafford -
Category
Documents
-
view
215 -
download
0
Transcript of Www.PrismaTech.net Jump-Start Embedded SQL into RPG Presented by: Robert Arce.
www.PrismaTech.net
Jump-Start Embedded SQL Jump-Start Embedded SQL into RPGinto RPG
Presented by:
Robert Arce
Jump-Start embedded SQL into RPG by Robert Arce
Why SQL in RPG?Why SQL in RPG?
Useful for ad hoc retrieval and update
Easy manipulation of data
SQL alone may not provide logic capabilities to perform required operations
SQL has no user interface
ad hoc statements can be complex
sorting possibilities are enhanced by SQL
Jump-Start embedded SQL into RPG by Robert Arce
SQL embedded typesSQL embedded types
Static statements – structure of the statement does NOT change
Static “select into” statementsDynamic StatementsCursors – to handle multiple retrieves
Jump-Start embedded SQL into RPG by Robert Arce
SQL source typeSQL source type
SQLRPGDB2/400 Query Manager RPG
SQLRPGLE DB2 Query Manager RPG/400 Integrated
Language Environment
Jump-Start embedded SQL into RPG by Robert Arce
Create SQL ILE RPG ObjectCreate SQL ILE RPG Object
CRTSQLRPGIPrecompiles SQL statements creates
RPGLE source file in QTEMP/QSQLTEMP1 (ToSrcFile option)
Create Bound RPG program CRTBNDRPGVoila! you have a program
Jump-Start embedded SQL into RPG by Robert Arce
Precompiler DirectivesPrecompiler Directives
Only one SQL statement
C/exec sql --open sql code
C+ update CUSMS
C+ set CMREPS=233
C+ where CMREPS=5
C/end-exec -- close sql code
Jump-Start embedded SQL into RPG by Robert Arce
Host VariablesHost VariablesRPG program variables are use within the SQL statements
C/exec sql --open sql code
C+ update CUSMS
C+ set CMREPS=:newslsrep
C+ where CMREPS=:oldslsrep
C/end-exec -- close sql code
Jump-Start embedded SQL into RPG by Robert Arce
Indicator Variable ~ NullIndicator Variable ~ Null
Indicator variable can optionally be coded after the host variable
Two ways to declare Indicator variable:Dindnul_1 s 5i 0
Dindnul_2 s 4b 0
Assign negative (-1) as null or positive (0) as not null
Jump-Start embedded SQL into RPG by Robert Arce
Indicator VariableIndicator Variable
C+ set SHIPTO=:newshipto:indshipSame as:
if indship = -1
SHIPTO = null
else
SHIPTO = newshipto
endif
Jump-Start embedded SQL into RPG by Robert Arce
Select IntoSelect IntoONLY retrieve ONE row or NADA! Into clause must list one host variable for
each column in the select listOptionally and is suggested to use an
indicator variable per each rowCan select all fields “*” and assign them
into the proper Data structuresqlstt = '21000‘ more than one row
Jump-Start embedded SQL into RPG by Robert Arce
Select IntoSelect Into * Set host variables
C customer=700583 *C/exec sqlC+ select CMCSNO, --customer numberC+ CMCSNM, --customer nameC+ CMDFSH --default shipto C+ into :csCMCSNO, C+ :csCMCSNM, C+ :csCMDFSH :inCMDFSHC+ from CUSMSC+ where CMCSNO=:customerC/end-exec
Jump-Start embedded SQL into RPG by Robert Arce
Dynamic SQLDynamic SQL
Construct in string variables on the flyExecute immediate vs Prepare and executePrepare takes extra time building statement
but execution runs fasterParameter markers ‘?’ define in the
Using clause with one host variable per marker
Jump-Start embedded SQL into RPG by Robert Arce
Dynamic Dynamic execute immediateexecute immediate
* Assign SQL statement dynsqlstm='delete from CUSMS where '+ 'CMCSNM LIKE '+'''%CAR%''‘
* default when create pgm COMMIT =*CHG
C/exec sql set option commit=*none C/end-exec C/exec sql C+ execute immediate :dynsqlstm C/end-exec
Jump-Start embedded SQL into RPG by Robert Arce
Dynamic Dynamic Prepare-executePrepare-execute
Prepare Statement-Name from :host-variable
C/exec sql --Part 1
C+ prepare dltcuscar C+ from :dynsqlstm C/end-exec * C if sqlstt = '00000'C/exec sql -- Part 2
C+ execute dltcuscar C/end-exec
Jump-Start embedded SQL into RPG by Robert Arce
Dynamic Dynamic Prepare-Parameter MarkerPrepare-Parameter Marker
dynsqlstm='delete from CUSMS where CMCSNM like ?' C/exec sql C+ prepare dltcustcar C+ from :dynsqlstm C/end-exec C if sqlstt = '00000'C eval namelike ='%CAR%‘ *C/exec sql C+ execute dltcustcar C+ using :namelike C/end-exec
Jump-Start embedded SQL into RPG by Robert Arce
CursorsCursorsCursors open more than one rowIs based on Select statementsAllow you to do data manipulation
• Sorts• Sums• Use Host Variable for sorts and selections
Declare as read-only or updatable
Jump-Start embedded SQL into RPG by Robert Arce
CursorsCursorsDeclare cursorOpen the cursorFetch – like read records (rows) from cursor
(optionally update or delete the most recently fetched record)
Close cursor (cursor must be open)
Jump-Start embedded SQL into RPG by Robert Arce
Cursor - DeclareCursor - DeclareC/exec sql
C+ declare custcursor cursor
C+ for select CMCSNO, CMCSNM, CMDFSH
C+ from CUSMS
C+ where CMCSNM like :namelike
C+ order by :custsort
C+ for read only
C/end-exec
Jump-Start embedded SQL into RPG by Robert Arce
Cursor - OpenCursor - OpenHost variables are evaluated only when the cursor is
open
* sets values for the host variablesC eval namelike = '%CAR%' C eval custsort = 'CMCSNO'C/exec sqlC+ open custcursorC/end-exec
Jump-Start embedded SQL into RPG by Robert Arce
Cursor - FetchCursor - FetchC dow morerowsC/exec sql C+ Fetch Next C+ from custcursor C+ into :csCMCSNO, C+ :csCMCSNM, C+ :csCMDFSH C/end-exec
Jump-Start embedded SQL into RPG by Robert Arce
Cursors - UpdateCursors - Update
In the declare cursor statement replace:
“for read only” by “for update of CMREPS” After the fetch do the update:
C/exec sql
C+ update CUSMS
C+ set CMREPS=:newslsrep
C+ where current of custcursor --name of the cursor
C/end-exec
Jump-Start embedded SQL into RPG by Robert Arce
Cursor - DynamicCursor - DynamicC/exec sql
C+ declare custcursor cursor
C+ for selcustcar
C/end-exec
* Asssign sql to host variable:
sqlstm='select CMCSNO,CMCSNM,CMDFSH '+
'from CUSMS ' +
'where CMCSNM like ? ' +
'order by CMCSNO'
Jump-Start embedded SQL into RPG by Robert Arce
Cursor - DynamicCursor - DynamicC/exec sql C+ prepare selcustcar C+ from :dynsqlstm C/end-exec C if sqlstt ='00000' * sets parameter marker valueC eval namelike='%CAR%'C/exec sqlC+ open custcursorC+ using :namelikeC/end-exec
Jump-Start embedded SQL into RPG by Robert Arce
Fetch PositionFetch Position
Next - Prior First - Last Before before the first row After after the last row Current no change in position Relative n
negative ~ previous positive ~ next
Jump-Start embedded SQL into RPG by Robert Arce
referencesreferences
DB2 UDB for iSeries SQL programming
http://publib.boulder.ibm.com/iseries/v5r1/ic2924/index.htm?info/sqlp/rbafymst02.htm
SQL/400 developer’s guide by Paul Conte and Mike Cravitz