Anda di halaman 1dari 48

HTTP 5105 Database Design and

Development
How to Create Stored Procedures and Functions

HTTP 5105 Database Design and Development 1 November 20, 2104


Objectives

Applied
Create stored procedures and user-defined functions
using any of the features presented in this chapter.
Code CALL statements and scripts that call stored
procedures.
Code SQL statements that use user-defined functions.

HTTP 5105 Database Design and Development 2 November 20, 2104


Objectives

Knowledge
Explain why a stored procedure executes faster than
an equivalent SQL script.
Describe the difference between passing parameters
by position and passing them by name.
Describe the basic process for validating data within a
stored procedure or function.
Describe the benefits of using packages for stored
procedures and functions.
HTTP 5105 Database Design and Development 3 November 20, 2104
How to Create and Call a Stored
Procedure

To start a stored procedure you code the CREATE keyword or with


the optional OR REPLACE followed by the PROCEDURE keyword
Our first example will show a stored procedure called
update_invoices_credit_total
This procedure will update the credit total column of the invoices
table
After the procedure name you code one or more parameters for the
procedure, these appear inside a set of parentheses
A parameter is typically used to pass a value to the stored
procedure from a calling program
A parameter can also be used to pass a value back from the stored
procedure to the calling program

HTTP 5105 Database Design and Development 4 November 20, 2104


How to Create and Call a Stored
Procedure

If a procedure accepts more than one parameter, you


use a comma to separate each parameter
When you declare a parameter you give it a name
followed by its data type
After the parentheses you code either the keyword IS
or AS
Followed by the PL/SQL code to be executed

HTTP 5105 Database Design and Development 5 November 20, 2104


The CREATE PROCEDURE Statement

CREATE [OR REPLACE] PROCEDURE procedure_name


[(
parameter_name_1 data_type
[, parameter_name_2 data_type]...
)]
{IS | AS}
pl_sql_block

HTTP 5105 Database Design and Development 6 November 20, 2104


How to Create and Call a Stored
Procedure

When you run the CREATE PROCEDURE statement


Oracle compiles the PL/SQL code for the procedure
and stores the compiled code in the database
Oracles compiler checks the syntax of the code
within the procedure
If you made a coding error the system responds with
an appropriate error and the procedure is not created

HTTP 5105 Database Design and Development 7 November 20, 2104


Stored Procedure
You will notice the
output just says
compiled there is no
output given
You now have a
compiled procedure
that is stored in the
database
In your tree on the
left side of SQL
Developer is an entry
for Procedures,
expand and there is
the procedure stored

HTTP 5105 Database Design and Development 8 November 20, 2104


CALL a Stored Procedure

You can execute or call a stored procedure by using the


CALL statement
The CALL statement calls the procedure and passes one
value for each of the parameters defined by the procedure
In our example there were two parameters, one was a
VARCHAR2 and the second was a NUMBER
When you CALL the procedure the most common way to
pass the parameters is by position
With this method the parameter values used in the CALL
must be in the same order as they are listed in the
procedure

HTTP 5105 Database Design and Development 9 November 20, 2104


CALL a Stored Procedure

CALL keyword is used followed by the name of the stored procedure and the two
parameter values in this case

HTTP 5105 Database Design and Development 10 November 20, 2104


Using an Anonymous PL/SQL
Program to Call a Stored Procedure

A stored procedure can also be called using an Anonymous PL/SQL program


HTTP 5105 Database Design and Development 11 November 20, 2104
Pass Parameters by Name

AN alternate method to pass parameters to a stored


procedure is to pass by name
To do this the names of the parameters that are
defined in the CREATE PROCEDURE statement are
included followed by the => association operator
followed by the value being passed
When you use this method you can list the
parameters in any order

HTTP 5105 Database Design and Development 12 November 20, 2104


Pass Parameters by Name

An anonymous PL/SQL block is


used again but the parameter
names are given followed by their
value
The order does not matter this
way

HTTP 5105 Database Design and Development 13 November 20, 2104


How to Code Input and Output
Parameters

Stored procedures provide three different types of


parameters:
Input parameters IN
Output parameters OUT
Input/output parameters IN/OUT
In our first example the IN keyword was not used for
either of the two parameters declared
If the keyword is omitted it is assumed to be an input
parameter
HTTP 5105 Database Design and Development 14 November 20, 2104
How to Code Input and Output
Parameters

