Creating a Static Procedure for DB2

Creating a static TABLE request requires the data adapter SQL COMPILE facility; its sole function is to create static SQL code for TABLE requests. Creating a static MODIFY is an extension to the usual procedure for compiling a FOCEXEC with the FOCUS COMPILE facility. Invoke the static creation process after you complete the FOCEXEC or MODIFY procedure.

In this section:

Write the FOCEXEC

Allocate the Required DDNAMEs

Optionally Issue the SET STATIC Command

Optionally Issue the SET SSID Command

Compile the FOCEXEC

Optionally BIND the Plan for the FOCEXEC

Authorize Users to Run the Plan

Run-time Requirements

Processing and Security Overview

DB2 Static TABLE Example

DB2 Static MODIFY Example

You must allocate some additional DDNAMEs, you may need to issue the data adapter SET STATIC command, and you must execute either the FOCUS COMPILE command (for MODIFY) or the data adapter SQL COMPILE command (for TABLE). In response, the data adapter automatically creates an Assembler program with the embedded SQL required by the procedure. It then precompiles, assembles, link-edits, and, optionally, binds the program. It accomplishes all steps, including the automatic bind, from within FOCUS.

You must give some thought to the issue of plan management discussed in Plan Management in DB2 as it will affect your choice of bind option.

This section outlines the steps required to create a static procedure, and provides a description of FOCUS processing for each step. DB2 Static TABLE Example and DB2 Static MODIFY Example provide annotated examples.

Creating a static module for DB2 consists of the following steps:

  1. Write the procedure.

  1. Allocate the required DDNAMEs.

  2. Optionally issue the SET STATIC command.

  3. Optionally issue the SET SSID command.

  4. Compile the FOCEXEC.

  5. Optionally bind the plan for the FOCEXEC.

  6. Authorize users to run the plan.


Top of page

Write the FOCEXEC

Virtually any TABLE or MATCH FILE request is valid. A MODIFY procedure, however, must be compilable. Consult Static SQL Requirements and your FOCUS documentation on maintaining databases for more information on compiled MODIFY requirements.


Top of page

Allocate the Required DDNAMEs

The following chart lists the DDNAMEs you must allocate before compiling a FOCEXEC to use static SQL. You can add these allocations to your FOCUS CLIST, batch JCL, or PROFILE FOCEXEC. Note that these allocations are additions to the normal allocations for running FOCUS with DB2:

DDNAME

DCB Parameters

Description

ASMSQL

DSORG(PO)
RECFM(FB)
LRECL(80)

Target data set for the assembler source code generated by the compilation. The size of each member varies depending on the size of the TABLE requests in the FOCEXEC or the size of the MODIFY procedure and the number of columns in each table the MODIFY procedure references.

DBRMLIB

DSORG(PO)
RECFM(FB)
LRECL(80)

Target data set for the database request module generated by the compilation.

DB2LOAD

n/a

DB2 load library. The name for this library is site-specific, but usually follows the form DSNxy0.SDSNLOAD (where xy0 is DB2 Version x Release y). The DB2 load library should be the same one used by the DB2 subsystem where the bind will take place. Note: The STEPLIB allocation of the DB2 load library is still necessary.

STUBLIB

DSORG(PO)
RECFM(U)

Contains the load module created for the procedure. This data set is also required at run time.

SQLERR1

DSORG(PS)
RECFM(FB)
LRECL(130)

Contains the output of the IBM precompiler. DCB parameters are set by the precompiler. To route output to the terminal, use DA(*).

SQLERR2

DSORG(PS)
RECFM(FM)
LRECL(121)

Contains the output of the IBM assemble operation. DCB parameters are set by the assembler. To route output to the terminal, use DA(*).

SQLERR3

DSORG(PS)
RECFM(FA)
LRECL(81)

Contains the output of the IBM linkage editor. DCB parameters are set by the linkage editor. To route output to the terminal, use DA(*).

FOCCOMP

DSORG(PO)
RECFM(VB)
LRECL(32756)
BLKSIZE(32760)

