Exception Section
• An exception is any error that occurs
during program execution.
• Syntax of error handling:
– for specific exceptions
when exception_name then
error-handling-code;
– for unspecified exceptions
when others then
error-handling-code;
System Pre-defined Exceptions
Examples:
invalid_cursor: sqlcode = -1001
too_many_rows: sqlcode = -1422
dup_val_on_index: sqlcode = -1
no_data_found: sqlcode = +100
• Each system exception has an error
message, stored in sqlerrm.
• System exceptions are raised
automatically by PL/SQL.
An Example
exception /* exception section */
when dup_val_on_index then
dbms_output.put_line(sqlcode ||
‘--’ || sqlerrm);
end;
Output message if the exception occurs:
-1--ORA-00001: unique constraint violated
More on Pre-defined Exceptions
exception name Oracle error number
cursor_already_open ORA-06511
dup_val_on_index ORA-00001
invalid_cursor ORA-01001
no_data_found ORA-01403
rowtype_mismatch ORA-06504
storage_error ORA-06500
too_many_rows ORA-01422
not_logged_on ORA-01012
login_denied ORA-01017
User-Defined Exceptions
• User-defined exceptions need to be
explicitly declared.
exception_name exception;
• User-defined exceptions need to be
raised by the user.
raise exception_name;
An Example – part 1
declare /* declare section */
invalid_gpa exception;
begin /* execution section */
if (gpa < 0 or gpa > 4.0) then
raise invalid_gpa;
end if;
exception /* exception section */
when invalid_gpa then
dbms_output.put_line(‘GPA value
is invalid.’);
end;
/
Exception Handling
Recommendation
• It is a good practice to handle all
exceptions explicitly and to handle
them in the exception section.
– Can improve reliability and
readability
Different Types of Blocks
• Anonymous Blocks (unnamed blocks)
• Procedures
procedure proc_name [parameters] is
[declaration] begin …
• Functions
function func_name [parameters]
return data_type is
[declaration] begin …
Example of An Anonymous Block
declare
company_payroll number;
begin
select sum(salary)
into company_payroll from Employees;
while company_payroll < 5000000 loop
update Employees
set salary = salary * 1.02;
select sum(salary) into
company_payroll from Employees;
end loop;
end;
/
Procedure
procedure procedure_name
[(parameter, parameter, …, parameter)] is
[local declarations]
begin
execution section;
[exception section]
end;
parameter definition:
parameter_name [in | out | in out] data_type
Sample Program 3 (1)
set serveroutput on
declare
v_cid customers.cid%type;
v_cname customers.cname%type;
v_city customers.city%type;
status boolean;
procedure get_customer(
cust_id in customers.cid%type,
cust_name out customers.cname%type,
cust_city out customers.city%type,
status out boolean) is
Sample Program 3 (2)
begin
select cname, city into cust_name, cust_city
from customers where cid = cust_id;
status := true;
exception
when no_data_found then status := false;
end;
begin
v_cid := ‘c001’;
get_customer(v_cid, v_cname, v_city, status);
Sample Program 3 (3)
if (status) then
dbms_output.put_line(v_cid || ‘ ‘ ||
v_cname || ‘ ‘ || v_city);
else
dbms_output.put_line(‘Customer ‘ ||
v_cid || ‘ not found’);
end if;
end;
/
Sample Program 4 (1)
set serveroutput on
declare
v_city customers.city%type;
customer_no number;
function no_of_customers(
cust_city in customers.city%type)
return number is
num_of_customers number;
begin
select count(*) into num_of_customers
from customers where city = cust_city;
Sample Program 4 (2)
return (num_of_customers);
end;
begin
v_city := ‘Dallas’;
customer_no := no_of_customers(v_city);
dbms_output.put_line(‘Number of customers
in ’ || v_city || ‘ is ‘ || customer_no);
end;
/
Stored Procedure and Function (1)
• procedure or function definitions can be
stored for later use (in SQL and other
PL/SQL blocks).
• Stored procedures/functions are stored in
compiled form.
• Stored procedures and functions are
created by
create or replace procedure proc_name …
create or replace function func_name …
– only in parameter allowed for function.
An Example – part 2
set serveroutput on
create or replace procedure get_cus_name(
v_cust_id in customers.cid%type) is
v_cust_name customers.cname%type;
begin
select cname into v_cust_name
from customers where cid = v_cust_id;
dbms_output.put_line(‘Customer
name: ‘ || v_cust_name);
end;
/
show errors
Stored Procedure and Function (2)
• Compile stored procedure in file proc.sql
SQL> start proc
show errors displays errors detected
during compiling the procedure.
• Execute stored procedure get_cus_name
SQL> execute get_cus_name(‘c001’);
Package
• A package is a group of related PL/SQL
objects (variables, …), procedures, and
functions.
• Each package definition has two parts:
– package specification
– package body
• Package specification provides an
interface to the users of the package.
• Package body contains actual code.
Package Specification (1)
create or replace package banking as
function check_balance
(account_no in Accounts.acct_no%type)
return Accounts.balance%type;
procedure deposit
(account_no in Accounts.acct_no%type,
amount in Accounts.balance%type);
Package Specification (2)
procedure withdraw
(account_no in Accounts.acct_no%type,
amount in Accounts.balance%type);
end;
/
show errors
Package Body (1)
create or replace package body banking as
function check_balance
(account_no in Accounts.acct_no%type)
return Accounts.balance%type is
acct_balance Accounts.balance%type;
begin
select balance into acct_balance
from Accounts
where acct_no = account_no;
return acct_balance;
end;
Package Body (2)
procedure deposit
(account_no in Accounts.acct_no%type,
amount in Accounts.balance%type) is
begin
if (amount <= 0) then
dbms_output.put_line(‘Wrong amount.’);
else update Accounts
set balance = balance + amount
where acct_no = account_no;
end if;
end;
Package Body (3)
procedure withdraw
(account_no in Accounts.acct_no%type,
amount in Accounts.balance%type) is
acct_balance Accounts.balance%type;
Package Body (4)
begin
acct_balance := check_balance(account_no);
if (amount > acct_balance) then
dbms_output.put_line(‘Insufficient
fund.’);
else update Accounts
set balance = balance - amount
where acct_no = account_no;
end if;
end;
end; /* end of the package body */
/
show errors
Public versus Private Constructs
• Public constructs of a package are listed in
the package specification.
• Private constructs are listed in the package
body but not in package specification.
• Public constructs can be accessed by others.
• Private constructs can only be accessed by
other constructs in the same package.
Example: If we remove the definition of the
check_balance function from the banking
package specification, it becomes private.
Use of Package
• Objects defined in a package specification
can be used in other packages, PL/SQL
blocks and SQL queries.
• Compile the package
– SQL> start packspec1 (or @packspec1)
– SQL> start packbody1
• May use execute to invoke a package.
SQL> execute banking.deposit(100, 200);
SQL> execute banking.withdraw(200, 500);
Use of a Stored Function
• Declare a variable
SQL> var bal number
• Execute the function
SQL> execute :bal :=
banking.check_balance(200);
• Print the value
SQL> print bal
Related Data Dictionary Information (1)
select object_name, object_type, created
from user_objects
where object_type = ‘PACKAGE’;
OBJECT_NAME OBJECT_TYPE CREATED
---------------------- --------------------- --------------
BANKING PACKAGE 07-SEP-98
Related Data Dictionary Information (2)
• To see the source code of a user_defined
object, use the following SQL query:
select text
from user_source
where name = ‘BANKING’;
• Both specification and body will be
shown.