Within a procedure you can use input parameters like


variables
However, you cannot change the value of the
parameter
In our next example the procedure uses the first
parameter within an UPDATE statement to specify
the invoice number for the invoice row to be updated
Then it uses it in a SELECT statement to get the
number of rows updated

HTTP 5105 Database Design and Development 15 November 20, 2104


How to Code Input and Output
Parameters

An output parameter is returned to the calling program


from the stored procedure
To code an output parameter you must explicitly identify
the parameter by coding the keyword OUT after the name
of the parameter
If the UPDATE executes successfully, the SELECT
statement stores the count of the rows that were updated
in this parameter
Since the invoice number should be unique this should
store the value of 1 in the output parameter

HTTP 5105 Database Design and Development 16 November 20, 2104


How to Code Input and Output
Parameters

However if the UPDATE does not execute successfully


the SELECT statement in the EXCEPTION sections
stores a 0 (zero) in the output parameter
The value of the output parameter is returned to the
calling program when the procedure finishes

HTTP 5105 Database Design and Development 17 November 20, 2104


How to Code Input and Output
Parameters

HTTP 5105 Database Design and Development 18 November 20, 2104


A Script that Calls the Stored Procedure and
Uses the Output Parameter

HTTP 5105 Database Design and Development 19 November 20, 2104


How to Code Optional Parameters

When you call a procedure that has optional parameters,


you can omit any of its optional parameters
In the next example the second parameter is an optional
parameter
As a result when you call this procedure you can omit the
second parameter
In that case the credit total for the specified invoice is set
to the value of the second parameter
The optional parameters are generally coded at the end of
the parameter list
HTTP 5105 Database Design and Development 20 November 20, 2104
How to Code Optional Parameters

You use the DEFAULT keyword to specify a default


value for each of the parameters
In our example the DEFAULT specifies a default value
of 100 for the second parameter
The first CALL statement will supply both parameters,
so the credit total will be set for invoice 200
The second CALL omits the second parameter for the
invoice so it is set to the default value of 100

HTTP 5105 Database Design and Development 21 November 20, 2104


Syntax for Declaring an Optional
Parameter

parameter_name_1 data_type [DEFAULT


default_value]

HTTP 5105 Database Design and Development 22 November 20, 2104


A Statement That Uses an Optional
Parameter
Procedure is compiled
Notice the optional
parameter declared with
the DEFAULT keyword

HTTP 5105 Database Design and Development 23 November 20, 2104


A Statement That Calls the Stored
Procedure
The procedure is called with
two parameters
The values are verified with
a SELECT statement

HTTP 5105 Database Design and Development 24 November 20, 2104


A Statement That Calls the Stored
Procedure
The procedure was run for a
second time, the
credit_total was omitted
The SELECT shows the
DEFAULT value of 100 was
inserted

HTTP 5105 Database Design and Development 25 November 20, 2104


How to Raise Errors

Within a stored procedure it is generally considered a


good practice to prevent errors by checking the
parameters before they are used to make sure they
are valid
This is often referred to as data validation
Then if the data is not valid you can execute code that
makes it valid or you can raise an error which returns
an error to the calling program

HTTP 5105 Database Design and Development 26 November 20, 2104


How to Raise Errors

In the next example we will see how to raise an error


by raising one of the predefined functions that are
available in Oracle
To do this you code the RAISE statement followed by
the name of the predefined exception
In this example the IF statement checks whether the
value of the second parameter is less than zero
If so it raises the predefined error called
VALUE_ERROR exception

HTTP 5105 Database Design and Development 27 November 20, 2104


How to Raise Errors

If the calling program catches this exception it can include


code that handles the exception
In this example the calling program includes an EXCEPTION
section that handles this exception by printing the error
message to the console that says, A VALUE ERROR
occurred
If necessary this message could be improved to be more
user-friendly
In addition the EXCEPTION section handles all other
exceptions by printing a message the says An unexpected
error occurred

HTTP 5105 Database Design and Development 28 November 20, 2104


Code is Rewritten
Notice the IF statement
checking the value of the
credit_value_param to make
sure it is not less than zero
If it is less than zero we RAISE
an error, for VALUE_ERROR

HTTP 5105 Database Design and Development 29 November 20, 2104


Call the Procedure as Before