For MODIFY only. Contains the compiled MODIFY procedure. This data set is also required at run time. Note: The FOCCOMP library member for a MODIFY procedure contains a record of the fact that the procedure uses static SQL. If you have both static and dynamic versions of the compiled procedure, you need two FOCCOMP libraries, one compiled with STATIC ON or NOBIND, and the other compiled with STATIC OFF.


Top of page

Optionally Issue the SET STATIC Command

The syntax for the SET STATIC command for DB2 is

SQL [DB2] SET STATIC process

where:

process
Indicates the command that invokes static processing. Valid values are as follows:

OFF is the default setting.

ON invokes static processing with automatic bind. This option does not support extended plan management (see Plan Management in DB2) or modification of any of the default bind parameters. If you require more flexibility, use the NOBIND option.

NOBIND invokes static processing without bind. Use this option if you need extended plan management, to change the default bind parameters, or to bind your programs at another time (for example, during an off peak period).

Note:


Top of page

Optionally Issue the SET SSID Command

If you request the automatic bind option and the DB2 subsystem in which you want the bind to occur is different from your site's installation default, you must issue the SET SSID command (see Environmental Commands). You can obtain the current setting for the DB2 subsystem by issuing the SQL DB2 ? command.

Note: The DB2 subsystem referenced by the SET SSID command should use the DB2 load library allocated to DDNAME DB2LOAD.


Top of page

Compile the FOCEXEC

To compile a TABLE request, use the data adapter SQL COMPILE facility; you must then use the data adapter SQL RUN facility to run it.

To compile a MODIFY procedure, use the FOCUS COMPILE facility.


Top of page

Reference: The SQL COMPILE and SQL RUN Commands for TABLE

The data adapter provides facilities for creating and running static TABLE procedures. See Using the SQL COMPILE and SQL RUN Facilities for a complete description of the SQL COMPILE command, the SQL RUN command, SQL COMPILE and SQL RUN processing, host variable length considerations, and host variable placement.

FOCEXECs compiled with the Static SQL facility do not support the use of the LOAD command; to execute them, you must use the SQL RUN command.


Top of page

Syntax: How to Compile a Static MODIFY Procedure on MVS

Before compiling the request, be sure to allocate all input and output files, such as transaction files or log files, that the MODIFY will use at run time. Any COMBINE structures must be in effect both before compiling the MODIFY and at run time.

Issue the COMPILE command

COMPILE focexec

where:

focexec
Is the name of the MODIFY procedure to compile.

You do not have to recompile or rebind FOCUS applications for different releases of a FOCUS Version. This saves time and effort.

For example, a FOCUS static MODIFY procedure compiled and bound in Version 7.0 Release 8, does not have to be recompiled or rebound for FOCUS Version 7.0 Release 9, or vice versa. However, the procedure must be recompiled and rebound for a different FOCUS Version (for example, FOCUS Version 7.2).

Note: The 'AS module' extension to the COMPILE command is not supported for static SQL procedures. The module name must be identical to the FOCEXEC name.

FOCEXECs compiled with the Static SQL facility do not support the use of the LOAD command; to execute them you must use the RUN command.


Top of page

Optionally BIND the Plan for the FOCEXEC

This step is only necessary if you issued the SET STATIC command with the NOBIND option.

You may choose to bind one application plan for each FOCEXEC (basic plan management) or to combine several FOCEXECs into one application plan (extended plan management). Also use this option to supply bind parameters (such as isolation level) other than the default. Plan Management in DB2 discusses plan management, and Introduction to Relational Data Adapters, introduces bind concepts.

Issue the BIND command outside the FOCUS environment, using one of the methods supplied by IBM. Your BIND may include one or more FOCEXEC DBRMs.

For example:

BIND PLAN (BIGPLAN) MEM(FEX1 FEX2 FEX3 FEX4) ACTION(ADD) ISOLATION(CS)

For a full explanation of bind methods and parameters, consult the IBM DB2 Command and Utility Reference.


Top of page

Authorize Users to Run the Plan

Issue the SQL GRANT EXECUTE command to authorize users to execute the application plan created with the previous steps. This example shows how to issue the command from within a FOCUS session:

