Working with stored procedures

6.2 Working with stored procedures

A stored procedure is a database application object that can encapsulate SQL statements and business logic. It helps improve performance by reducing network traffic. Figure 6.2 illustrates how stored procedures work.

Figure 6.2 – Network traffic reduction with stored procedures

At the top left corner of the figure, you see several SQL statements executed one after the other. Each SQL is sent from the client to the data server, and the data server returns the result back to the client. If many SQL statements are executed like this, network traffic increases. On the other hand, at the bottom, you see an alternate method that incurs less network traffic. This second method calls a stored procedure myproc stored on the server, which contains the same SQL; and then at the client (on the left side), the CALL statement

Chapter 6 – Stored procedures and functions 147 is used to call the stored procedure. This second method is more efficient, as there is only

one call statement that goes through the network, and one result set returned to the client. Stored procedures can also be helpful for security purposes in your database. For

example, you can let users access tables or views only through stored procedures; this helps lock down the server and keep users from accessing information they are not supposed to access. This is possible because users do not require explicit privileges on the tables or views they access through stored procedures; they just need to be granted sufficient privilege to invoke the stored procedures.

6.2.1 Types of procedures

There are primarily two types of stored procedures: SQL procedures and external procedures. SQL procedures are written in SQL; external procedures are written in a host language. However, you should also consider several other important differences in behavior and preparation.

SQL procedures and external procedures consist of a procedure definition and the code for the procedure program. Both an SQL procedure definition and an external procedure definition require the following information:

 The procedure name.  Input and output parameter attributes.  The language in which the procedure is written. For an SQL procedure, the

language is SQL.  Information that will be used when the procedure is called, such as runtime options,

length of time that the procedure can run, and whether the procedure returns result sets.

The following example shows a definition for an SQL procedure. CREATE PROCEDURE UPDATESALARY (1)

(IN EMPNUMBR CHAR(10), (2) IN RATE DECIMAL(6,2)) LANGUAGE SQL (3) UPDATE EMP (4) SET SALARY = SALARY * RATE WHERE EMPNO = EMPNUMBR

In the example:

1. The stored procedure name is UPDATESALARY.

2. There are two parameters, EMPNUMBR with data type CHAR(10), and RATE with data type DECIMAL(6,2). Both are input parameters.

3. LANGUAGE SQL indicates that this is an SQL procedure, so a procedure body follows the other parameters.

Database Fundamentals 148

4. The procedure body consists of a single SQL UPDATE statement, which updates rows in the employee table.

The following example shows a definition for an equivalent external stored procedure that is written in COBOL. The stored procedure program, which updates employee salaries is called UPDSAL.

CREATE PROCEDURE UPDATESALARY (1) (IN EMPNUMBR CHAR(10), (2) IN RATE DECIMAL(6,2)) LANGUAGE COBOL (3) EXTERNAL NAME UPDSAL; (4)

In the example:

1. The stored procedure name is UPDATESALARY.

2. There are two parameters, EMPNUMBR with data type CHAR(10), and RATE with data type DECIMAL(6,2). Both are input parameters.

3. LANGUAGE COBOL indicates that this is an external procedure, so the code for the stored procedure is in a separate COBOL program.

4. The name of the load module that contains the executable stored procedure program is UPDSAL.

6.2.2 Creating a stored procedure

To create a Java, PL/SQL or SQL PL stored procedure in Data Studio, follow the steps below. Note that stored procedures in other languages cannot be created from Data Studio. In the following steps, we choose SQL (representing SQL PL) as the language for the stored procedure, however similar steps apply to Java and PL/SQL languages.

Step 1: Write or generate the stored procedure code

When you want to create a stored procedure, right-click on the Stored Procedures folder and choose New -> Stored Procedure. Complete the information requested in the New Stored Procedure wizard such as the project to associate the procedure with, the name and language of the procedure, and the SQL statements to use in the procedure. By default, Data Studio gives you an example SQL statement. Take all the defaults for all the other panels, or at this point, you can click Finish and a stored procedure is created using some template code and the SQL statement provided before as an example. This is shown in Figure 6.3.

Chapter 6 – Stored procedures and functions 149

Figure 6.3 – A sample stored procedure

In Figure 6.3, the code for the sample stored procedure MYPROCEDURE was generated. You can replace all of this code with your own code. For simplicity, we will continue in this chapter using the above sample stored procedure as if we had written it.

Step 2: Deploy a stored procedure

Once the stored procedure is created, to deploy it, select it from the Data Project Explorer view, right-click on it, and then choose Deploy. Deploying a stored procedure is essentially executing the CREATE PROCEDURE statement, compiling the procedure and storing it in the database. Figure 6.4 illustrates this step.

Database Fundamentals 150

Figure 6.4 – Deploying a stored procedure

After clicking Deploy, in the Deploy options panel, taking the defaults and clicking on Finish is normally good enough.

Step 4: Run a stored procedure

Once the stored procedure has been deployed, you can run it by right-clicking on it and choosing Run. The results would appear in the Results tab at the bottom right corner of the Data Studio workbench window as shown in Figure 6.5.

Chapter 6 – Stored procedures and functions 151

Figure 6.5 – Output after running a stored procedure

To run a stored procedure from the DB2 Command Window or the Command Editor, you can use the CALL <procedure name> statement. Remember you first need to connect

to the database since this is where the stored procedure resides. Figure 6.6 illustrates this.

Database Fundamentals 152

Figure 6.6 – Calling a stored procedure from the DB2 Command Window

Just like you can call a stored procedure from the DB2 Command Window, you can also do so from a Java program, a C program, a Visual Basic program, and so on. You just need to use the correct syntax for the given language.

6.2.3 Altering and dropping a stored procedure

There are two ways to alter an existing stored procedure:

1. Drop the existing procedure and recreate the procedure again with a new definition.

2. Use ‘CREATE OR REPLACE PROCEDURE syntax instead of ‘CREATE PROCEDURE'.

There is also an ALTER PROCEDURE statement, but it can only be used to alter specific properties of the procedure rather than the code itself.

To drop a procedure, use the fully qualified name of the procedure with the ‘DROP PROCEDURE’ command as shown in example below.

drop procedure myschema.EMPLOYEE_COUNT To alter a procedure it is preferred to use the CREATE OR REPLACE PROCEDURE syntax

in the first place rather than dropping and recreating the procedure. This is because dropping a procedure may have other consequences like invalidating objects depending on the procedure. With the CREATE OR REPLACE PROCEDURE syntax this invalidation does not occur.

Chapter 6 – Stored procedures and functions 153