Tải bản đầy đủ (.pdf) (34 trang)

Tài liệu modularizing programming with subprograms pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (246.57 KB, 34 trang )

Modularizing Programming with
Subprograms
20
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder20Ć2
Schedule: Timing Topic
60 minutes Lecture
20 minutes Practice
80 minutes Total
Class Management Note:
Files required for lesson are:
DEMO: l20proc.pls, l20func.pls
PRACTICE: p20proc.pls
Modularizing Programming with Subprograms 20Ć3
Objectives
Modularity allows you to break your code into manageable, well-defined logical
units. Each of these units in PL/SQL is called a program unit, or a subprogram.
PL/SQL has two types of subprograms called procedures and functions. This
lesson covers the structure of subprograms and how to invoke them.
At the end of this lesson, you should be able to
D
Determine the types of program units and where to use them.
D
Explain the differences between and benefits of procedures and functions.
D
Develop subprograms.
D
Invoke subprograms from Procedure Builder.
Class Management Note:
This lesson is designed to give students an overview of a subprogram
because Procedure Builder can only store subprograms, not anonymous
blocks. Therefore, use this lesson as a high level overview, with the details


to follow in the next lessons.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder20Ć4
Modularizing Programming with Subprograms 20Ć5
Overview
Program units are named PL/SQL blocks. They fall into three main categories:
D
Procedures to perform actions
D
Functions to compute a value
D
Packages to bundle logically related procedures and functions
Stored or Application Subprogram?
These program units can be created in a variety of environments, including
server-side stored subprograms or as application subprograms.
Concept
Stored Subprogram Application Subprogram
Location Is in the database. Is within the application.
Executed From any database tool or
application.
From only the application in
which it was created.
Availability By way of database security. Independently of, and in
addition to, stored subprograms.
Subprograms are composed of a number of sections:
D
A header to name and type the block
D
An optional declarative section to set up local identifiers
D
An executable part to perform the actions

D
An optional exception handling section to handle exceptions
For more information, see
PL/SQL User’s Guide and Reference, Release 2.3, “Subprograms.”
Class Management Note:
Packages and other PL/SQL constructs are covered in the Develop
Database Procedures course.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder20Ć6
Modularizing Programming with Subprograms 20Ć7
Creating a Subprogram
The following steps will assist you to create a subprogram.
1.
Select your environment.
If using Procedure Builder, then select either the Program Units node or the
Database Objects node and Stored Procedures Units subobject node.
2.
Write your syntax.
If using Procedure Builder, enter the syntax in the Program Unit Editor. If using
SQL*Plus, write your code in a text editor as a script file.
3.
Compile your code.
The source code is complied into p-code. If using Procedure Builder, click the
Compile button. If using SQL*Plus, start your file at the SQL prompt.
4.
Invoke the successfully compiled procedure or function.
Guidelines
D
SQL*Plus is another development environment for writing and for initial testing
of a procedure, although you will need to test the procedure by invoking it from
an application.

D
The SQL commands issued to create a stored subprogram are CREATE
PROCEDURE or CREATE FUNCTION.
D
The SQL commands issued to remove a stored subprogram are DROP
PROCEDURE or DROP FUNCTION.
D
Use CREATE OR REPLACE PROCEDURE or CREATE OR REPLACE
FUNCTION so you do not have to issue a DROP statement.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder20Ć8
Modularizing Programming with Subprograms 20Ć9
Creating a Procedure
Create a PL/SQL procedure to store a series of actions for later execution. The
procedure can contain zero or more parameters, which are arguments that you list
when calling the procedure.
Syntax
PROCEDURE name
[(parameter,...)]
IS
pl/sql_block;
where: name is the procedure name, which adheres to the
standard Oracle naming rules.
parameter is the parameter syntax shown below.
pl/sql_block is the procedural body that defines the action
performed by the procedure.
The parameter syntax is as follows.
Syntax
parameter_name [IN | OUT | IN OUT] datatype
[{:= | DEFAULT} expr]
where: parameter_name is the name of the parameter.

datatype is the datatype of the parameter, without
constraints.
expr is the value to initialize the parameter.
Guidelines
D
Start the PL/SQL block with the keyword IS.
D
Enter any local declarations between IS and BEGIN.
When creating the procedure from Procedure Builder, the CREATE OR REPLACE
portion of the syntax is implied. Therefore, when creating the procedure from
SQL*Plus, begin the statement with CREATE OR REPLACE.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder20Ć10
Modularizing Programming with Subprograms 20Ć11
Creating a Procedure
continued
Procedural Parameters
Transfer values to and from the calling environment through parameters. There are
two types that you use. When declaring a procedure, the formal parameter is used to
define the values used in the executable portion of the PL/SQL block. The actual
parameter, or argument, is referenced when invoking a subprogram.
Parameter Modes for Formal Parameters
Parameter Mode
Description
IN Default argument.
Passes a value from the calling environment into the
subprogram.
Formal parameter acts as a constant—you cannot
overwrite the value.
Actual parameter can be an expression, a constant, a
literal, or an initialized variable.

OUT Must be specified.
Returns a value from the procedure to the calling
environment.
Formal parameter acts as an uninitialized variable.
Formal parameter cannot be assigned to another
variable or to itself.
Actual parameter must be a variable; it cannot be a
constant or expression.
IN OUT Must be specified.
Passes a value from the calling environment into the
procedure, and returns a possibly different value from
the procedure to the calling environment.
Formal parameter acts as an initialized variable.
Formal parameter can be used as a normal variable; it
can be assigned to a value.
Actual parameter must be a variable.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder20Ć12
Class Management Note:
DEMO: l20proc.pls
PURPOSE: Demonstrate how to use the Procedure Builder environment,
including loading and compiling code from a text file.
1.Make sure you are connected to the database.
2.Select from the menu File Load and load the file.
3.Show students the code and Interpreter line command.
You will invoke this procedure later in the lesson.
Modularizing Programming with Subprograms 20Ć13
Creating a Procedure
continued
Example
Update the salary of the specified employee to the specified amount through a

procedure.
PROCEDURE change_salary
(v_emp_id IN NUMBER, -- formal parameters
v_new_salary IN NUMBER)
IS
BEGIN -- begin PL/SQL block
UPDATE s_emp
SET salary = v_new_salary
WHERE id = v_emp_id;
COMMIT;
END change_salary;
This procedure, when invoked, will take the parameters for the employee number and
the new salary, and update the salary for that specified employee.
The content of the PL/SQL block will be covered in the lesson “Developing a Simple
PL/SQL Block.”
Eliminate Unnecessary IN Arguments
D
Where possible, derive values in the procedure, or use column default values.
D
Generate the primary key using a database sequence.
D
Record the username from the USER function.
D
Record the current date from the SYSDATE function.
D
Take advantage of business rules to compute input values automatically using a
formula.

×