The Natives are getting restless, an
overview of DB2 Stored Procedures
Frank Rhodes
BMC Software
September 16/17, 2015
Click to edit Master title style
Basis of Presentation
• Presentation based on research for managing schema
• This may be a different perspective than that of a DBA
who is considering using stored procedures in an
application
• This presentation is an overview of stored procedures
2
Click to edit Master title style
Topics
• History of Stored Procedures supported by DB2
• Anatomy of Stored Procedures
• The 3 types of Stored Procedures
• Advantages of one type over another
• Native SQL: Deployment and Schema Management
3
Click to edit Master title style
History of Stored Procedures in DB2
• Stored Procedures introduced in DB2v5
• Required manual insertion into catalog
• ‘Create procedure’ syntax introduced in DB2v6
• External SQL procedures introduced in DB2v7
• Native SQL procedures introduced in DB2v9
• Originally the DB2 ‘scripting’ language was C, COBOL,
etc.
4
Click to edit Master title style
LANGUAGE SQL
MODIFIES SQL DATA
DYNAMIC RESULT SETS 0
DISABLE DEBUG MODE
PACKAGE OWNER RDAMCG
ASUTIME NO LIMIT
COMMIT ON RETURN YES
(IN EMPLOYEE_NUMBER CHAR(8), IN RATE DECIMAL(6,2))
CREATE PROCEDURE MG017.SPSQN006
Anatomy of a Procedure
5
id
parms
options
(SYSPARMS)
(SYSROUTINES)
(IN EMPLOYEE_NUMBER CHAR(8), IN RATE DECIMAL(6,2))
LANGUAGE SQL
MODIFIES SQL DATA
DYNAMIC RESULT SETS 0
DISABLE DEBUG MODE
PACKAGE OWNER RDAMCG
ASUTIME NO LIMIT
COMMIT ON RETURN YES
CREATE PROCEDURE MG017.SPSQN006CREATE PROCEDURE MG017.SPSQN006 id
(IN EMPLOYEE_NUMBER CHAR(8), IN RATE DECIMAL(6,2))
parms(SYSPARMS)
Click to edit Master title style
Anatomy of a Procedure part 2
6
procedure body
comment
UPDATE MG017.TG01SS
SET SALARY = SALARY * RATE
WHERE EMPNO = EMPLOYEE_NUMBER;
UPDATE MG017.TG01SS
SET SALARY = SALARY * RATE
WHERE EMPNO = EMPLOYEE_NUMBER;
COMMENT ON PROCEDURE MG017.SPSQN006 IS
‘THIS IS A COMMENT’;
COMMENT ON PROCEDURE MG017.SPSQN006 IS
‘THIS IS A COMMENT’;
GRANT EXECUTE ON PROCEDURE MG017.SPSQN006
TO JOHNNY;
GRANT EXECUTE ON PROCEDURE MG017.SPSQN006
TO JOHNNY; grant
UPDATE MG017.TG01SS
SET SALARY = SALARY * RATE
WHERE EMPNO = EMPLOYEE_NUMBER;
COMMENT ON PROCEDURE MG017.SPSQN006
IS ‘THIS IS A COMMENT’;
procedure body
comment
(SYSROUTINEAUTH)
Click to edit Master title style
Three types of Stored Procedures
• External
• External SQL
• Native SQL
7
Click to edit Master title style
External Procedure – example
CREATE PROCEDURE ACMQA01.SPOP0001
( IN CODE CHAR (3)
,INOUT DESCRIP CHAR(10))
LANGUAGE C
EXTERNAL NAME SPOP1
PARAMETER STYLE DB2SQL
ASUTIME NO LIMIT
PROGRAM TYPE MAIN
COMMIT ON RETURN NO;
8
LANGUAGE C
EXTERNAL NAME SPOP1
Language
Load Module Name
No procedure body
LANGUAGE C Language
EXTERNAL NAME SPOP1 Load Module Name
Click to edit Master title style
Storing of procedures
• External Procedure
9
SYSROUTINES
SYSPARMS
SYSROUTINEAUTH
Click to edit Master title style
External Procedure continued
Languages supported by DB2
• Assembler
• C
• COBOL
• PL/I
• REXX
• JAVA
• Used to support COMPJAVA (compiled JAVA) but no more
10
Click to edit Master title style
How to Create an External Procedure
• Create a source module in a supported language
• Pre-compile, Compile and Link the source, creating a
Load Module and DBRM member
• Bind the DBRM member into a package (if contains SQL)
• Execute ‘Create Procedure’ DDL
11
Click to edit Master title style
Native SQL Procedures – advantages
• Use same SPL as External SQL procedures but MUCH
easier to build
• No DSNTPSMP – hooray!
• No WLM environment – hooray!
• Not converted to C program; SPL text is executed
interpretively
• Procedure body is stored in 2 mg CLOB column in
SYSROUTINES
21
Click to edit Master title style
Native SQL Procedures – advantages
• Besides ease of use there are cost savings and
performance advantages
• Native procedures may use the ZIIP engine when invoked from a
off mainframe client
• Native procedures run in DBM1 – especially good for data access
• But not so good for procedural processes
22
Click to edit Master title style
Native SQL Procedure - example
CREATE PROCEDURE MG017R.SPSQN047
(IN EMPLOYEE_NUMBER CHAR (8) FOR SBCS DATA
,OUT RATE DECIMAL (6,2)
)
VERSION V1
…
BEGIN
SELECT SALARY INTO RATE
FROM MG017R.TG01SS
WHERE EMPNO = EMPLOYEE_NUMBER;
END
23
procedure body
Click to edit Master title style
Storing of procedures
• External Procedure
• External SQL
• Native SQL
24
SYSROUTINES
SYSPARMS
SYSROUTINEAUTH
SYSROUTINE_SRC
SYSROUTINE_OPTS
SYSPACKAGE
SYSENVIRONMENT
Click to edit Master title style
Native SQL Procedures – versions
• One procedure can have many versions. New versions
are created using the ALTER statement
25
Myproc
V1CREATE PROCEDURE Myproc …
ALTER PROCEDURE Myproc ADD
VERSION V2 …Myproc
V2
Myproc
V3ALTER PROCEDURE Myproc ADD
VERSION V3 …
Click to edit Master title style
Native SQL Procedures – Active Version
• Only one version is ACTIVE at one time.
26
Myproc
V1
Myproc
V2
Myproc
V3
call
Myproc
call
Myproc
call
Myproc
PGM 2
PGM 1
Trigger
Myproc
V1
Myproc
V2
ALTER PROCEDURE Myproc ACTIVATE VERSION V2
Click to edit Master title style
Native SQL Procedures – How Versions Might Be Used
• Fallback if errors occur when procedure is in production
27
Myproc
V1
Myproc
V1
Myproc
V2
Myproc
V2
Myproc
V3
call
Myproc
call
Myproc
call
Myproc
PGM 2
PGM 1
Trigger
ALTER PROCEDURE Myproc ACTIVATE VERSION V1
Click to edit Master title style
Native SQL Procedures – Testing an inactive version
28
Myproc
V1
Myproc
V1
Myproc
V2
Myproc
V2
call
Myproc
call
Myproc
call
Myproc
PGM 2
PGM 1
Trigger
strcpy(strName,”Myproc”);
EXEC SQL SET PATH Myschema;
EXEC SQL SET CURRENT ROUTINE VERSION = V3;
EXEC SQL CALL :strName (‘AAAAA’);
call
Myproc
strcpy(strName,”Myproc”);
EXEC SQL SET PATH Myschema;
EXEC SQL SET CURRENT ROUTINE VERSION = V3;
EXEC SQL CALL :strName (‘AAAAA’);
Myproc
V3Testprog
Click to edit Master title style
Native SQL Procedures – Alter Statements
• Most of the options can be changed using an ALTER
statementALTER PROCEDURE A.B VERSION V2
NOT DETERMINISTIC
READS SQL DATA CREATE
• When updating the text and some options, an ALTER
REPLACE is requiredALTER PROCEDURE A.B
REPLACE VERSION V2
( IN A SMALLINT)
LANGUAGE SQL
...
New text
29
Click to edit Master title style
Native SQL Procedures – Maintenance Considerations
31
Myproc.V1ASULIMIT 1000CREATE PROCEDURE Myproc
ASULIMIT 1000 …
ALTER PROCEDURE Myproc ADD
VERSION V2 …
Myproc.V2NO ASULIMIT
• When you add a version (or alter replace a version), the
owner is always the owner of the original version
• When you add a version (or alter replace a version), the
owner is always the owner of the original version
• When executing an Add Version or Alter Replace, the
parms must be repeated in entirety.
• Challenges with Signature changes
Click to edit Master title style
Native SQL Procedures – Signature
• What is a Signature? The following:
• Schema name
• Procedure name
• Parms and their attributes (excluding name of parm)
• Option, PARAMETER CCSID
• All versions of a native procedure have to have the same
signature
• If you need to change part of the signature, you must
change it in all versions. You must drop all versions
(using drop procedure), then recreate all supported
versions.
32
Click to edit Master title style
Native SQL Procedures – PARAMETER CCSID
• Historically, you could always put the CCSID on the
parms if you didn’t want to use the default CCSID
• Later, DB2 introduced PARAMETER CCSID which
offered a convenient way of specifying the CCSID for all
character based parameters. You no longer had to repeat
the CCSID on each parm
• Of course, if CCSID is also specified on some of the
character based parms, the CCSID on the parms must
match the CCSID in PARAMETER CCSID.
33
Click to edit Master title style
Native SQL Procedures – DROP Options
34
Myproc
V1ALTER PROCEDURE Myproc DROP
VERSION V2;
Myproc
V2
Myproc
V3DROP PROCEDURE Myproc;
Click to edit Master title style
Native SQL Procedures – Rebinds
35
Myproc
V1
ALTER PROCEDURE Myproc REPLACE
VERSION V1 …;
Myproc
V2
Myproc
V3
Automatic rebind of Native Procedure
call
Myproc
call
Myproc
call
Myproc
PGM 2
PGM 1
TriggerREBIND TRIGGER … ;
REBIND MEMBER(PGM1) …;
REBIND MEMBER(PGM2) …;
Click to edit Master title style
Native SQL Procedures – Rebinds
36
Myproc
V1
An ALTER is performed against Table Mytab.
All packages accessing Mytab become
invalidated
Myproc
V2
Myproc
V3
Table
MytabNeed to find all packages
that reference Table Mytab.
Native Procedure package will have a type
of ‘N’ vs. the type of ‘P’ most packages have.
REBIND PACKAGE(Myschema.Myproc(V1))
REBIND PACKAGE(Myschema.Myproc(V2))
REBIND PACKAGE(Myschema.Myproc(V3))
Click to edit Master title style
• BIND PACKAGE(remote_location.collection_id)
COPY(sp_schema.sp_name) COPYVER(sp_ver)
ACTION(ADD)
• Process will define the package at a remote location but
not the procedure itself.
• Used when procedure is defined on ssid A but accesses
ssid B. Needs a package defined on ssid B to work.
38
Native SQL Procedures – Bind Copy AddRemote
Local
Remote
Click to edit Master title style
• BIND PACKAGE (remote_location.new_schema)
DEPLOY (sp_schema.sp_name)
COPYVER(sp_version)
ACTION (ADD) QUALIFIER(new_qualifier)
• The command will take a local procedure and define it on
remote DDF location.
• Viable way to move procedures to production
39
Native SQL Procedures – Bind DeployRemote
Local
Remote
Click to edit Master title style
• Bind Deploy discussed previously
• Data Studio
• Not a true Change Management tool
• Combination of vendor tools
• Source Management Tools
• Schema Management Tools
40
How to Move to Production
Click to edit Master title style
Native SQL Procedures – Format
• The various formats which can be used determine the amount of
formatting which will be retained in the SPL text.
• --#SET SQLFORMAT SQL
• collapses each line of an SQL statement into a single line and discards
comments
• Not good for procedures
• --#SET SQLFORMAT SQLCOMNT
• Same as SQL except comments are retained
• --#SET SQLFORMAT SQLPL
• Retains comments and terminates each line of an SQL statement with a
line feed character (hex25)
• Good for procedures
42
Click to edit Master title style
BEGIN
DECLARE STMT CHAR(100);
DECLARE TABLE_NAME CHAR(30);
--FIRST COMMENT
SET STMT = 'DROP TABLE '||TABLE_NAME; --SECOND COMMENT
PREPARE S1 FROM STMT;
EXECUTE S1;
END
43
Native SQL Procedures – Format Examples Build with SQL
What gets saved in SYSIBM.SYSROUTINES(TEXT)
BEGIN DECLARE
STMT CHAR(100); DECLARE TABLE_NAME
CHAR(30); SET STMT = 'DROP TABLE
'||TABLE_NAME; PREPARE S1 FROM STMT;
EXECUTE S1; END
Note linefeed and comments are not maintained in the save text
Click to edit Master title style
BEGIN
DECLARE STMT CHAR(100);
DECLARE TABLE_NAME CHAR(30);
--FIRST COMMENT
SET STMT = 'DROP TABLE '||TABLE_NAME; --SECOND COMMENT
PREPARE S1 FROM STMT;
EXECUTE S1;
END
44
Native SQL Procedures – Format Examples Build with SQLCOMNT
What gets saved in SYSIBM.SYSROUTINES(TEXT)
BEGIN DECLARE
STMT CHAR(100); DECLARE TABLE_NAME
CHAR(30); --FIRST COMMENT
/n SET STMT = 'DROP TABLE '||TABLE_NAME; --SECOND COMMENT/n
PREPARE S1 FROM STMT; EXECUTE S1;
END
Note: Comments are saved with linefeeds, but no other linefeeds are
saved
Click to edit Master title style
BEGIN
DECLARE STMT CHAR(100);
DECLARE TABLE_NAME CHAR(30);
--FIRST COMMENT
SET STMT = 'DROP TABLE '||TABLE_NAME; --SECOND COMMENT
PREPARE S1 FROM STMT;
EXECUTE S1;
END
45
Native SQL Procedures – Format Examples Build with SQLPL
What gets saved in SYSIBM.SYSROUTINES(TEXT)
BEGIN\n DECLARE STMT CHAR(100);\n DECLARE TABLE_NAME CHAR(30);\n
--FIRST COMMENT\n SET STMT = 'DROP TABLE '||TABLE_NAME; --SECOND
COMMENT\n PREPARE S1 FROM STMT;\n EXECUTE S1;\nEND\n
Note the linefeeds and comments are both maintained in
the save text
Frank RhodesBMC Software
The Natives are getting Restless, an overview of DB2 Store Procedures
Click to edit Master title style
More Trivia
• Grants are at procedure level for Native procs
• Comments are at version level for Native procs
• Parm Names for External procs may be blank
• XML is not a valid datatype in DB2v9. But it is valid for
native procs in DB2v10
• Rowid is not a valid datatype for native procedures in any
version of DB2
• Datatype is carried as REAL/DOUBLE for parms instead
of FLOAT as in columns
47
Click to edit Master title style
More Trivia – II
• TIMEZONE syntax is spelled out for parms unlike the
behavior for columns.
• For external procs, if proc name is 8 bytes or less then
you do not have to specify an external name. The
external name will default to the name of the proc. The
keyword EXTERNAL needs to be included in the options.
48
Click to edit Master title style
More Trivia – III
• DB2 has option syntax for the following but does not store
values in catalog
• Scale portion of DECIMAL
• FOR UPDATE CLAUSE
• Following should be stored in SYSENVIRONMENT but
we never saw it happen
• TIME FORMAT (ISO/EUR/USA/JIS)
• DATE FORMAT (ISO/EUR/USA/JIS)
49
Click to edit Master title style
Native SQL Procedures – Maintenance Considerations
• When you alter certain options, you have to be ‘in the
same environment’ as the environment which existed for
the creation of the procedure. If not, a -4706 will be
issued. The options are
• QUALIFIER
• PACKAGE OWNER
• WLM ENVIRONMENT FOR DEBUG MODE
• OPTHINT
• SQL PATH
• DECIMAL if the value includes a comma
• Use Alter Replace to get around this restriction
50