The calling program is not catching he exception thus


the error message

HTTP 5105 Database Design and Development 30 November 20, 2104


Call the Procedure with Anonymous
Block

The error message is printed form the anonymous


block, the error is now being handled

HTTP 5105 Database Design and Development 31 November 20, 2104


Error Handling with
RAISE_APPLCATION_ERROR
Code was rewritten to use the
RAISE_APPLICATION_ERROR
handler

HTTP 5105 Database Design and Development 32 November 20, 2104


CALL Procedure with the
RAISE_APPLICATION_ERROR

HTTP 5105 Database Design and Development 33 November 20, 2104


RAISE_APPLICATION_ERROR

The RASIE_APPLICATION_ERROR procedure raises an


application error with the specified error number and
message
When you use this statement the error number
parameter must be between -20000 and -20999
When the stored procedure is called form an
anonymous procedure again, the out put is now
caught by the WHEN OTHERS clause

HTTP 5105 Database Design and Development 34 November 20, 2104


RAISE_APPLICATION_ERROR

The code for the WHEN OTHERS is now output when the
RAISE_APPLICTION_ERROR handler is used

HTTP 5105 Database Design and Development 35 November 20, 2104


Stored Procedure that Inserts a Row

Parameters that are declared for the stored procedure

HTTP 5105 Database Design and Development 36 November 20, 2104


Stored Procedure that Inserts a Row

Next part of the program code

HTTP 5105 Database Design and Development 37 November 20, 2104


Stored Procedure that Inserts a Row

Last part of procedure to insert a row


HTTP 5105 Database Design and Development 38 November 20, 2104
Stored Procedure that Inserts a Row

The procedure has nine parameters that correspond the 9


columns in the invoices table
Each parameter is assigned the same data type as the
matching column in the invoices table
As a result if the calling program passes a value that cannot
be converted to the proper data type an error will be
raised when the procedure is called
None of these parameters corresponds with the invoice_id
column
The body of the procedure uses a sequence to provide a
value for t his column, which is what you want

HTTP 5105 Database Design and Development 39 November 20, 2104


Stored Procedure that Inserts a Row

Three statements that used the stored


procedure to insert the three rows
HTTP 5105 Database Design and Development
successfully
40 November 20, 2104
Stored Procedure that Inserts a Row

A validation error occurs so the program raises an error


HTTP 5105 Database Design and Development 41 November 20, 2104
Stored Procedure that Inserts a Row

Since our program does not contain a COMMIT


statement the application program that calls this
procedure must commit the change to the database
Ideally an application program would commit the
invoice data to the database after the line items for
the invoice have also been inserted
We only inserted data for the invoice header

HTTP 5105 Database Design and Development 42 November 20, 2104


The Syntax for Creating a Function

CREATE [OR REPLACE] FUNCTION function_name


[(
parameter_name_1 data_type
[, parameter_name_2 data_type]...
)]
RETURN data_type
{IS | AS}
pl_sql_block

HTTP 5105 Database Design and Development 43 November 20, 2104


How to Code User-Defined Functions

The functions here are referred to as user-defined


functions (UDFs) or stored functions or just functions
There are many similarities between functions and
stored procedures
There are two differences:
A function must always return a value
A function cannot make changes to the database such
as executing an INSERT, UDATE, or DELETE statement

HTTP 5105 Database Design and Development 44 November 20, 2104


How to Code User-Defined Functions

A scalar function is a function that always returns a


single value
You start by coding the CREATE FUNCTION statement
followed by the name of the function
You can add the optional OR REPLACE keywords
After the name of the function you code a set of
parameters for the function, these are contained in a
set of parentheses

HTTP 5105 Database Design and Development 45 November 20, 2104


How to Code User-Defined Functions

After the parentheses you code the RETURN keyword


followed by the data type that is returned by the
function
After the declaration of the RETURN you code the IS
or AS keyword to signal you are about begin coding
the PL/SQL code for the function
After the BEGIN keyword this function uses a SELECT
statement to get the vendor_id value

HTTP 5105 Database Design and Development 46 November 20, 2104


Create a User-Defined Function

HTTP 5105 Database Design and Development 47 November 20, 2104


A SELECT Statement That Uses a
Function

HTTP 5105 Database Design and Development 48 November 20, 2104

Anda mungkin juga menyukai