Difference between revisions of "SQLRPGLE"
From MidrangeWiki
(→SQL Directives) |
(→Introduction to RPG IV with Embedded SQL) |
||
Line 6: | Line 6: | ||
== Introduction to RPG IV with Embedded SQL== | == Introduction to RPG IV with Embedded SQL== | ||
SQL is a powerful tool in any programmer's skill set. It is possible to leverage this tool from within [[:Category:RPG|RPG]] (& other languages). The source member type for these programs is SQLRPGLE. | SQL is a powerful tool in any programmer's skill set. It is possible to leverage this tool from within [[:Category:RPG|RPG]] (& other languages). The source member type for these programs is SQLRPGLE. | ||
− | Here is an example of an embedded SQL statement: | + | Here is an example of an embedded SQL statement in non-free RPG: |
<pre> | <pre> | ||
C/EXEC SQL | C/EXEC SQL | ||
Line 12: | Line 12: | ||
C/END-EXEC | C/END-EXEC | ||
</pre> | </pre> | ||
+ | And again in V5r4+ free-form RPG: | ||
+ | <pre> | ||
+ | exec sql select * from mylib/myfile; | ||
+ | </pre> | ||
+ | |||
[[Category:Quick Reference]] | [[Category:Quick Reference]] | ||
+ | |||
== SQL Directives == | == SQL Directives == | ||
<table style="table-layout: auto; border-width: thin; border-color: navy; border-style: ridge inset" class="sortable"> | <table style="table-layout: auto; border-width: thin; border-color: navy; border-style: ridge inset" class="sortable"> |
Revision as of 17:27, 10 August 2007
Contents
RPG IV with Embedded SQL
Introduction to RPG IV with Embedded SQL
SQL is a powerful tool in any programmer's skill set. It is possible to leverage this tool from within RPG (& other languages). The source member type for these programs is SQLRPGLE. Here is an example of an embedded SQL statement in non-free RPG:
C/EXEC SQL C+ SELECT * FROM mylib/myfile C/END-EXEC
And again in V5r4+ free-form RPG:
exec sql select * from mylib/myfile;
SQL Directives
SQL Directives | ||
---|---|---|
Directive | Function performed | |
SELECT | The Select directive is used to extract data from a Physical file or Table | |
DECLARE | The Declare Cursor directive is used to define a data path in your program | |
OPEN | The Open Cursor directive is used to open a data path defined by a Declare Cursor directive. | |
CLOSE | The Close Cursor directive is used to close a data path opened by a Open cursor directive | |
FETCH | The Fetch directive is used to extract records from a data path opened via the Open Cursor directive | |
PREPARE | The Prepare directive is used to prepare an dynamic SQL statement for execution | |
EXECUTE | The Execute directive is used to execute an dynamic SQL statement prepared via the Prepare directive | |
EXECUTE IMMEDIATE | The Execute Immediate directive is used to execute an SQL statement directly & does not require a Prepare directive | |
DELETE | The Delete directive is used to delete data from a Physical file or Table | |
DROP TABLE | The Drop Table directive is used to delete a Physical file or Table | |
CREATE TABLE | The Create Table directive is used to create a Physical file or Table | |
ALTER TABLE | The Alter Table directive is used to change file & field attributes in a Physical file or Table | |
UPDATE | The Update directive is used to update data from a Physical file or Table |
Sample Embedded SQL program
H ActGrp(*CALLER) H DftActGrp(*NO) D OpenCursor PR n D FetchCursor PR n D CloseCursor PR n D MyLib s 10a D MyFile s 10a /free *inlr=*on; if not OpenCursor(); // perform error routine to alert the troops // ... Else; Dow FetchCursor(); // putting the fetchcursor on the do loop allows the user of // iter, and thus iter will not perform an infinite loop // normal processing here... EndDo; CloseCursor(); EndIf; return; /end-free P OpenCursor B D OpenCursor PI like(ReturnVar) D ReturnVar s n // The immediately following /EXEC SQL is SQL's version of RPG's H Spec // It is never executed. Just used at compile time. C/EXEC SQL C+ Set Option C+ Naming = *Sys, C+ Commit = *None, C+ UsrPrf = *User, C+ DynUsrPrf = *User, C+ Datfmt = *iso, C+ CloSqlCsr = *EndMod C/END-EXEC C/EXEC SQL C+ Declare C1 cursor for C+ Select System_Table_Schema as library, C+ System_Table_Name as file C+ from qsys2/systables C/END-EXEC C/EXEC SQL C+ Open C1 C/END-EXEC /free Select; When SqlStt='00000'; return *on; Other; return *off; EndSl; /end-free P OpenCursor E /eject P FetchCursor B D FetchCursor PI like(ReturnVar) D ReturnVar s n C/EXEC SQL C+ Fetch C1 into :MyLib, :MyFile C/END-EXEC /free Select; When sqlstt='00000'; // row was received, normal ReturnVar=*on; When sqlstt='02000'; // same as %eof, sooner or later this is normal ReturnVar=*off; Other; // alert the troops! ReturnVar=*off; EndSl; return ReturnVar; /end-free P FetchCursor E /eject P CloseCursor B D CloseCursor PI like(ReturnVar) D ReturnVar s n C/EXEC SQL C+ Close C1 C/END-EXEC /free Select; When sqlstt='00000'; // cursor was closed, normal ReturnVar=*on; Other; // alert the troops! ReturnVar=*off; EndSl; return ReturnVar; /end-free P CloseCursor E