SQL DB2 GRANT EXECUTE ON PLAN BIGPLAN TO USER1, USER2

For more information on GRANT, consult the IBM DB2 SQL Reference.


Top of page

Run-time Requirements

In addition to the allocations required for FOCUS and the data adapter outlined in Invoking Relational Data Adapters, you must include allocations for the FOCCOMP (for MODIFY only) and STUBLIB libraries allocated to DDNAMEs FOCCOMP and STUBLIB.

If you use extended plan management (see Extended Plan Management), you must issue the data adapter SET PLAN command before running any of the procedures included in the DB2 application plan (see Maintaining Tables With FOCUS):

SQL DB2 SET PLAN BIGPLAN

This setting overrides the plan for the FOCUS dynamic data adapter. Extended Plan Management explains options for resetting the plan at the conclusion of your procedures.

You can use the SQL DB2 ? command to view all current data adapter plan settings, for example:

   SQL DB2 SET PLAN BIGPLAN

SQL DB2 ?
>
.
.
.
(FOC1448) ACTIVE PLAN FOR CALL ATTACH IS - :
(FOC1459) USER SET PLAN FOR CALL ATTACH IS - : BIGPLAN
(FOC1460) INSTALLATION DEFAULT PLAN IS - : P7029910
.
.
.

The most recently issued request to DB2 sets the Active Plan. If the thread is closed or marked inactive, no value displays for Active Plan. Your SET PLAN command determines the User Set Plan.

Note: To execute a static MODIFY procedure, you do not need the Access File library (allocated to DDNAME FOCSQL) at run time if you will not be accessing DB2 dynamically during the FOCUS session. The Access File library is always required when executing a static TABLE FOCEXEC. The Master File library (allocated to DDNAME MASTER) is required for all static and dynamic access.


Top of page

Processing and Security Overview

When you run a compiled FOCEXEC procedure, the data adapter searches the STUBLIB load library for a load module of the same name and loads it, if it exists.

When you run a compiled MODIFY procedure, FOCUS loads the FOCCOMP library member into virtual memory. If the flag setting in this member indicates that this is a static procedure, the data adapter searches the STUBLIB load library for a load module of the same name. The data adapter compares timestamps in the FOCCOMP and STUBLIB members for consistency.

Next, FOCUS connects to DB2 and opens a thread to the application plan of the same name as the static procedure (you can use the SET PLAN command to override this). DB2 compares the owner, program name, and timestamps in the STUBLIB member and the DB2 application plan for consistency.

This process verifies that the compiled MODIFY (FOCCOMP member), the program load module (STUBLIB member), and the DB2 application plan are valid and that no substitutions have been made.


Top of page

DB2 Static TABLE Example

The following annotated example illustrates the creation and automatic bind of a static FOCEXEC procedure named FEX1. It uses the following multi-table Master and Access File, EMPLOYEE, that relates two tables, PAYROLL and COURSES.

EMPLOYEE Master File:

FILENAME=EMPLOYEE,    SUFFIX=SQLDS,$
SEGNAME= PAYROLL, SEGTYPE=S0,$
FIELD= EMP_ID, EMP_ID, A5, A5, MISSING=OFF,$
FIELD= SALARY, SALARY, P13.2, P6, MISSING=OFF,$
SEGNAME= COURSES, SEGTYPE=S0,$
FIELD= EMP_NUM, EMP_NUM, A5, A5, MISSING=OFF,$
FIELD= COURSE_NUM, COURSE_NUM, A5, A5, MISSING=OFF,$
FIELD= POINTS, POINTS, I3, I2, MISSING=OFF,$

EMPLOYEE Access File:

SEGNAME=PAYROLL, TABLENAME="USER1"."PAYROLL",
KEYS=1, WRITE=YES, KEYORDER=LOW,$
SEGNAME=COURSES, TABLENAME="USER1"."COURSES",
KEYS=2, WRITE=YES, KEYORDER=LOW,
KEYFLD=EMP_ID, IXFLD=EMP_NUM,$

