Vous êtes sur la page 1sur 5

The Anatomy of a Stored Procedure

Creating Stored Procedures

This document is going to attempt to outline the steps to create a stored procedure in Oracle.

Introducing the Named Block or Stored Procedure

Problem to resolve:

You are to create a stored procedure called FOR_SALE that is to retrieve and display properties for sale in a particular
city. This variable in your code is called v_city_in. You will use this to provide the city name when the procedure is
called. The procedure is to list the ProperteyID, AskingPrice, Address, and City.

This example is based on the code for tables called Properties, Listings, and CustAgentList. The code is available to you
in order to try the examples in these notes.

Since there can be more than one listing in a city an explicit cursor is required.

A stored procedure is PL/SQL block or program with a name. They contain a header section. This tells Oracle the name
of the block and whether it is a function or a procedure. A procedure is a named PL/SQL block that is stored on the
Oracle server and performs some action. The action of executing a procedure is termed calling the procedure. You call
the procedure by calling its name.

Header

This defines that you are creating a procedure; optionally you can also specify the OR REPLACE keywords to update and
replace the procedure if it already existed. The procedure is also given a name, in this case FOR_SALE is the name
assigned.

An open parenthesis is used and any parameters that are needed in the procedure are then listed. This is an IN
parameter which means a value is provided when the procedure is called. There is only on in this so a closing
parenthesis is used to indicate the end of the parameter list.

Following this is the keyword AS or you could use the IS keyword it works exactly the same; it is just a matter of choice.

Following this any local variables are listed. This includes CURSORS local variable declarations or even scalar variables.
Their form is identical to those used in the declaration section of the anonymous PL/SQL block.

1
Pieces to be declared

Our code requires an explicit cursor since more than one listing can exist for the city specified when the procedure is
called.

Following the AS keyword the CURSOR is given. The cursor name C_PROPERTIES is just a name for the SELECT statement
given in the code. So if you refer to C_PROPERTIES you are actually referring to the SELECT statement that follows the
cursor name.

BEGIN

Following the BEGIN keyword are the steps needed to process what is required by the procedure. The procedure is to
list the property ID, asking price, address, and city. These are the values being retrieved from our tables through the
SELECT statement defined in the CURSOR.

First of all a set of headings are been shown to tell the user what is being displayed. This is being displayed using a
DBMS_OUTPUT.PUT_LINE built-in package and procedure.

Since we have used an explicit cursor this implies there is more than one row to be shown in the output. This tells us a
LOOP is going to be needed. Since the CUSOR FOOR LOOP requires less coding I have elected to use this structure.

The CURSOR FOR LOOP opens the cursor for processing, and it also closes the CURSOR once all the records are retrieved
from the cursor. You do not need to specify the loop termination it will end when all the records have been processed.

2
The TO_CHAR functions are being used to format the output , since both are numbers this is the proper way to output
them since the DBMS_OUTPUT is for outputting character data these convert them numbers to character data and also
apply formatting symbols to the output values. For every LOOP there has to be an END LOOP.

EXCEPTION Handler

This is not required but is is a moe elegant way to handle poossible errors in the procedure. If an error occurs program
control is sent toe h EXCEPTION block to be handled. The generic SQLERRM function is being used. This will output the
error message that is returned by the procedure and display it to the user.

3
The finished stored procedure

Notice the SET SERVEROUTPUT ON statement is also part of the stored procedure. This is needed so the output
produced by the procedure can be seen when it is called.

4
Test the procedure

Regardless of the case that the city is entered in, the program handles the case entered in any fashion.

Vous aimerez peut-être aussi