Professional Documents
Culture Documents
Subprograms
(Procedures & Functions)
What is a Subprogram?
● Subprograms are named PL/SQL blocks that can take
parameters and be invoked.
● These can be stored in the database and run when
appropriate
● Like unnamed or anonymous PL/SQL blocks,
subprograms have a declarative part,
an executable part, and an optional exception-handling
part.
● PL/SQL has two types of subprograms called procedures
and functions.
● Generally Procedures are used to perform an action and a
function to compute a value.
Advantages of Subprograms
DECLARE
Acct_id NUMBER;
amount NUMBER;
PROCEDURE debit_account (acct_id NUMBER, amount
NUMBER) IS ...
BEGIN
debit_account(account_num, amt);
-- positional notation
debit_account(amt => amount, account_num => acct);
-- named notation
Parameter Modes
Functions
CREATE [OR REPLACE] FUNCTION fun_name
[(argument1[IN\OUT\INOUT]type),
(argument2[IN\OUT\INOUT]type),…..]
RETURN return_datatype IS\AS
[local declarations]
BEGIN
executable statements
[EXCEPTION
exception handlers]
END [fun_name];
Function Example
CREATE OR REPLACE FUNCTION balance
(acct_id INTEGER) RETURN REAL IS
acct_bal REAL;
BEGIN
SELECT bal INTO acct_bal FROM accts
WHERE acct_no = acct_id;
RETURN acct_bal;
END balance;
Thank You