Procedures & Functions
Procedures & Functions
[:=
[:= || DEFAULT
DEFAULT expr];
expr];
Notice that PL/SQL
Examples includes all SQL types,
…and more
Declare
Declare
birthday
birthday DATE;
DATE;
age
age NUMBER(2)
NUMBER(2) NOT
NOT NULL
NULL :=
:=
27;
27;
name
name VARCHAR2(13)
VARCHAR2(13) :=
:= 'Levi';
'Levi';
magic
magic CONSTANT
CONSTANT NUMBER
NUMBER :=
:= 77;
77;
valid
valid BOOLEAN
BOOLEAN NOT
NOT NULL
NULL :=
:=
Declaring Variables with the
%TYPE Attribute
Examples
Accessing column sname
in table Sailors
DECLARE
sname Sailors.sname%TYPE;
fav_boat VARCHAR2(30);
my_fav_boat fav_boat%TYPE :=
'Pinta';
...
Accessing
another variable
Declaring Variables with the
%ROWTYPE Attribute
reserves_record Reserves%ROWTYPE;
And how do we access the fields in
reserves_record?
reserves_record.sid:=9;
Reserves_record.bid:=877;
In this example, we declared a variable named "emp" with the %ROWTYPE
attribute, which means it has the same data type as a row in the "employees" table.
We then assigned values to the variable and inserted it into the "employees" table
using the INSERT INTO statement.
Creating a Cursor
• We create a Cursor when we want to go over a
result of a query (like ResultSet in JDBC)
• Syntax Example:
sailorData is a
DECLARE variable that
cursor c is select * from sailors; can hold a
ROW from
sailorData sailors%ROWTYPE; the sailors
table
BEGIN
open c; Here the
fetch c into sailorData; first row of
sailors is
inserted into
sailorData
SELECT Statements
DECLARE
v_sname VARCHAR2(10);
v_rating NUMBER(3);
BEGIN
SELECT sname, rating
INTO v_sname, v_rating
FROM Sailors
WHERE sid = '112';
END;
/
19
Function and Procedure
Parameter Types(cont..)
• Parameters are declared with data types but
without data type length or precision.
• a parameter may be declared as VARCHAR2
but it will not be declared with a length
component (VARCHAR2(30) would not be
valid).
20
Types of Parameters
• Modes:
– IN: procedure must be called with a value for the parameter.
Value cannot be changed
– OUT: procedure must be called with a variable for the parameter.
Changes to the parameter are seen by the user (i.e., call by
reference)
– IN OUT: value can be sent, and changes to the parameter are
seen by the user
• Default Mode is: IN
Example 13.4 – Procedure with No Parameters
CREATE OR REPLACE PROCEDURE DisplaySalary IS
temp_Salary NUMBER(10,2);
BEGIN
SELECT Salary INTO temp_Salary FROM Employee
WHERE EmployeeID = '01885';
IF temp_Salary > 15000 THEN
DBMS_OUTPUT.PUT_LINE ('Salary > 15,000.');
ELSE
DBMS_OUTPUT.PUT_LINE ('Salary < 15,000.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('Employee not found.');
END DisplaySalary;
/
declare
declare
howmany
howmany mylog.logon_num%TYPE;
mylog.logon_num%TYPE;
begin
begin
num_logged(‘John',howmany);
num_logged(‘John',howmany);
dbms_output.put_line(howmany);
dbms_output.put_line(howmany);
end;
end;
//
declare
declare
paulRate:=9;
paulRate:=9;
Begin
Begin
dbms_output.put_line(ratingMessage(paulRate));
dbms_output.put_line(ratingMessage(paulRate));
end;
end;
//
• Memory Allocation
– Stored procedures take advantage of the shared memory capabilities of
Oracle
– Only a single copy of the procedure needs to be loaded into memory for
execution by multiple users.
• Productivity
– By designing applications around a common set of procedures, you can
avoid redundant coding and increase your productivity.
– Procedures can be written to insert, update, or delete rows from a table
and then called by any application without rewriting the SQL statements
necessary to accomplish these tasks.
– If the methods of data management change, only the procedures need to
be modified, not all of the applications that use the procedures.
Benefits of Procedures III
• Integrity
– Stored procedures improve the integrity and consistency of
your applications. By developing all of your applications
around a common group of procedures, you can reduce the
likelihood of committing coding errors.
– You can test a procedure or function to guarantee that it
returns an accurate result and, once it is verified, reuse it in
any number of applications without testing it again.
– If the data structures referenced by the procedure are
altered in any way, only the procedure needs to be
recompiled; applications that call the procedure do not
necessarily require any modifications.
Procedures VS Functions
• Procedures are traditionally the workhorse of
the coding world
• functions are traditionally the smaller, more
specific pieces of code.
• In general, if you need to update the chart of
accounts, you would write a procedure.
• If you need to retrieve the organization code
for a particular GL account, you would write a
function.
42
Procedures VS Functions(cont..)
Here are a few more differences between a
procedure and a function:
• A function MUST return a value
• A procedure cannot return a value
• Procedures and functions can both return data
in OUT and IN OUT parameters
• The return statement in a function returns
control to the calling program and returns the
results of the function
43
Procedures VS Functions (cont..)
• The return statement of a procedure returns
control to the calling program and cannot return
a value
• Functions can be called from SQL, procedure
cannot
• Functions are considered expressions,
procedure are not
• That's about all the differences I can think of off
the top of my head. Can you think of any more?
44