Introduction to creating an Oracle Procedure
This module discusses creating procedures using PL/SQL.
Procedures are more versatile than functions. While a function returns a single value, a procedure can return no value, or multiple values using outgoing parameters. A function has severe restrictions against modifying database data, while a procedure is allowed to modify data. During this lesson, you will create a procedure and then add a call to a function within that procedure.
By the end of this module, you will know how to:
- Distinguish between a function and a procedure
- Create a procedure to calculate a percentage and an average for a given sales transaction
- List capabilities and restrictions of calling other procedures
- Add a function to a procedure
You also will use a simulation to build your own procedure for the House-O-Pets course project.
The next lesson describes a procedure and how it differs from a function.
Why Procedures are needed?
One of the biggest challenges in PL/SQL programming is finding ways to reduce the complexity of our environment. When faced with a massive problem to solve, the mind is likely to recoil in horror. Where do I start?
How can I possibly find a way through that jungle of requirements and features?
A human being is not a massively parallel computer. Even the most talented programmers have trouble keeping track of more than seven tasks (plus or minus two) at one time. We need to break down huge, intimidating projects into smaller, more manageable components, and then further decompose those components into individual programs with an understandable scope.
We can then figure out how to build and test those programs, after which we can construct a complete application from these building blocks.
Whether you use top-down design or some other methodology, there is absolutely no doubt that you will find your way to a high-quality and easily maintainable application by modularizing your code into procedures, functions, and object types.
A procedure is a subprogram that can take parameters and be called. Generally, you use a procedure to perform an action. A procedure has two parts:
- the specification and
- the body.
The specification (spec for short) begins with the keyword PROCEDURE and ends with the procedure name or a parameter list.
Parameter declarations are optional. Procedures that take no parameters are written without parentheses. The procedure body begins with the keyword IS (or AS) and ends with the keyword END followed by an optional procedure name.
The procedure body has three parts:
- an optional declarative part,
- an executable part,
- and an optional exception-handling part.
The declarative part contains declarations of types, cursors, constants, variables, exceptions, and subprograms. These items are local and cease to exist when you exit the procedure.
The executable part contains statements that assign values, control execution, and manipulate Oracle data. The exception-handling part contains handlers that deal with exceptions raised during execution.
Note that the procedure declaration
in a PL/SQL block or package is not the same as creating a procedure in SQL.