Beruflich Dokumente
Kultur Dokumente
DB2 for i
Procedural SQL
Each time that an SQL Routine is created, DB2 for i uses the SQL
procedural source to generate a C program object
Developer does not need to know C code
C compiler purchase is not required
SQL MAIN
RPGLE
SUB
NO SQL
READS SQL DATA
CONTAINS SQL
NOT ATOMIC
BEGIN
ATOMIC
<declare variables>
Compound <declare conditions> Declarations
Statement <declare cursors>
<declare handlers>
Logic -
<logic > Can contain nested
compound statements
END
ATOMIC
all statements succeed or are rolled back.
COMMIT or ROLLBACK cannot be specified in the routine
must also be created with COMMIT ON RETURN YES
CASE Expression
First form: Second form:
CASE workdept CASE
WHEN 'A00' THEN WHEN vardept='A00' THEN
UPDATE department UPDATE department
SET deptname = 'ACCOUNTING'; SET deptname = 'ACCOUNTING';
WHEN 'B01' THEN WHEN vardept='B01' THEN
UPDATE department UPDATE department
SET deptname = 'SHIPPING'; SET deptname = 'SHIPPING';
ELSE UPDATE department
ELSE UPDATE department
SET deptname = 'UNKNOWN';
SET deptname = 'UNKNOWN;
END CASE;
END CASE;
Procedural SQL
Default Parameters
Parameter Defaults
DEFAULT
NULL
constant
special-register
global-register
( expression )
Examples:
CREATE TYPE partids AS CHAR(3) ARRAY[10];
CREATE TYPE intarray AS INTEGER ARRAY[5];
BEGIN
--Take the inputted region number, Region# and
--return the set of customers from that region
--via a result set
OPEN c1;
END;
BEGIN
--Take the inputted region number, Region# and
--return the set of customers from that region
--via a result set
OPEN c1;
RETURN TO CALLER
DECLARE c1 CURSOR WITH RETURN TO CALLER FOR SELECT * FROM t1
Proc 1 Proc 1
. .
. RETURN .
RETURN . .
TO
TO Proc n-1 Proc n-1
CALLER
CLIENT
Proc n Proc n
ASSOCIATE LOCATORS
Ties locator to stored procedure result set
ALLOCATE CURSOR
Defines cursor that can be used to access & process data in result
set
Once cursor allocated, program can use normal FETCH and
CLOSE operations
myloop: LOOP
FETCH mycur INTO prname, prstaff;
Resources
DB2 for i SQL Reference - http:ibm.com/systems/i/db2/books.html
http://www.ibm.com/developerworks/ibmi/library/i-sqlnaming/index.html
http://www.ibm.com/developerworks/ibmi/library/i-system_sql2/index.html
Procedural SQL
CALL SYSIBMADM.CREATE_WRAPPED (
CREATE PROCEDURE chgSalary(IN empno CHAR(6))
LANGUAGE SQL
BEGIN
UPDATE employee SET empsal = empsal*(1 + .05*empjobtype)
WHERE empid = empno; END);
Obfuscation Considerations
Obfuscated version of CREATE statement can be up to
one-third longer than original SQL statement