Notice that the FEX1 FOCEXEC explicitly sets OPTIMIZATION, even though it requires the default value, ON:

SQL DB2 SET OPTIMIZATION ON
TABLE FILE EMPLOYEE
PRINT EMP_ID SALARY POINTS
WHERE SALARY GE &SALARY
END

The numbers 1 through 6 refer to the explanatory notes that follow the example. Also included for your information are the options FOCUS uses for each step (for example, Assembler options). Please refer to the appropriate IBM manual for an explanation of these parameters and their possible settings.

      SQL DB2 SET SSID DB2P

1. SQL DB2 COMPILE FEX1 SALARY=50000
END
2. (FOC1472) STATIC SQL PROGRAM CREATED SUCCESSFULLY
3. (FOC1473) STATIC SQL PROGRAM PREPROCESSED. RETURN CODE IS: 4
4. (FOC1474) STATIC SQL PROGRAM ASSEMBLED. RETURN CODE IS : 0
5. (FOC1476) STATIC SQL PROGRAM LINKED. RETURN CODE IS : 0
DSN
6. BIND PLAN(FEX1) MEM(FEX1) ACTION(REPLACE) ISOLATION(CS)
DSNT252I - BIND OPTIONS FOR PLAN FEX1
ACTION ADD
OWNER USER1
VALIDATE RUN
ISOLATION CS
ACQUIRE USE
RELEASE COMMIT
EXPLAIN NO
DSNT253I - BIND OPTIONS FOR PLAN FEX1
NODEFER PREPARE
CACHESIZE 1024
QUALIFIER USER1
CURRENTSERVER
CURRENTDATA NO
DEGREE 1
SQLRULES DB2
DISCONNECT EXPLICIT
DSNT200I - BIND FOR PLAN FEX1 SUCCESSFUL
DSN
END

The steps in the process are:

  1. Issue the SQL COMPILE command.

  1. Create the Assembler program.

  1. Precompile the Assembler program.

Note:

  1. Assemble the program.

  1. Link-edit the program.

  1. Optional automatic BIND.

Note:

FOCEXECs compiled with the Static SQL for TABLE facility do not support the use of the LOAD command; to execute them you must use the SQL DB2 RUN command.


Top of page

DB2 Static MODIFY Example

The following annotated example illustrates the creation and automatic bind of a static MODIFY procedure named MOD1. The numbers 1 through 7 refer to the explanatory notes that follow the example.

Also included for your information are the options FOCUS uses for each step (for example, Assembler options). Please refer to the appropriate IBM manual for an explanation of these parameters and their possible settings.

    SQL DB2 SET SSID DB2P

SQL DB2 SET STATIC ON

1. COMPILE MOD1

EMPLOYEE ON 02/27/91 at 14.26.57
2. (FOC1472) STATIC SQL PROGRAM CREATED SUCCESSFULLY:
3. (FOC1473) STATIC SQL PROGRAM PREPROCESSED. RETURN CODE IS: 4
4. (FOC1474) STATIC SQL PROGRAM ASSEMBLED . RETURN CODE IS: 0
5. (FOC1476) STATIC SQL PROGRAM LINKED . RETURN CODE IS: 0

DSN SYSTEM(DB2P)
6. BIND PLAN(MOD1) MEM(MOD1) ACTION(ADD) ISOLATION(RR)
DSNT252I - BIND OPTIONS FOR PLAN MOD1
ACTION ADD
OWNER PMSEF
VALIDATE RUN
ISOLATION RR
ACQUIRE USE
RELEASE COMMIT
EXPLAIN NO
DSNT253I - BIND OPTIONS FOR PLAN MOD1
NODEFER PREPARE
DSNT200I - BIND FOR PLAN MOD1 SUCCESSFUL
END
7.  COMPILED...

The steps in the process are:

  1. Issue the COMPILE command.

  1. Create the Assembler program.

  1. Precompile the Assembler program.

Note:

  1. Assemble the program.

  1. Link-edit the program.

  1. Optional automatic BIND.

Note:

  1. Compile the FOCEXEC.


Information Builders