Embedded SQL (SQL EMBEBIDO en PROGRAMAS RPG)
Embedded SQL (SQL EMBEBIDO en PROGRAMAS RPG)
Embedded SQL (SQL EMBEBIDO en PROGRAMAS RPG)
in RPG Programs
Susan Gantner
[email protected]
www.Partner400.com
www.SystemiDeveloper.com Your partner in System i Education
Many RPG programmers have used interactive SQL as a tool to quickly browse data or to create
test data scenarios, but have stopped short of embedding it into their RPG programs. Come to this
session to learn why, when and how you may want to use SQL in conjunction with RPG. We will
cover the basics of embedding SQL into RPG, including using SQL cursors.
The authors, Susan Gantner and Jon Paris, are co-founders of Partner400, a firm specializing in
customized education and mentoring services for AS/400 and iSeries developers. After many years
with IBM, including several years at the Rochester and Toronto laboratories, Jon and Susan are
now devoted to educating developers on techniques and technologies to extend and modernize
their applications and development environments. This is done via on-site custom classes for
individual companies as well as conferences and user group events.
Together with her partner, Jon Paris, Susan authors regular technical articles for the IBM
publication, IBM Systems Magazine, i5 edition (formerly iSeries Magazine and eServer Magazine,
iSeries edition), and the companion electronic newsletter, i5 EXTRA (formerly iSeries Extra). You
may view articles in current and past issues and/or subscribe to the free newsletter or the magazine
at: http://www.ibmsystemsmag.com.
Susan and Jon are also partners in SystemiDeveloper, a company that hosts the RPG Summit and
DB2 Summit conferences. See SystemiDeveloper.com for more details.
This presentation may contain small code examples that are furnished as simple examples to
provide an illustration. These examples have not been thoroughly tested under all conditions. We
therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.
All code examples contained herein are provided to you "as is". THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
DISCLAIMED.
Programming Statements
DESCRIBE, PREPARE - used for dynamic embedded SQL
Logic control statements for SQL procedures, triggers and functions
(SPL)
iSeries Navigator
SQL wizards, productivity and testing tools
Graphical alternative to Interactive SQL
Note: /Free form allows embedded SQL only V5R4 and later
* No F spec needed !
D EmpNbr S 5 0
D Name S 25
D Job S 1
C/EXEC SQL
C+ SELECT NAME, POS
C+ INTO :Name, :Job
C+ FROM EMPL
C+ WHERE NBR = :EmpNbr
C/END-EXEC
/Free
EXEC SQL SELECT NAME, POS
INTO :Name, :Job FROM EMPL
WHERE NBR = :EmpNbr;
C/Exec SQL
C+ Include SrcMbrName
C/End-Exec
SELECT INTO
SELECT INTO SQL Statement is used to retrieve a single
record from the database
If your statement returns more than 1 row, you must use another form
of SELECT we will discuss later
D CustID S 7P 0
D CustName S 30A
D CustSales S 7P 0
C/EXEC SQL
C+ SELECT CUSTID,CUSTNAM, CUSTSALES
C+ INTO :CUSTID,:CUSTNAME,:CUSTSALES
C+ FROM CUSTOMER
C+ WHERE CUSTID = :InputCust
C/END-EXEC
C/EXEC SQL
C+ UPDATE empl
C+ SET sal = sal + (sal * .10)
C+ WHERE pos = 5
C/END-EXEC
D CustInfo DS
D CustID 7P 0
D CustName 30A
D CustSales 7P 0
D InputCust S 7P 0
C/EXEC SQL
C+ SELECT CUSTID, CUSTNAM, CUSTSALES
C+ INTO :CustInfo
C+ FROM CUSTOMER
C+ WHERE CUSTID = :InputCust
C/END-EXEC
D CustRec E DS ExtName(Customer)
D InputCust S 7P 0
C/EXEC SQL
C+ SELECT *
C+ INTO :CustRec
C+ FROM CUSTOMER
C+ WHERE CUSTID = :InputCust
C/END-EXEC
Nbr
10
Name
AMY
Pos
2
Sex
F
Sal
1200
Result Set
35 JOE 5 M 1000 Nbr Name
25 ANN 8 F 1550
C/EXEC SQL
C+ OPEN empcsr
C/END-EXEC
FETCH statement:
Bring rows into the program
One at a time for processing
Fields placed into host variables
one for one with fields in SELECT
C/EXEC SQL
C+ FETCH NEXT FROM empcsr
C+ INTO :number, :name, :salary
C/END-EXEC
Close Statement
Close the cursor
Cursor must be opened in order to be closed
C/EXEC SQL
C+ CLOSE empcsr
C/END-EXEC
User Modified
Source Precompile Source Compile Program
Member Member
Processed Access
SQL Stmts Plans
(temporary)
/EXEC SQL
INCLUDE SQLCA
/END-EXEC
SQLSTT or
SQLCOD or
Condition SQLSTATE Class
SQLCODE
(1st 2 positions)
Successful 0 '00'
End of Data
100 '02'
Row not found
C/EXEC SQL
C+ SELECT name INTO :nam
C+ WHERE emp = :number
C/END-EXEC
C Select
C When SQLCod < 0
C ExSR SQLError
C When SQLCod = 100
C ExSR NoMoreRows
C When ( SQLCod > 0 Or SQLWn0 <> *Blank )
C ExSR SQLWarning
C Other
C ExSR ProcessRow
C EndSL
C/EXEC SQL
C+ SELECT name INTO :nam
C+ WHERE emp = :number
C/END-EXEC
C Select
C When %Subst(SQLStt:1:2) >= '03'
C ExSR SQLError
C When %Subst(SQLStt:1:2) = '02'
C ExSR NoMoreRows
C When %Subst(SQLStt:1:2) = '01'
C ExSR SQLWarning
C Other
C ExSR ProcessRow
C EndSL
Dynamic SQL
SQL Statements built during program execution
Greater Flexibility
Summary
Practical, effective solution for applications requiring
complex data retrieval
Very flexible, functional application development tool
embedded in a HLL program
or entered interactively
C If DeleteCorp
C Eval Condition = 'Corp = ?'
C Else
C Eval Condition = 'CustNo = ?'
C EndIf
C Eval SQLStmtFld = 'Delete From Customer Where '
C + Condition
C/EXEC SQL
C+ PREPARE DynSQLStmt
C+ FROM :SQLStmtFld
C/END-EXEC
C If (SQLCod = 0) And (SQLWn0 = *Blank)
C/EXEC SQL
C+ EXECUTE DynSQLStmt
C+ Using :Cust
C/END-EXEC
C EndIf