Académique Documents
Professionnel Documents
Culture Documents
Copyright 1992, 1993 BY DMC CONSULTING, INC. ALL RIGHTS RESERVED 5650 West Central Avenue, Suite B, Toledo, OH 43615
AS/400 is a trademark of International Business Machines Corporation
RPG IV
PRE-COMPILER
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
FS pe cs
Type options, press Enter. 2=Edit 3=Copy 4=Delete 5=Display 8=Display description 9=Save 13=Change text Opt Member SQLPROC SQLTEST STOREDPRC1 STOREDPRC3 STOREDPRC4 STOREDPRC5 TSTRPGPRC TSTRPGPRC2 Type SQLRPGLE SQLRPGLE SQLRPGLE SQLRPGLE SQLRPGLE SQLRPGLE SQLRPGLE SQLRPGLE Text Test Test Test Test Test Test Test Test
6=Print 14=Compile
Syntax
C/EXEC SQL C+ C/END-EXEC :field-name C/EXEC SQL INCLUDE member-name C/END-EXEC ?
Syntax
RPG IV variables you cant use as a HOST variable:
Pointers Tables UDATE UDAY UMONTH UYEAR Look-ahead fields Named constants Definitions requiring the resolution of %SIZE or %ELEM
Avoid creating fields that start with: 'SQ', 'SQL', 'RDI', or 'DSN
SQL Pre-Compiler
The SQL pre-compiler adds fields such as:
SQLAID SQLABC SQLCOD / SQLCODE SQLERL SQLERM SQLERP SQLERR SQLSTT / SQLSTATE SQLWRN
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
CRTSQLRPGI
Create SQL ILE RPG Object (CRTSQLRPGI) Type choices, press Enter. Object . . . . . . . Library . . . . . Source file . . . . Library . . . . . Source member . . . Commitment control . Relational database Compile type . . . . Listing output . . . Text 'description' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . > RPG100 > DMCSQLLIB > QRPGLESRC > DMCSQLLIB > RPG100 *CHG *LOCAL > *PGM *NONE *SRCMBRTXT Name Name, Name, Name, Name, *CHG,
CRTSQLRPGI
Create SQL ILE RPG Object (CRTSQLRPGI) Type choices, press Enter. Target release . . . . . INCLUDE file . . . . . . Library . . . . . . . Allow copy of data . . . Close SQL cursor . . . . Allow blocking . . . . . Delay PREPARE . . . . . Severity level . . . . . Date format . . . . . . Date separator character Time format . . . . . . Time separator character Replace . . . . . . . . RDB connect method . . . Default Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . *CURRENT . *SRCFILE . *LIBL . *YES . *ENDACTGRP . *READ . *NO . 10 . *JOB . *JOB . *HMS . *JOB . > *YES . *DUW . *NONE *CURRENT, *PRV, VxRxMx Name, *SRCFILE Name, *LIBL, *CURLIB *YES, *NO, *OPTIMIZE *ENDMOD, *ENDACTGRP *READ, *NONE, *ALLREAD *YES, *NO 0-40 *JOB, *USA, *ISO, *EUR... *JOB, /, ., ,, -, ' ', *BLANK *HMS, *USA, *ISO, *EUR, *JIS *JOB, :, ., ,, ' ', *BLANK *YES, *NO *DUW, *RUW Name, *NONE More... F13=How to use this display
F5=Refresh
F12=Cancel
CRTSQLRPGI
Create SQL ILE RPG Object (CRTSQLRPGI) Type choices, press Enter. Package . . . . . . Library . . . . . IBM SQL Flagging . . ANS flagging . . . . Print file . . . . . Library . . . . . Debugging view . . . User profile . . . . Dynamic User Profile Sort Sequence . . . Library . . . . . Language id . . . . To source file . . . Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . *OBJ *OBJLIB *NOFLAG *NONE QSYSPRT *LIBL *NONE *NAMING *USER *JOB *JOB QSQLTEMP1 QTEMP Name, *OBJ Name, *OBJLIB *NOFLAG, *FLAG *NONE, *ANS Name Name, *LIBL, *CURLIB *SOURCE, *NONE *NAMING, *USER, *OWNER *USER, *OWNER Name, *HEX, *JOB, *JOBRUN... Name, *LIBL, *CURLIB *JOB, *JOBRUN... Name Name, *LIBL, *CURLIB
F5=Refresh
F12=Cancel
C C
KEY
CHAIN IF
FILE %FOUND
ENDIF RETURN
Embedding SELECT
Selecting ONE field from ONE record * DW_ITEM DW_DESC S S 9P 0 40
* Run the SQL select C 'ITEM?' C/Exec SQL C+ C+ C+ C/End-exec C W_DESC C DSPLY SELECT MDESC INTO :W_DESC FROM ITMMST WHERE MITEM = :W_ITEM DSPLY RETURN W_ITEM
Embedding SELECT
Selecting TWO fields from ONE record DW_ITEM DW_PRICE DW_DESC S S S 9P 0 7P 2 40
* Run the SQL select C 'ITEM?' C/Exec SQL C+ C+ C+ C/End-exec C W_DESC C W_PRICE C
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
DSPLY SELECT MDESC, MPRICE INTO :W_DESC, :W_PRICE FROM ITMMST WHERE MITEM = :W_ITEM DSPLY DSPLY RETURN
W_ITEM
Embedding SELECT
Selecting MANY fields from ONE record DW_DATA DS EXTNAME(ITMMST)
* Run the SQL select C 'ITEM?' C/Exec SQL C+ C+ C+ C/End-exec C MDESC C MPRICE C DSPLY SELECT * INTO :W_DATA FROM ITMMST WHERE MITEM = :MITEM DSPLY DSPLY RETURN MITEM
EXAMPLE #1
DW_CUST DW_NAME C 'CUST' S S DSPLY 6P 0 40 W_CUST
C/EXEC SQL C+ SELECT CNAME INTO :W_NAME C+ from CUST WHERE CNUMBER = :W_CUST C/END-EXEC C C C 'NAME IS' DSPLY EVAL RETURN *INLR = *ON W_NAME
Embedding INSERT
* Run the SQL INSERT W/VALUES C/Exec SQL C+ C+ C/End-exec INSERT INTO CUST VALUES(7, The Fruit Stand,,, Miami,FL,,)
* Run the SQL INSERT W/SUB-SELECT C/Exec SQL C+ C/End-exec INSERT INTO TESTLIB/CUST SELECT * FROM PRODLIB/CUST
Embedding INSERT
* Run the SQL INSERT W/n ROWS DW_FIELDS D W_ITEM D W_DESC D W_WGT D W_PRICE or D W_FIELDS D E DS EXTNAME(FILE) OCCURS(10) DS 9 40 4 9 0 2 2 OCCURS(10)
C/Exec SQL C+ UPDATE CUSTWORK SET ROW = (SELECT * FROM CUST WHERE C+ CUSTWORK.NAME = CUST.NAME) C/End-exec
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
EXAMPLE #2
DW_CUST C C C C E DS EVAL EVAL EVAL EVAL EXTNAME(CUST) CNUMBER = 1111 CNAME = 'The Garden Grill' CSTATE = 'OH' CCITY = 'Toledo'
RPG200 - LAB #2
C/EXEC SQL C+ UPDATE CUST C+ SET CCITY = 'Miami' WHERE CNUMBER = 1111 C/END-EXEC C/EXEC SQL C+ DELETE FROM CUST C+ WHERE CNUMBER = 1111 C/END-EXEC C C EVAL RETURN *INLR = *ON
Using PREPARE
DW_SQL DW_SELECT DW_FROM DW_WHERE DW_FIELD DW_VALUE C C C C C C
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
1000 SELECT MITEM FROM ITMMST WHERE 10 30 W_FIELD W_VALUE W_SQL= W_SELECT + W_FROM + W_WHERE + W_FIELD + = + W_VALUE = + W_VALUE +
FIELD?' VALUE?'
OR
Using PREPARE
C/Exec SQL C/End-exec C/Exec SQL C/End-exec C/Exec SQL C/End-exec C C/Exec SQL C/End-exec C C/Exec SQL C/End-exec
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
OPEN @C1 DOU SQLCOD <> *zero FETCH @C1 INTO :W_VALUE ENDDO CLOSE @C1
Using PREPARE
DW_SQL DW_DELETE DW_FROM DW_WHERE DW_FIELD DW_VALUE C C C C C FIELD?' VALUE?' S C C C S S DSPLY DSPLY EVAL 1000 DELETE FROM ITMMST WHERE 10 30 W_FIELD W_VALUE W_SQL= W_DELETE + W_FROM + W_WHERE + W_FIELD + = + W_VALUE
Using PREPARE
C/Exec SQL C/End-exec C/Exec SQL C/End-exec PREPARE @S1 from :W_SQL
EXECUTE @S1
EXAMPLE #3 (1 of 3)
DW_CUST DW_CITY DW_SQL C C C C 'CITY' E DS S S DSPLY EVAL EXTNAME(CUST) 20 1000 W_CITY W_SQL = 'SELECT * FROM + CUST WHERE CCITY = ' + '''' + W_CITY + ''''
C/EXEC SQL C+ PREPARE S1 FROM :W_SQL C/END-EXEC C/EXEC SQL C+ DECLARE C1 CURSOR FOR S1 C/END-EXEC
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
EXAMPLE #3 (2 of 3)
C/EXEC SQL C+ OPEN C1 C/END-EXEC C/EXEC SQL C+ FETCH C1 INTO :W_CUST C/END-EXEC C DOW SQLCOD <> 100 CNAME
C 'NAME IS' DSPLY C/EXEC SQL C+ FETCH C1 INTO :W_CUST C/END-EXEC C ENDDO
EXAMPLE #3 (3 of 3)
C/EXEC SQL C+ CLOSE C1 C/END-EXEC C/FREE W_SQL = 'UPDATE CUST SET + CPHONE = ' + '''555-1212''' + ' WHERE CZIP <> ''32509''' C/END-FREE C/EXEC SQL C+ EXECUTE IMMEDIATE :W_SQL C/END-EXEC C C EVAL RETURN *INLR = *ON
It is possible to read more than one record at a time, loading a MODS with values for C/EXEC at once. This is C1 FOR than ROWS several records SQL FETCHmore efficient 10 the standard one record at a time method.INTO :W_FIELDS C+
C/END-EXEC
ENDDO
C C
ITEM?' ITEM?'
W_VALUE1 W_VALUE2
W_SQL= W_DELETE + W_FROM + W_WHERE PREPARE @S1 from :W_SQL EXECUTE @S1 USING :W_VALUE1, :W_VALUE2
C ITEM?' DSPLY W_VALUE1 C EVAL %LEN(W_COMP) = %LEN(%TRIM( C W_VALUE1)) C EVAL W_COMP = %TRIM(W_VALUE1) C EVAL W_SQL= W_DELETE + W_FROM + C W_WHERE C/Exec SQL PREPARE @S1 from :W_SQL C/End-exec C/Exec SQL EXECUTE @S1 USING :W_COMP C/End-exec
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
EXAMPLE #4 (1 of 2)
DW_CUST DW_CITY DW_STATE DW_SQL C C C C C 'CITY' 'STATE' E DS S S S DSPLY DSPLY EVAL EXTNAME(CUST) 20 2 1000 W_CITY W_STATE W_SQL = 'SELECT * FROM + CUST WHERE CCITY = ' + '? OR CSTATE = ?'
C/EXEC SQL C+ PREPARE S1 FROM :W_SQL C/END-EXEC C/EXEC SQL C+ DECLARE C1 CURSOR FOR S1 C/END-EXEC
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
EXAMPLE #4 (2 of 2)
C/EXEC SQL C+ OPEN C1 USING C/END-EXEC C/EXEC SQL C+ FETCH C1 INTO C/END-EXEC C C 'NAME IS' C/EXEC SQL C+ FETCH C1 INTO C/END-EXEC C C/EXEC SQL C+ CLOSE C1 C/END-EXEC C C
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
:W_CITY, :W_STATE
EVAL RETURN
*INLR = *ON
SQLWRN - String of 11, 1 byte flags. SQLSTT - A return code for the most recent SQL statement.
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
ENDO
?... SQLCOD = ?
ENDO
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
FIRST - positions cursor ON the first record in the result table. LAST - positions cursor ON the last record in the result table.
Complex SQLs
BEFORE
- positions cursor BEFORE the first record in the result table. - positions cursor AFTER the last record in the result table. - no change.
AFTER
CURRENT
RELATIVE - positions cursor on the record specified by a host variable. A negative number moves back x rows. A positive value moves forward X rows.
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
Null Values
C/EXEC SQL C+ UPDATE DATA SET LONGNAM2 = NULL WHERE NAME IS NOT NULL C/END-EXEC
Null Values
* Single Row FETCH testing for NULL values DW_NAME DW_LONGNAM1 DW_LONGNAM2 DW_IND1 DW_IND2 S S S S S 30 120 120 5U 0 5U 0
C/EXEC SQL C+ FETCH NEXT FROM @QC1 INTO :W_NAME :W_IND1, C+ :W_LONGNAM1, :W_LONGNAM2 :W_IND2 C/END-EXEC
Null Values
* Multi Row FETCH testing for NULL values DW_FIELDS D W_NAME D W_LONGNAM1 D W_LONGNAM2 DW_IND D W_IND1 D W_IND2 D W_IND3 DS 30 120 120 DS 5U 0 5U 0 5U 0 OCCURS(10) OCCURS(10)
C/EXEC SQL FETCH @QC1 FOR 10 ROWS C+ INTO :W_FIELDS :W_IND C/END-EXEC
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
The SQLDA is used to determine what fields will be returned by a prepared SELECT statement before performing the fetch, allowing you to tailor the number and size of values used in the FETCH or OPEN statement.
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
SQL Descriptors
SQLN - The number of occurrences of SQLVAR. SQLD - Contains the number of occurrences of SQL_VAR being used. SQLVAR - Array containing descriptions of each variable being used. SQLTPE - Contains data type. Odd types are NULL capable.
SQLLEN - Contains the length of the variable. SQLRES - Reserved area for boundary alignment. SQLDATA - Contains a pointer to the address of the variables. SQLIND - Contains a pointer to an integer. A negative value indicates NULL, otherwise it is NOT NULL. SQLNAME - Contains column name, label, or system name. SQLNAMELEN - Contains the length of SQLNAME.
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993
SQL Descriptors
To use the IBM supplied SQLDA specify the following code in your program: C/EXEC SQL INCLUDE SQLDA C/END-EXEC SQL_VAR: is an array which holds the description of each variable used in a FETCH or OPEN statement. The data structure SQLVAR is used to parse the values in SQL_VAR. SQL_NUM: is a numeric constant that must be defined before the SQLDA is defined. It determines the number of variables which will be dimensioned by the SQL_VAR array.
SQL Descriptors
DESCRIBE statement INTO data-area USING SYSTEM NAMES
The USING clause determines what values are used for column labels. USING NAMES (default if USING is omitted) USING SYSTEM NAMES USING LABELS USING BOTH (NAMES and LABELS) USING ALL (NAMES, LABELS, and SYSTEM NAMES) For BOTH and ALL you must double or triple the size of the SQLVAR array.
FETCH @C1 USING DESCRIPTOR :SQLDA DOW DO EVAL SELECT SQLCOD = *ZERO SQLD X PTR_DATA = SQLDATA(X)
SQL Descriptors SQLDA is only for programs that are so dynamic that the program knows nothing about the files and fields being used, at the time it is written.
Summary
SQL contains powerful update capabilities. It provides the ability to perform updates to specific records or make massive updates to every record in a table. Statements can be created statically, or dynamically. Static SQLs offer better performance. Dynamic SQLs offer more flexibility. The pre-compiler adds a significant amount of code to the program, providing features such as the SQLCA, SQLCODE, and SQLSTATE. A prepared SELECT statement must be processed with the DECLARE, OPEN, and FETCH statements. Other prepared statements can simply be executed. Combining the flexibility of SQL with the robust RPG IV language, creates a hybrid program of great potential. Also remember that SQL is used in ODBC and JDBC. So learning other languages such as VB and JAVA, will be easier.
EMBEDDED SQL 2/10/2003 DMC Consulting, 1993