7.14. Stored Procedures
Stored procedures are programs whose executable binaries reside at the database server. They serve as subroutines to calling applications, and they normally wrap multiple SQL statements with flow logic. Figure 7.27 depicts a situation in which stored procedures are useful.
Figure 7.27. Reducing network traffic by using stored procedures

In this example, the procedure name is CSMMGR.NEW_SALARY . This procedure takes an input parameter p_empno and an output parameter p_empName . The procedure will increase by 5% the value in the salary column of table employee for the employee with employee number p_empno . It will then return the name of the employee who received the increase in the format lastname, firstname . Figure 7.28 shows the Development Center tool used to develop the procedure. At the bottom of the figure, you can see the result of its execution.
CREATE PROCEDURE CSMMGR.NEW_SALARY (IN p_empno CHAR(6),
OUT p_empName VARCHAR(30) )
LANGUAGE SQL
---------------------------------------------------------------------
-- SQL Stored Procedure used to update the salary of an employee
---------------------------------------------------------------------
P1: BEGIN
DECLARE v_firstName VARCHAR(12);
DECLARE v_lastName VARCHAR(15);
UPDATE employee SET salary = salary * 1.05
WHERE empno = p_empno;
SELECT lastname, firstnme INTO v_lastName, v_firstName
FROM employee WHERE empno = p_empno;
SET p_empName = v_lastName || ', ' || v_firstName;
END P1
Figure 7.28. Using the Development Center tool to develop, test, and run the
CSMMGR.NEW_SALARY stored procedureChapter 5, Understanding the DB2 Environment, DB2 Instances, and Databases, for details.An unfenced stored procedure runs in the same address space as the DB2 engine. In terms of performance, unfenced stored procedures run faster than fenced ones; however, there is a risk that unfenced procedures may corrupt DB2 information, so you should make sure to test these procedures thoroughly.NOTESQL PL stored procedures can run only unfenced.