* stored procedures and stored functions
are stored as part of a database,
managed by the DBMS
* procedure - something that does not return
anything (like a void function in C++)
its purpose is to perform some desired
side-effects
(they are called like a statement,
NOT like an expression with a value)
function - something that does return a value
(like a function with a type other
than void in C++)
its purpose is to return something
(OK, and it might have side-effects,
also)
(they CAN be called like an expression
with a value)
* so, PL/SQL (in Oracle) allows you to create
three kinds of PL/SQL subroutines:
triggers
stored procedures
stored functions
* from where can you call a trigger?
* YOU can't, it is called when the triggering
action takes place;
from where can you call a stored function or
procedure?
* from another PL/SQL subroutine
* directly from the SQL*Plus command line
* from the application tier
* one source I saw made the following claim:
* the primary goal of triggers is to
maintain database integrity
* the primary goal of stored procedures
and functions is to implement business
rules
* YES, we are kind of violating that logical
n-tier software architecture we talked about
last time with these...!
* SO, we hopefully only do this when we
get some definite gain from it;
do it thoughtfully;
* PL/SQL basic block structure:
[DECLARE
variable declarations;]
BEGIN
[statements]
[EXCEPTION
exception handling statements]
END;
* you CAN nest these if you'd like
* if it is a top-level block,
this will be a preceded by a header
and FOLLOWED with:
/
if YOU want it to be created and compiled...!
and:
show errors
if you want to see any error messages
* stored procedures
what's the header for a stored procedure?
create [or replace] procedure <proc_name> {as | is}
[declarations] /* NO DECLARE keyword for
a proc of funct's outermost block */
begin
...same as a block, but NO return statement
* stored functions
what's the header for a stored function?
create [or replace] function <funct_name> return <ret_type> {as | is}
[declarations] /* NO DECLARE keyword for
a proc of funct's outermost block */
begin
...same as a block, but WITH return statement(s)
* looping!
* there are several looping variants!
don't use the ugly ones in course assignments!
[prof determines what's ugly for course assignments]
* these are looping versions approved for CS 328:
WHILE <condition>
LOOP
<desired_statements;>
END LOOP;
FOR <loop_index> IN <lowest_num> .. <highest_num>
LOOP
<desired_statements;>
END LOOP;