0% found this document useful (0 votes)
109 views12 pages

Sub Programs in PL/SQL

This document discusses PL/SQL subprograms, which are named blocks of code that can take parameters and be invoked. There are two types: procedures, which perform actions, and functions, which compute values and return them. Subprograms provide benefits like extensibility, modularity, reusability, and maintainability. Procedures are created using the CREATE PROCEDURE statement and can include parameters, declarations, executable code, and exceptions. Functions are similar but use the CREATE FUNCTION statement and RETURN a value. Parameters can be passed positionally or by name.

Uploaded by

Hariom Vibhute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views12 pages

Sub Programs in PL/SQL

This document discusses PL/SQL subprograms, which are named blocks of code that can take parameters and be invoked. There are two types: procedures, which perform actions, and functions, which compute values and return them. Subprograms provide benefits like extensibility, modularity, reusability, and maintainability. Procedures are created using the CREATE PROCEDURE statement and can include parameters, declarations, executable code, and exceptions. Functions are similar but use the CREATE FUNCTION statement and RETURN a value. Parameters can be passed positionally or by name.

Uploaded by

Hariom Vibhute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

PL/SQL

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

● Subprograms provide extensibility; that is, they let you tailor


the PL/SQL language to suit your needs.

● Subprograms provide modularity; that is, they let you break


a program down into manageable, well-defined modules.

● Subprograms promote reusability and maintainability.

● Subprograms aid abstraction,


Procedures

CREATE [OR REPLACE] PROCEDURE proc_name


[(argument1[IN\OUT\INOUT]type),
(argument2[IN\OUT\INOUT]type),…..] IS\AS
[local declarations]
BEGIN
executable statements
[EXCEPTION
exception handlers]
END [proc_name];
Example of a Procedure
CREATE OR PROCEDURE debit_account (acct_id IN NUMBER,
amount IN NUMBER) IS
old_balance NUMBER;
new_balance NUMBER;
overdrawn EXCEPTION;
BEGIN
SELECT bal INTO old_balance FROM accts WHERE
acct_no = acct_id;
new_balance := old_balance - amount;
IF new_balance < 0 THEN
RAISE overdrawn;
ELSE
UPDATE accts SET bal = new_balance WHERE
acct_no = acct_id;
END IF;
EXCEPTION
WHEN overdrawn THEN
Dbms_output.Put_line(‘The account doesn’t have
sufficient balance’);
Example of a procedure
DECLARE
account_num NUMBER(3):=100;
amt NUMBER(7,2):= 5000.00;
BEGIN
debit_account (account_number , amt)
END;
Actual Versus Formal Parameters

● Subprograms pass information using parameters.

● The variables or expressions referenced in the


parameter list of a subprogram call are actual
parameters. (Account_num,amt)

● The variables declared in a subprogram spec and


referenced in the subprogram body are formal
parameters. (acct_id ,amount)
Positional Versus Named Notation

● Indicates the association between an actual and formal


parameter by position or name.

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

You might also like