Procedures and Functions
Procedures and Functions
and Functions
What can you do with
PL/SQL?
Allows sophisticated data processing
Build complex business logic in a modular fashion
Use over and over
Execute rapidly – little network traffic
Stored procedures
Functions
Triggers
Stored Procedures
Defined set of actions written using PL/SQL
When called, the procedure performs actions
Can be called directly from other blocks
Two parts
Procedure specification or header
Procedure body
PROCEDURES
• A procedure is a module performing one or more
actions; it does not need to return any values.
• The syntax for creating a procedure is as follows:
CREATE OR REPLACE PROCEDURE name
[(parameter[, parameter, ...])]
AS
[local declarations]
BEGIN
executable statements
[EXCEPTION
exception handlers]
END [name];
PROCEDURES
• A procedure may have 0 to many parameters.
• Every procedure has two parts:
1. The header portion, which comes before AS
(sometimes you will see IS—they are
interchangeable), keyword (this contains the
procedure name and the parameter list),
2. The body, which is everything after the IS
keyword.
• The word REPLACE is optional.
• When the word REPLACE is not used in the header
of the procedure, in order to change the code in
the procedure, it must be dropped first and then
re-created.
Bordoloi and Bock
Example: Procedure
BEGIN
Greetings:= 'Hello World';
DBMS_OUTPUT.PUT_LINE(greetings);
END hello;
Example: Procedure
CREATE OR REPLACE PROCEDURE Discount
AS
CURSOR c_group_discount
IS
SELECT distinct s.course_no, c.description
FROM section s,enrollment e,course c
WHERE s.section_id = e.section_id
AND c.course_no = s.course_no
GROUP BY s.course_no, c.description,
e.section_id, s.section_id
HAVING COUNT(*) >=8;
BEGIN
FOR r_group_discount IN c_group_discount
LOOP
UPDATE course
SET cost = cost * .95
WHERE course_no = r_group_discount.course_no;
DBMS_OUTPUT.PUT_LINE
('A 5% discount has been given to'||
r_group_discount.course_no||' '||
r_group_discount.description
);
Bordoloi and Bock
END LOOP;
Calling a Procedure
In order to execute a procedure in SQL*Plus use the
following syntax:
EXECUTE Procedure_name
To display output
set serveroutput on size 4000
EXECUTE hello;
newprice:=oldprice+oldprice*percent/10
0;
END increase;
Calling a Procedure with Arguments
DECLARE
price_increase NUMBER(6,2) := 20;
newp NUMBER(6,2) := 0;
BEGIN
DBMS_OUTPUT.PUT_LINE('Current price: '||
price_increase);
increase(oldprice=>price_increase,newprice=>newp
);
DBMS_OUTPUT.PUT_LINE
('Price after increase: '|| newp);
END;We should see a new price of 21
PARAMETERS
Calling Procedure
Environ IN Argument
-ment OUT Argument
IN OUT Argument
DECLARE
….
BEGIN
….
EXCEPTION
….
END;
FORMAL AND ACTUAL PARAMETERS
• In a anonymous block
SET SERVEROUTPUT ON
DECLARE
v_description VARCHAR2(50);
BEGIN
v_description :=
show_description(&sv_cnumber);
DBMS_OUTPUT.PUT_LINE(v_description);
END;
• In a SQL statement
DECLARE
current_amt NUMBER:=100;
incorrect_amt NUMBER:=-5;
BEGIN
DBMS_OUTPUT.PUT_LINE(' Order and Discount');
DBMS_OUTPUT.PUT_LINE(current_amt || ' '||
discount(current_amt));
DBMS_OUTPUT.PUT_LINE(incorrect_amt||' '||
discount(incorrect_amt));
END;
Example
Write a PL/SQL function that accepts price and
onhand values, checks to be sure they are both
greater than 0 and multiplies them together. If
they are less than 0 return 0.
CREATE OR REPLACE FUNCTION total_amount (price
NUMBER, onhand NUMBER)
RETURN NUMBER IS
BEGIN
IF (price>0 AND onhand>0) THEN
return (price*onhand);
ELSE
return(0);
END IF;
END total_amount;