Learning SQL - Chapter 14 Triggers and Stored Procedures
Learning SQL - Chapter 14 Triggers and Stored Procedures
14 Executing SQL
Routines and
Modules—Triggers
and Stored
Procedures
Note: Functions that you write yourself are often called user-de-
fined functions (UDFs) to distinguish them from functions such
as SUM (sometimes called BIFs, for Built-In Functions) that are
part of the SQL language.
or
DROP MODULE module_name
Note: Procedures can also use a parameter for output or for both
input and output.
Scope of Variables Variables declared within SQL functions and procedures are
local to the routine. Values passed in through a parameter list
are declared in the parameter list and therefore become local
variables. However, there are circumstances in which you may
want to use variables declared outside the function or proce-
dure (host language variables). In that case, there are two things
you need to do:
◊ Redeclare the host language variables using a SQL de-
clare section.
BEGIN SQL DECLARE SECTION;
redeclaration of host language
variables;
END SQL DECLARE SECTION;
Assume, for example, that the owner of the rare book store
wants to give a discount on a total purchase to customers who
order more than $100 on a single order. The code to do so
could be written
IF sale_total_amt >= 100 THEN
sale_total_amt = sale_total_amt * .9;
END IF;
A purchase from the rare book store that must be shipped is as-
sessed shipping charges based on the number of volumes in the
purchase. Assuming the number of volumes in the purchase is
stored in how_many, an IF construct to assign those shipping
charges might be written as
IF how_many <= 5 THEN
shipping_charges = how_many * 2;
ELSEIF how_many <= 10 THEN
shipping_charges = how_many * 1.5;
ELSE
shipping_charges = how_many;
END IF
294 Chapter 14: Writing and Executing SQL Routines and Modulesx
CASE The SQL CASE expression comes in two forms, one with a
single condition and one with multiple conditions. (The syn-
taxes are essentially the same as the CASE statement that can
be used in a SELECT clause.) In its simplest form, it has the
general format:
CASE logical_expression
WHEN value1 THEN executable_statement(s)
WHEN value2 THEN executable_statement(s)
WHEN value3 THEN executable_statement(s)
:
ELSE default
END CASE
WHILE The SQL WHILE is very similar to what you will find as part
of a general-purpose programming language:
loop_name: WHILE boolean_expression DO
body_of_loop
END WHILE
Note: Whenever a host language (in this case SQL) variable is used
in a SQL statement, it must be preceded by a colon, as in :price.
loop_name: REPEAT
body_of_loop
UNTIL boolean_expression
END REPEAT
REPEAT
INSERT INTO items_purchased
VALUES (6, CURRENT_DATE, :price);
funds = funds – price;
price = price * 1.1;
UNTIL price > funds
END REPEAT;
One of the things you might decide to do with a stored pro- Example #1:
cedure is simplify issuing a query or series of queries. For ex-
ample, suppose that the owner of the rare book store wants Interactive
to see the sales that were made each day along with the total Retrievals
sales. A single interactive SQL command won’t produce both a
298 Chapter 14: Writing and Executing SQL Routines and Modulesx
CREATE PROCEDURE sell_it (sale_numb INT, customer_id INT, book_id CHAR (17),
price_paid NUMBER (7,2), tax_rate NUMBER (6,3))
LANGUAGE SQL
MODIFIES SQL DATA
BEGIN
DECLARE tax;
tax = compute_tax (tax_rate, price_paid);
IF (SELECT COUNT(*) FROM volume WHERE sale_id = sale_numb) < 1 THEN
INSERT INTO sale (:sale_id, :customer_numb, :sale_date, :sale_total_
amt, :sales_tax) VALUES (sale_numb, customer_id, CURRENT_DATE, :price_paid,
tax);
END IF
UPDATE volume
SET selling_price = :price_paid,
SET sale_id = :sale_numb;
END