Welcome to:
Program Preparation
© Copyright IBM Corporation 2005
3.1.1
Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit Objectives
After completing this unit, you should be able to:
Identify the additional steps necessary to prepare a program that
contains embedded SQL for execution
Describe the functions of the DB2 PRECOMPILE and BIND
processes
Describe factors relevant to the BIND process, including
RUNSTATS positioning, package status, parameters, and
authorization requirements
© Copyright IBM Corporation 2005
The Big Picture
Source
Includes
PRECOMPILE
Source Modified
w/out SQL DBRM/Bindfile
Source
COMPILER BIND
Libraries Object file
LINKER
Executable Package
© Copyright IBM Corporation 2005
Write the Source
EXEC SQL INCLUDE SQLCA END-EXEC.
EXEC SQL INCLUDE EMP END-EXEC.
...
EXEC SQL
SELECT FIRSTNME, LASTNAME, PHONENO
INTO :FIRSTNME,:LASTNAME,:PHONENO
FROM EMP
WHERE EMPNO = :EMPNO
END-EXEC.
COBOL
EXEC SQL INCLUDE SQLCA;
EXEC SQL INCLUDE EMP;
...
EXEC SQL
SELECT FIRSTNME, LASTNAME, PHONENO
INTO :firstnme,:lastname,:phoneno
FROM EMP
WHERE EMPNO = :empno;
C or PL/I
© Copyright IBM Corporation 2005
The Big Picture - Precompile
Source
Includes
PRECOMPILE
Source Modified DBRM/Bindfile
w/out SQL Source
COMPILER BIND
Libraries Object file
LINKER
Executable Package
© Copyright IBM Corporation 2005
Precompile
Program ...
Source EXEC SQL
SELECT
...
Precompile
Include
Member
...
Messages
CALL DBRM/
DSN...
... Bindfile
© Copyright IBM Corporation 2005
Bindfile
A Bindfile
contains the program's source SQL statements
A Bindfile
Contains extracted, parsed SQL source
Is stored as a filesystem file
One member or file created per precompile
Will become input to BIND
© Copyright IBM Corporation 2005
The Big Picture - Compile and Link
Source
Includes
PRECOMPILE
Source Modified
w/out SQL DBRM/Bindfile
Source
COMPILER BIND
Libraries Object file
LINKER
Executable Package
© Copyright IBM Corporation 2005
Compile and Link
P
R
E
C C
O O
Source File M M
P P Object File
I I L
L Modified L I
E Source File E N
R R K
E Load Module/
R Executable
File
Include
Files Libraries
© Copyright IBM Corporation 2005
The Big Picture - Bind
Source
Includes
PRECOMPILE
Source Modified
w/out SQL DBRM/Bindfile
Source
COMPILER BIND
Libraries Object file
LINKER
Executable Package
© Copyright IBM Corporation 2005
Bind
DBRM /BINDFILE
DB2
Catalog Bind Package
Validates SQL
CREATE OBJECT
TABLE DEFINITIONS Resolves table names
GRANT ... SECURITY Checks privileges
TO ... DEFINITIONS
Chooses an access path based
RUNSTATS
UTILITY
STATISTICS on catalog statistics
SQL Stores source SQL in catalog
STATEMENTS
DB2
Directory
Package
© Copyright IBM Corporation 2005
The Big Picture - Run
Source
Includes
PRECOMPILE
Source Modified
w/out SQL DBRM/Bindfile
Source
COMPILER BIND
Libraries Object file
LINKER
Executable Package
© Copyright IBM Corporation 2005
Run
D S
A E
T R Tables
Load Module/
Executable A V
B I Package
File
A C
S E Database
E S
© Copyright IBM Corporation 2005
Associating Load Modules and Packages
myapp source
PRECOMPILE
CT Consistency Token
CT
myapp
JACK
DBRM (z/OS)
modified or
myapp source myapp bindfile (UDB)
COMPILE BIND
and LINK
JACK load module package
JILL
myapp no match or not found myapp
JACK plan -805 JACK
CT no match dbrm CT
-818
© Copyright IBM Corporation 2005
Precompiling versus Binding
myapp
...
SELECT ...
FROM PAYROLL
...
PRECOMPILE
COMPILE
BIND
& LINK
JACK load module package JILL
SELECT ...
FROM JILL.PAYROLL
SYSIBM.SYSPACKAGE
CREATOR OWNER NAME
JILL JILL MYAPP
© Copyright IBM Corporation 2005
Adjusting Qualifier and Owner
Jill issues:
db2 bind myapp.bnd qualifier U1 owner U2
Unqualified SQL uses U1 schema
U2 owns package
Can drop, rebind, grant privileges
© Copyright IBM Corporation 2005
Bind using QUALIFIER Option
Program coded:
EXEC SQL
SELECT LASTNAME FROM EMPLOYEE
BIND PACKAGE ... QUALIFIER PATTI
Program executes:
EXEC SQL
SELECT LASTNAME FROM PATTI.EMPLOYEE
© Copyright IBM Corporation 2005
DB2 Optimizer
SQL STATEMENTS
Optimizer checks authorizations, determines
what data is requested, and from statistics,
determines an access plan.
ALTERNATE STATISTICS
COST DB2
ALGORITHMS CATALOG
Then selects one
The Optimizer Chooses:
PACKAGE
(Access Plan) The order in which tables are accessed
Which index to use
The join method
The type of scan required
© Copyright IBM Corporation 2005
BIND Access Strategy
SELECT * FROM TEMPL
WHERE SEX = 'F'
AND ( JOBCODE = 54 OR SALARY > 1000 )
AND WORKDEPT IN ('E11', 'D14')
Which search columns have indexes?
What is the I/O cost if an index is used?
What if an index is not used?
In which order should the tests be
performed?
*** RUNSTATS utility produces statistics ***
*** used in developing access strategy ***
© Copyright IBM Corporation 2005
Implicit Rebinding
Implicit rebinding is automatic at next execution
and triggered if:
Drop object referenced in package
Drop primary key on table referenced in package
Drop/add referential constraint on parent or child
table referenced in package
Revoke privilege required by owner to execute
static SQL statement embedded in package
Packages catalog information:
OWNER or
NAME or PKGNAME
BOUNDBY
VALID ...
Y can run without rebind
N cannot run without rebind
authorization id of package binder
© Copyright IBM Corporation 2005
Explicit Rebinding
REBIND package-name
PACKAGE
NEW INDEXES AVAILABLE
STATISTICS IN CATALOG HAVE CHANGED
CONTROL WHEN INVALID PACKAGES ARE BOUND
© Copyright IBM Corporation 2005
Privileges Required for Programming
Action Privileges Required
Create a new package BINDADD privilege
Privileges need to execute each
static SQL statement
CREATEIN (z/OS) or PACKADM (z/OS)
Modifying an existing package BIND privilege on package
Privileges need to execute each
static SQL statement
Re-create an existing package BIND privilege on package
Execute a package EXECUTE privilege on package
© Copyright IBM Corporation 2005
Protecting Resources Through Programs
UNIT TEST
PGM XX
CLP
..
FUNCTION
DELETE
FROM EXEC SQL TEST
PAYROLL
DELETE FROM
PAYROLL PROMOTION
WHERE ID=
:HOSTV
PRODUCTION
..
? USERA
GRANT EXECUTE ON PLAN XX TO USERA
© Copyright IBM Corporation 2005
Precompile
D S
P A E
R Tables
T R Indexes
E A V
C B I Package
O A C
myapp.sqc M
P
S E Database
E S
I
Source File L
E myapp.c
R
Modified
Source File
© Copyright IBM Corporation 2005
Precompile with Bindfile Option
P
R
E myapp.bnd
C
O
Bind File
M
myapp.sqc P
I
Source File L
myapp.c
E
R
Modified Source File
© Copyright IBM Corporation 2005
DB2 BIND REOPT Option
New choices for reoptimizing static and dynamic SQL
statements using REOPT parameter to bind:
NONE - Default estimates are used, based on estimates for host
variables, special registers and parameters (Pre-V8.2 approach,
and still the default)
ONCE - Reoptimized with values when first used
ALWAYS - Reoptimized every time that the statement is used
© Copyright IBM Corporation 2005
The Big Picture
Source
Includes
PRECOMPILE
Source
Modified DBRM/Bindfile
w/out SQL
Source
COMPILER BIND
Libraries Object file
LINKER
Executable Package
© Copyright IBM Corporation 2005
Unit Summary
Having completed this unit, you should be able to:
Identify the additional steps necessary to prepare a program that
contains embedded SQL for execution
Describe the functions of the DB2 PRECOMPILE and BIND
processes
Describe factors relevant to the BIND process, including
RUNSTATS positioning, package status, parameters, and
authorization requirements
© Copyright IBM Corporation 2005