You are on page 1of 8

Stored Procedures

• A stored procedure is a program which runs inside a DB2 subsystem and can
update DB2 objects. It can be in any language, but for our purposes will be
written in Cobol. It can be called from application programs which connect with
DB2 including MVS batch, CICS and web applications. Pre-defined. parameters
are passed to and from the stored procedure.

• Stored procedures must be defined to DB2 by means of a "CREATE


PROCEDURE" DDL statement. This statement includes the definitions of the
parameters passed to and from the stored procedure.

Figure 1:

CREATE PROCEDURE SFIIDSVR


( IN CALL_TYPE CHAR(08)
,IN SYSTEM_NAME CHAR(08)
,IN USERID CHAR(08)
,IN TIME TIMESTAMP
,IN SYSTEM_ID CHAR(50)
,IN INDIVIDUAL_ID CHAR(09)
,IN REFERRED_ID CHAR(09)
,IN STATUS_IN CHAR(09)
,IN ACCESS_FLAG_IN CHAR(01)
,IN CLOAKED_FLAG_IN CHAR(01)
,IN ISA_DATA CHAR(220)
,IN QUALS_COUNT_IN SMALLINT
,IN QUALIFIERS_IN VARCHAR(1080)
,OUT STATUS_OUT SMALLINT
,OUT RETURN_CODE SMALLINT
,OUT RETURNEE_COUNT SMALLINT
,OUT RETURNEE_DATA VARCHAR(30822)
)
LANGUAGE COBOL
DYNAMIC RESULT SET 0
EXTERNAL NAME SFIIDSVR
PARAMETER STYLE DB2SQL
COLLID SP2ECOLL
STAY RESIDENT YES
NO WLM ENVIRONMENT;

• Stored procedures are compiled and then linkedited into special loadlibs. There
are some specific compile and link parameters required for stored procedures.
The PPS production loadlib for stored procedures is
A3257.PPS.PROD.SPLOAD. The DBRM members are saved in
A3257.PPS.PROD.SPDBRM. In Endevor the stored procedure processing
group is COBSP.

• The loadlib for stored procedures must be defined to DB2 in STEPLIB of the
DB2xSPAS job (SPAS means Stored Procedure Address Space)

• Stored procedures are bound as packages into specific stored procedure


collection IDs, eg. SP2ACOLL.

• After a stored procedure has been recompiled, it is necessary to stop and restart
the procedure using the DB2 COMMANDS dialog (DB2.7). Occasionally after an
abend it may be necessary to restart a stored procedure in the same way.
Example:
-STOP PROCEDURE(SFHRSDD.SFIIDSVR)
-START PROCEDURE(SFHRSDD.SFIIDSVR)

Calling Stored Procedures

• Any DB2 COBOL program can call a stored procedure which has been correctly
defined, compiled and bound using an "EXEC SQL CALL" command.
• Other non-COBOL applications can call the stored procedure using the DB2
syntax for the application type.

Figure 2:

EXEC SQL
CALL SFIIDSVR(:PRM-CALL-TYPE-IN
,:PRM-SYSTEM-NAME-IN
,:PRM-USERID-IN
,:PRM-TIME-IN
,:PRM-SYSTEM-IID-IN
,:PRM-INDIVIDUAL-ID-IN
,:PRM-REFERRED-ID-IN
,:PRM-STATUS-IN
,:PRM-ACCESS-FLAG-IN
,:PRM-CLOAKED-FLAG-IN
,:PRM-ISA-DATA-IN
,:PRM-QUALS-COUNT-IN
,:PRM-QUALIFIERS-IN
,:PRM-STATUS-OUT
,:PRM-RETURN-CODE-OUT
,:PRM-RETURNEE-COUNT-OUT
,:PRM-RETURNEE-DATA-OUT)
END-EXEC.
Programs Called By Stored Procedures

• A stored procedure may call another program which may not itself be a stored
procedure using a regular call statement (not EXEC SQL CALL). All programs
called by stored procedures must also reside in the stored procedure loadlib.
These called programs are compiled using the COBSP processing group, with
DBRM saved in the stored procedure DBRM library and linkedited to the stored
procedure loadlib.

Tri-Use Programs

• In some cases, a program may be called by a stored procedure, but also called
by a CICS or batch program. These are called tri-use programs. They are
compiled and bound 3 times: once for batch, once for CICS and once as a stored
procedure. In each case, the DBRM information is stored in a separate
DBRMLIB, eg. A1555.AIS.PROD.DBRMLIB for batch,
A1555.AIS.PROD.C ICSDBRM for CICS and A1555.AIS.PROD.SPDBRM for
stored procedure. The Endevor processing type for tri-use programs is COB3.

• Tri-use programs must also be bound 3 times pointing to the appropriate DBRM
library. For batch, the called program is bound into the plan for the calling
program(s). For CICS the called program is bound into a package with a CICS
collection ID, eg. PP2ACOLL. For stored procedure use, the called program is
bound into a package with a stored procedure collection ID, eg. SP2ACOLL.
Debugging Stored Procedures

• Cobol Displays can be added to stored procedures. Adding the following line to the
very beginning of a program will control the output DD of the displays:
PROCESS OUTDD(xxxxxxxx). It is recommended that xxxxxxxx be the name of the
program. Then the displays can be seen in the xxxxxxxx output DD of the
DB2xSPAS job.

• Abends: When a stored procedure abends, there may be information about the
abend in the SYSOUT output DD of the DB2xSPAS job. However, if a CICS
program calls a stored procedure and the EXEC SQL CALL statement gets an error
SQLCODE, that information will appear in the output of the CICS job.

