Embedded SQL The IBM I Way
Embedded SQL The IBM I Way
Dan Cruikshank
Agenda
Is There a Difference?
You
want
this!!!
Rules for Embedding SQL in HLL programs
Each SQL statement must begin with EXEC SQL and end
with a semicolon (;)
The EXEC SQL keywords must be on the same line
– The SQL statement can be on the same line or multiple lines
Embedded SQL
referencing cursors
cannot be re-used.
VAD for SQL Modules and Service Programs
Custom SQL
Module
Service Program
SQL and HLL Enhancements
– New SQL statements allow simplification of result set consumption by host centric
programs using embedded SQL
• ASSOCIATE LOCATORS and ALLOCATE CURSOR
Allows common stored procedures for both host centric and external
applications
– Result set can be returned to caller (RPG/COBOL) or client (Java, .NET, PHP,
etc.)
Conceptual use of result set consumption
Data Flow
Result Set
VAD for Result Set Consumption
Cursor names
cannot be
variables
Procedure names
and result set
locators can be
variables. Diagnostics and
error handling
Called procedure
result set can be Service program can contain
consumed by other multiple modules
applications.
Using Host Variables
Host Variable
– Single field commonly used to:
• specify a value in the predicate of a search condition
• replace a literal value in an expression
• Provide a target to receive a value from an INTO clause
Host Structure
– A group of host variables commonly used to:
• Specify values for VALUES clause (INSERT), SET clause (UPDATE)
• Provide a target to receive multiple values from a FETCH or SELECT
INTO
Host Structure Array
– A multiple occurrence data structure or array
• Commonly used for blocked FETCH and INSERT statements
• Currently not supported in SQL PL
SQL Descriptor
– A flexible Host Structure
SQL Enhancements: Global Variables
Result
Set
VAD for Global Variables
Reusable
Application service imports
exports global global variable
variable.
Reusable service
GetGV may not be program module
required in most SQL contains setters and
Statements as GV can getters.
be referenced directly.
DYNAMIC SQL
Advantages of Dynamic SQL
Parameter Markers
– A placeholder (?) for substitution variables used in dynamic
SQL statements
SQL Descriptor Areas
What are they? Where could they be used?
An SQL descriptor area is used to Column lists
contain information about a Data for VALUES on INSERT or
dynamic SQL statement UPDATE
A set of variables are used to Data for WHERE clauses
communicate this information Example
between SQL and your program sqlString = ‘DELETE FROM T1
– Think externally described data WHERE EMPNO = ?’;
structure with a variable file name
EXEC SQL PREPARE S1 USING SQL
The meaning of the information in
DESCRIPTOR :sqlDescriptor
the descriptor area depends on the
FROM :sqlString;
type of statement
– SELECT or non-SELECT EXEC SQL SET SQL DESCRIPTOR
(UPDATE, INSERT, etc.) :sqlDescriptor DATA = :vEmpno;
EXECUTE S1 USING
:sqlDescriptor;
Simplified SQL Descriptor Usage
Process •
• UPDATE…SET
• PREPARE Statement-name FROM
(edlevel ) = (?)
:sqlString
• SELECT…WHERE
• SET SQL DESCRIPTOR GLOBAL
:inputParms COUNT :columns
workdept = ?
• DESCRIBE INPUT USING SQL
• CALL proc (?,?,?)
DESCRIPTOR GLOBAL :inputParms
Statements • SET SQL DESCRIPTOR GLOBAL
:inputParms VALUE N DATA =
:hostvar
• EXECUTE Statement-name USING
SQL DESCRIPTOR GLOBAL
:inputParms Descriptors
VAD Dynamic SQL Application Overview
Dynamic SQL
VAD Dynamic SQL – Setters and Getters
1. *INZSR is used to allocate the global
descriptor and set the name in a global
variable
2. The SQL string is set as part of the
transaction process
INSERT INTO t1 (c1,c2) VALUES(?,?)
3. SQL string and descriptor name (4) are
imported by service program
VAD Dynamic SQL Global Descriptors and Variables
1. *INZSR
2. Number of
parameter markers
3. SET DATA and
INDICATOR for each
parameter
Named And Default Parameters For SQL Procedures
Extend procedures without breaking existing callers
Simplify calling requirements by adding default values
– Parameters may be omitted or specified in any order
– Default can be expressions or global variables
Examples of using named and default parameters
– CALL SQLLE1 (‘D21’); CALL SQLLE1 (PARM2 => 16);
Conceptual usage:
CALL SQLLE1 SQLLE2 SP1
(PARM2 =>16);
Result
Set
RPG OPEN ACCESS
Open Access Structure
/Free
Setll(e) FILE1;
READE(e) FILE1;
CHAIN(e) FILE2; RPG IO operations
Update FILE1; coded as usual
/End-Free
The HANDLER Program
/COPY QOAR/QRPGLESRC,QRNOPENACC
Data structure template provided
DCL-PR Handle_Main ;
rpgIO LIKEDS(QrnOpenAccess_T);
in QOAR.
userArea LIKEDS(userArea_t)
OPTIONS(*NOPASS) ; Data structure passed by IBM I
END-PR ;) to handler program
• RUNSQL
CL • SET PROCEDURE NAME global variable SET
Pgm • CALL HLL_PROGRAM Global
Variables
HLL • HANDLER keyword assigned to file
Pgm
• OPEN Operation
GET
Hand-
• GET PROCEDURE NAME from global variable Global
• First IO Operation
ler
• CALL SQL Stored Procedure
Variables
• ASSOCIATE/ALLOCATE cursor
Result
Set
HLL Enhancements: Open Access
Open Access provides a unique opportunity for RPG programmers to access SQL
procedures with minimal change to existing programs
– Not available for COBOL
Opens up RPG‘s file I/O capabilities allowing programmers to code their own
programs (in any host language) to handle RPG operations
I/O handlers can be used to:
– Transform traditional record at a time I/O operations to SQL set based
operations
– Take advantage of SQL enhancements
Conceptual use of Open Access