RPG SQL stuff
Multiple files D T10 E DS extname(YOURFILE1)
D T20 E DS extname(YOURFILE2)
C+ select * from YOURFILE1 T10, YOURFILE2 T20
C+ order by T10.AField, T20.AField
C+ fetch C1 into :T10,:T20
Fetch error - SQLCODE 183- (date/timestamp error)
2 choices to fix problem
- compile with prompt and change keyword DATFMT *ISO
- NOTE - an H spec of DATFMT(*ISO) does not cover the SQL code just
RPG
- add the following statement before any SQL gets executed
C/exec sql
C+ set option
C+ DATFMT = *ISO
C/end-exec
Example program
nothing fancy, just something to get you started then you can start changing
and adding stuff for it to do.
The parms and PSDS are throw ins to show you how to use external parameters
in the SQL statement...I am a big "don't force me to send all parameters" kind
of guy. H BndDir('QC2LE') DFTACTGRP(*no) debug(*yes) ALWNULL(*USRCTL)
FQSysprt198O F 198 Printer oflind(*INOA)
‚* Program data structure
D SDS
D QPROG *PROC
D QPARMS *PARMS
D QPGMNM 1 10
D QJOBN 244 253
D QUSER 254 263
D T10 E DS extname(YOURFILE)
‚*=====================================================================
‚* Mainline
‚*=====================================================================
C *entry plist
C parm InParm 5 0
C if QParms > 0
C move InParm CkParm 5 0
C else
C clear CkParm
C endif
C
* do the select on the work file get proper sort
C/exec sql
C+ declare C1 dynamic scroll cursor for
C+ select * from YOURFILE T10
C+ where :QParms > 0 and
C+ AField = :CkParm or
C+ :QParms = 0 and
C+ AField = 2
C+ order by AField, AField1, AField2
C/end-exec
C
‚* open the cursor
C/exec sql
C+ open C1
C/end-exec
‚* do until end-o-rows, like eof (end-o-file)
C dou SQLCOD <> 0 and SQLCOD <> -181
‚* fetch each record from the view in the data structure
C clear T10
C/exec sql
C+ fetch C1 into :T10
C/end-exec
‚* monitor for certain SQL codes
‚* 181 = Value in date, time, or timestamp string not valid
‚* 183 = Result of date or timestamp expression not valid
‚* use http://publib.boulder.ibm.com/iseries/v5r2/ic2924/index.htm?info/rzala/rzalafinder.htm
C if SQLCOD <> 0 and
C SQLCOD <> -181 and
C SQLCOD <> 183
C if SQLCOD <> -181
C*** SQLCOD dsply
C*** dump
C*** leave
C endif
C endif
* do stuff in here with your data
C eval Count = Count + 1
C except DetLin2
C enddo
C/exec sql
C+ close C1
C/end-exec
C seton LR
CLR except Footer
‚*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
‚/EJECT
‚*=====================================================================
‚* *INZSR - Initialize subroutine to do initialization stuff...duh
‚*=====================================================================
C *INZSR begsr
C call 'SYSGET'
C parm PARM1
C eval CURDATE = %date
C eval CURTIME = %time
C eval HoldNum = 99999
C CurDate subdur 18:*months Date18mths
C endsr
‚*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
‚/EJECT
OQSYSPRT198H OA 3
O QPROG 10
O 160 'Date:'
O CURDATE +001
O 180 'Page:'
O PAGE +001
OQSYSPRT198H OA 1
O QUSER 10
O 110 'Report Name'
O 160 'Time:'
O CURTIME +001
OQSYSPRT198EF DetLin2 1
O ' '
O AField
O '|'
O AField2
O '|'
OQSYSPRT198EF Footer 1
O 76 '*** End Of Report *** '
O ' Record Count-'
O Count +001
You are here: Home-Computer Tips & Help-Programming-SQL RPG examples
Previous Topic: SQL Dates Next Topic: Batch Files
|