Collection Contents Index Benefits of procedures and triggers Introduction to user-defined functions pdf/chap10.pdf

User's Guide
   PART 1. Working with Databases
     CHAPTER 10. Using Procedures, Triggers, and Batches       

Introduction to procedures


In order to use procedures, you need to understand how to do the following:

This section discusses each of these aspects of using procedures, and also describes some of the different uses of procedures.

Top of page  Creating procedures

Procedures are created using the CREATE PROCEDURE statement. You must have RESOURCE authority in order to create a procedure.

Where you enter the statement depends on the tool you are using:

The following simple example creates a procedure that carries out an INSERT into the department table of the sample database, creating a new department.

CREATE PROCEDURE new_dept ( IN id INT,
IN name CHAR(35),
IN head_id INT )
BEGIN
INSERT 
INTO dba.department ( dept_id,
dept_name,
dept_head_id )
VALUES ( id, name, head_id );
END

For Info     For a complete description of the CREATE PROCEDURE syntax, see CREATE PROCEDURE statement.

The body of a procedure is a compound statement. The compound statement starts with a BEGIN statement and concludes with an END statement. In the case of new_dept, the compound statement is a single INSERT bracketed by BEGIN and END statements.

For Info     For more information, see Using compound statements.

Parameters to procedures are marked as one of IN, OUT, or INOUT. All parameters to the new_dept procedure are IN parameters, as they are not changed by the procedure.

Top of page  Calling procedures

A procedure is invoked with a CALL statement. Procedures can be called by an application program, or they can be called by other procedures and triggers.

For Info     For more information, see CALL statement.

The following statement calls the new_dept procedure to insert an Eastern Sales department:

CALL new_dept( 210, 'Eastern Sales', 902 );

After this call, you may wish to check the department table to see that the new department has been added.

The new_dept procedure can be called by all users who have been granted EXECUTE permission for the procedure, even if they have no permissions on the department table.

Top of page  Dropping procedures

Once a procedure is created, it remains in the database until it is explicitly removed. Only the owner of the procedure or a user with DBA authority can drop the procedure from the database.

The following statement removes the procedure new_dept from the database:

DROP PROCEDURE new_dept

Top of page  Permissions to execute procedures

A procedure is owned by the user who created it, and that user can execute it without permission. Permission to execute it can be granted to other users using the GRANT EXECUTE command.

For example, the owner of the procedure new_dept could allow another_user to execute new_dept with the statement:

GRANT EXECUTE ON new_dept TO another_user

The following statement revokes permission to execute the procedure:

REVOKE EXECUTE ON new_dept FROM another_user

For Info     For more information on managing user permissions on procedures, see Granting permissions on procedures.

Top of page  Returning procedure results in parameters

Procedures can return results to the calling environment in one of the following ways:

This section describes how to return results from procedures as parameters.

The following procedure on the sample database returns the average salary of employees as an OUT parameter.

CREATE PROCEDURE AverageSalary( OUT avgsal
NUMERIC (20,3) )
BEGIN
SELECT AVG( salary ) 
INTO avgsal 
FROM employee;
END

To run this procedure and display its output from Interactive SQL, carry out the following steps:

  1. Connect to the sample database from Interactive SQL as user ID DBA using password SQL.

  2. Create the procedure.

  3. Create a variable to hold the procedure output. In this case, the output variable is numeric, with three decimal places, so create a variable as follows:

    CREATE VARIABLE Average NUMERIC(20,3)
  4. Call the procedure, using the created variable to hold the result:

    CALL AverageSalary(Average) 

    The Interactive SQL statistics window displays the message "Procedure completed" if the procedure was created and run properly.

  5. Look at the value of the output variable Average. The Interactive SQL Data window displays the value 49988.623 for this variable; the average employee salary.

Top of page  Returning procedure results in result sets

In addition to returning results to the calling environment in individual parameters, procedures can return information in result sets. A result set is typically the result of a query. The following procedure returns a result set containing the salary for each employee in a given department:

CREATE PROCEDURE SalaryList ( IN department_id INT)
RESULT ( "Employee ID" INT, "Salary" NUMERIC(20,3) )
BEGIN
   SELECT emp_id, salary
   FROM employee
   WHERE employee.dept_id = department_id;
END

If this procedure is called from Interactive SQL, the names in the RESULT clause are matched to the results of the query and used as column headings in the displayed results.

To test this procedure from Interactive SQL, you can CALL it, specifying one of the departments of the company. The results are displayed in the Interactive SQL Data window. For example:

  To list the salaries of employees in the R & D department (department ID 100):
  1. Type the following:

CALL SalaryList (100)

Employee ID

Salary

102

45700.000

105

62000.000

160

57490.000

243

72995.000

247

48023.690

To execute a CALL of a procedure that returns a result set, Interactive SQL opens a cursor.

The cursor is left open after the CALL in case a second result set is returned. The Interactive SQL statistics window displays the plan of the SELECT query in the procedure and then displays the line:

Procedure is executing. Use RESUME to continue.

You need to execute the RESUME statement or the Interactive SQL CLEAR command from the Interactive SQL Command window before you can alter or drop the procedure.

For Info     For more information about using cursors in procedures, see Using cursors in procedures and triggers.

Top of page  

Collection Contents Index Benefits of procedures and triggers Introduction to user-defined functions pdf/chap10.pdf