• When SFIIDSVR abends, the EXEC SQL CALL statement from UCIID100 will
usually get a -430 SQLCODE. Frequently, after that, ANY call to SFIIDSVR will
result in a -471 SQLCODE which says that the stored procedure is stopped. This
means that a -START PROCEDURE command must be issued for the stored
procedure before it can be used again.
Triggers

• Triggers can be defined for DB2 tables to perform editing or maintenance


whenever a record is added, modified or deleted. This allows consistent edits or
maintenance to be performed regardless of how the data is updated.

• Triggers are defined using a "CREATE TRIGGER" DDL statement. A trigger can
contain statements to act on a table, or it can call a stored procedure to do the
operations.

• In the ID system, there are 3 triggers defined for the SF0INM table, one for
inserts, one for updates and one for deletes. Below is the DDL for defining the
insert trigger.

Figure 3:

CREATE TRIGGER SF0INMIN


AFTER INSERT ON SF0INM
REFERENCING NEW AS N
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
UPDATE SF0INM SET INM_FULL_NAME =
RTRIM(UPPER(INM_SURNAME)) || '#' ||
RTRIM(UPPER(INM_GIVEN_NAME)) || '#' ||
RTRIM(UPPER(INM_MIDDLE_NAME))
WHERE INM_INDIVIDUAL_ID = N.INM_INDIVIDUAL_ID
AND INM_SOURCE = N.INM_SOURCE ;
CALL SFHRSPA.SFINMTR (N.INM_INDIVIDUAL_ID) ;
END$

• All operations take place after the "BEGIN ATOMIC" statement


• The first action is to modify the INM_FULL_NAME field of the SF0INM table
with a concatenation of the 3 name components.
• Then the stored procedure SFINMTR is called to perform additional
operations.
Specific Stored Procedures in use in the ID System

• There are currently 2 stored procedures defined for the ID system:


o SFIIDSVR
o SFINMTR

• In addition, there are several programs which are called by SFIIDSVR.


o SFIDIDBU
o SFIDICFU (tri-use)
o SFIDIDQU
o SFIDINMU
o SFIDISAU
o SFIDIXBU
o SFIDVRED
o SFIIDCHK
o SFIIDMTC
o SFIIDNUM
o SFIIDUID
o SFTBLEDT (tri-use)

Of these, SFIDICFU and SFTBLEDT are tri-use, that is they can be called from
either a stored procedure or a regular CICS or batch program.

• SFINMTR is called from the triggers for the SF0INM table. Its purpose is to update
the IDB_MIN_INM_SOURCE in the SF0IDB table with the minimum value for the
source field in all SF0INM records for the ID.
o It is defined to DB2 in DDLLIB member SPINM00C.

• SFIIDSVR is a utility for updating ID system tables. It is currently called from CICS
programs and also from web applications.
o It is defined to DB2 in DDLLIB member SPIDS00C - see Figure 1 above.
o The input and output parameters are laid out in copylib member SFPRMIDS.
See Figure 4 below.
o Copylib member SFLNKIDS is used as a work area for formatting the
parameters.
o SFIIDSVR is called by UCIID100 and UCIID200 (see Figure 2 above) which
are CICS programs called from both the PPS and ID systems. These two
programs format the parameters and interpret the results for the programs
which called them.
Figure 4:

Copylib member SFPRMIDS

01 PRM-SYSTEM-IID -IN PIC X(50).


01 PRM-INDIVIDUAL-ID-IN PIC X(09).
01 PRM-REFERRED-ID-IN PIC X(09).
01 PRM-STATUS-IN PIC X(09).
01 PRM-ACCESS-FLAG-IN PIC X(01).
01 PRM-CLOAKED-FLAG-IN PIC X(01).
01 PRM-ISA-DATA-IN PIC X(220).
01 PRM-QUALS-COUNT-IN PIC S9(04) COMP.
01 PRM-QUALIFIERS-IN PIC X(1082).
01 FILLER REDEFINES PRM-QUALIFIERS-IN.
05 PRM-QUALIFIERS-IN-LEN PIC 9(4) BINARY.
05 PRM-QUALIFIERS-IN-TXT PIC X(1080).
01 PRM-STA TUS-OUT PIC S9(04) COMP.
01 PRM-RETURN-CODE-OUT PIC S9(04) COMP.
01 PRM-RETURNEE-COUNT-OUT PIC S9(04) COMP.
01 PRM-RETURNEE-DATA-OUT PIC X(30822).
01 FILLER REDEFINES PRM-RETURNEE-DATA-OUT.
05 PRM-RETURNEE-DATA-OUT-LEN PIC 9(4) BINARY.
05 PRM-RETURNEE-DATA-OUT-TXT.
10 PRM-RETURNEE-OUT OCCURS 20.
15 PRM-RETURNEE-FIELDS PIC X(79).
15 PRM-ISA-DATA PIC X(220).
15 PRM-RETURNEE-QUALS-COUNT-OUT PIC 9(02).
15 PRM-RETURNEE-QUALIFIERS-OUT PIC X(1240).
Figure 5 PPAPEUAB
SFIIDSVR CALLING PATH PPAPEUCA
PPWEEID
SFAPCLFH
SFAPIDFP
SFAPIDKS
SFBLDAUT
SFURFAB0
SFURFCA0
SFURFKS0
SFURFUP1
SFWHSDS
SFWHSUP
SFWIASN
SFWIDBR
SFWIDIS
SFWRUID
UCAPIDFE
UCIIDASN
UCIIDMNT UCAPIDFE
UCWIDAS UCAPIDKS
UCWIDBS UCIIDASN
UCWIDDS UCIIDMNT
UCWIDUP

UCIID100 UCIID200

SFIIDSVR

SFIIDUID SFIIDMTC SFIIDNUM SFIIDCHK SFIDIDBU SFIDINMU SFIDIDQU SFIDIXBU SFIDISAU SFIDVRED

You might also like