PL/ SQL: Exceptions Cursors Procedures Functions Packages Triggers
PL/ SQL: Exceptions Cursors Procedures Functions Packages Triggers
1. Introduction
2. Exceptions
3. Cursors
4. Procedures
5. Functions
6. Packages
7. Triggers
Introduction:-
1) What is Pl/SQL ?
It is extension to SQL language.
PL/SQL = SQL + Programming features.
The following are the advantages of PL/SQL
1) We can use programming features like If stmt, loops, branching etc;
2) We can have user definied error messages by using the concept of exception handling.
3) We can perform related actions by using the concept of triggers.
4) Pl/SQL helps in reducing the network traffic.
PL/SQL Block structure:
----------------------
declare
........... -- Declare section
...........
...........
begin
........... -- Executable section
..........
...........
...........
...........
exception
.......... -- Exception section
..........
end;
/
A Pl/SQL block contains 3 sections.
1) Declare section
2) Executable section
3) Exception Section
1) Declare section:
-------------------
It is used to declare local variables, Cursor , exceptions etc;. All the lines between declare and
begin is called declare section.This section is optional.
2) Executable Section:
----------------------------
The actual task which should be done is written in the executable section. All the lines between
Begin and exception keywords is called as Executable section.
This section is mandatory
3) Exception Section:
-----------------------
If an exception is raised in the executable section,
control enters into exception section.
All the lines between exception and end is called exception section. This section is optional.
Ex1:
----
Write a PL/SQL block to display 'Hello World'.
For this program, we do not need any local variables.
So, we can start the program by using keyword begin.
Before the running this program, we need to make the environment variable serveroutput to ON.
To command to make the serveroutput to ON]
SQL> Set serveroutput on
Begin
dbms_output.put_line('Hello World');
end;
/
Hello World
Pl/SQL procedure successfully completed.
Ex 2:
-------
Write a PL/SQL block to calculate sum of two numbers.
For this program, we need 3 variables, so we need declare section.
Syntax to declare variable:
----------------------------
<variable> <datatype>(size);
Declare
a number(3);
b number(3);
c number(4);
begin
a :=10;
b :=20;
c := a+b;
dbms_output.put_line ( ' The sum is ...'||c);
end;
/
The sum is ...30
Pl/SQL procedure successfully completed.
In the above program, there are two important points to learn.
i) := is assignment operator, which is used to assign value from the right hand side to the variable
in the left hand side.
ii) || (pipe) is concatenation operator.
We can initilize at the time of declaration.
declare
a number(3) :=10;
b number(3) :=20;
In the abvoe program, we have hard coded the value 10 and 20 in the program. Instead of hard
coding the value, we can accept the values from the user.
Ex 3:
---
Write a program to accept two values from the user and display its sum.
Declare
a number(3);
b number(3);
c number(4);
begin
a := &a;
b := &b;
c := a+b;
dbms_output.put_line('The sum is ...'||c);
end;
/
Enter a value for A:40
Enter a value for B:30
The sum is ...70
Pl/SQL procedure successfully completed.
Note: & operator is used to accept value from the user.
Ex 4:
------
Write a PL/SQL block to accept empno and increments his salary by 1000.
Note: To increment the salary (change the value) in a table, we need to use update command.
Declare
l_empno number(4);
begin
l_empno := &empno;
update emp set sal = sal+1000
where empno = l_empno;
end;
/
Enter a value for empno: 7900
Procedure successfully completed.
To make the above update command permanent, we can use commit after update command in
PL/SQL block.
ex:
---
Declare
l_empno number(4);
begin
l_empno := &empno;
update emp set sal = sal+1000
where empno = l_empno;
commit;
end;
/
Writing a select stmt in a PL/SQL Block:
-------------------------------------------
Write a pl/SQL block which accepts empno and display ename and salary.
As ename and sal are the values present in the emp table, to get those values we need to write a
select stmt.
Note: Every select stmt in a PL/SQL block should have into clause.
Declare
l_empno number(4);
l_ename varchar2(20);
l_sal number(5);
begin
l_empno := &empno;
select ename,sal into l_ename, l_sal from emp
where empno = l_empno;
dbms_output.put_line(l_ename||'....'||l_sal);
end;
/
Note:
--------
As the above select stmt selects two columns, we need two local variable to catch the value
returned by the select stmt.
Using %TYPE attribute:
------------------------
%TYPE attribute is used to declare the local variables.
Instead of hardcoding the datatype and size for local variable, we can use %TYPE attribute.
Ex:
l_ename varchar2(20); -- we are hard coding datatype and size
l_ename emp.ename%TYPE; --- The datatype of ename column
of emp table is applicable to the local variable.
The above program, i use %TYPE attribute to declare local variables.
Declare
l_empno emp.empno%TYPE;
l_ename emp.ename%TYPE;
l_sal emp.sal%TYPE;
begin
l_empno := &empno;
select ename,sal into l_ename, l_sal from emp
where empno = l_empno;
dbms_output.put_line(l_ename||'....'||l_sal);
end;
/
Using %ROWTYPE Attribute:
--------------------------
We can make sure that the program is completed normally by catching the exception using
Exception section.
Syntax:
----------
Declare
.........
.........
begin
........
........
.........
Exception
When <Exception_handler> then
....................
....................
end;
/
Ex:
---
declare
l_sal emp.sal%type;
begin
dbms_output.put_line('Welcome');
select sal into l_Sal from emp
where empno = 2255;
dbms_output.put_line('The sal is ....'||l_sal);
dbms_output.put_line('Thank You');
Exception
when NO_DATA_FOUND then
dbms_output.put_line('Invalid empno');
end;
/
Output:
------
Welcome
Invalid empno
Pl/SQL Procedure successfully completed.
2) TOO_MANY_ROWS:
----------------------
TOO_MANY_ROWS exception is raised, when select stmt returns more than one row.
Ex:
----
declare
l_sal emp.sal%type;
begin
dbms_output.put_line('Welcome');
select sal into l_Sal from emp
where deptno=10;
dbms_output.put_line('The sal is ....'||l_sal);
dbms_output.put_line('Thank You');
end;
/
Output:
-----------
Welcome
Error
Note:
---------
As we get the output 'Welcome', this means that program execution is started.
As the select stmt returns more than one row, TOO_MANY_ROWS exception is raised.
As we know, Once an exception is raised control will not execute the remaining lines of
excutable section, searches for the Exception section.
As we do not have exception section, program is terminated abnormally.
We can avoid abnormal termination of the program by catching the Exception.
Ex:
---
declare
l_sal emp.sal%type;
begin
dbms_output.put_line('Welcome');
select sal into l_Sal from emp
where deptno=10;
dbms_output.put_line('The sal is ....'||l_sal);
dbms_output.put_line('Thank You');
Exception
When TOO_MANY_ROWS then
dbms_output.put_line( 'Select stmt returns more than one row');
end;
/
Output:
--------
Welcome
Select stmt returns more than one row.
Pl/SQL Procedure successfully completed.
3) ZERO_DIVIDE:
-----------------
This exception is raised, when we divide a number by zero.
Ex:
----
Declare
a number(4);
begin
dbms_output.put_line('Welcome');
a := 10/0;
dbms_output.put_line(a);
dbms_output.put_line('Thank You');
end;
/
Output:
--------
Welcome
Error
Note:
------
In the above program, as we are dividing by zero, ZERO_DIVIDE exception is raised.
As we are not catching the exception, program is terminated abnormally.
As a developer, we need to make sure that programs are completed successfully at any case.
SO we need to handle exception which is raised by using the Exception Section.
Ex:
--------
Declare
a number(4);
begin
dbms_output.put_line('Welcome');
a := 10/0;
dbms_output.put_line(a);
dbms_output.put_line('Thank You');
Exception
When ZERO_DIVIDE then
dbms_output.put_line('DO not divide by 0');
end;
/
Output:
-------
Welcome
DO not divide by 0.
Pl/SQL Procedure successfully completed.
4) VALUE_ERROR:
-----------------------
This exception is raised, when the value which is returned does not match with the datatype
variable.
Ex:
-------
Declare
l_ename number(10);
begin
dbms_output.put_line('Welcome');
select ename into l_ename from emp
where empno = 7369;
dbms_output.put_line('The employee name is...'||l_ename);
end;
/
Output:
-------
Welcome
Error
Note:
--------
As the select stmt returning char value, it cannot be stored in varible of number data.
In this case VALUE_ERROR exception is raised.
As we are not catching the exception, program is terminated abnormally.
We can avoid abnormal termination of the program by catching the exception using Exception
Section.
Ex:
----
Declare
l_ename number(10);
begin
dbms_output.put_line('Welcome');
select ename into l_ename from emp
where empno = 7369;
dbms_output.put_line('The employee name is...'||l_ename);
Exception
when VALUE_ERROR then
dbms_output.put_line('Pl check the datatype of the local variables');
end;
/
Output:
--------
Welcome
Pl check the datatype of the local variables
5) DUP_VAL_ON_INDEX:
----------------------------------------
This exception is raised when we try to insert a dulicate value on a primary key or unique key.
ex:
--------
Create the following table:
create table student ( sno number(3) primary key,
sname varchar2(20),
marks number(3));
insert a row in the table:
insert into student values (101,'arun',40);
commit;
begin
dbms_output.put_line ( 'Welcome');
insert into student values (101,'vijay',50);
dbms_output.put_line ( 'Thank You');
end;
/
Output:
-----------
Welcome
Error
Note:
---------
As we are inserting a duplicate value in a primary key column, DUP_VAL_ON_INDEX
exception is raised. As we are not catching the exception program is terminated abnormally.
We can avoid abnormai termination of the program by catching the exception.
Ex:
-------
begin
dbms_output.put_line ( 'Welcome');
insert into student values (101,'vijay',50);
dbms_output.put_line ( 'Thank You');
Exception
when DUP_VAL_ON_INDEX then
dbms_output.put_line('Do not insert duplicate value in a primary key');
end;
/
Output:
-------------
Welcome
Do not insert duplicate value in a primary key
When Others handler:
---------------------------------
When others can handle any type of exception
Ex1:
--------
Declare
a number(4);
begin
dbms_output.put_line('Welcome');
a := 10/0;
dbms_output.put_line(a);
dbms_output.put_line('Thank You');
Exception
When others then
dbms_output.put_line('Pl check the code');
end;
/
Output:
-----------
Welcome
Pl check the code
Note:
Exception that is raised is ZERO_DIVIDE.
We do not have ZERO_DIVIDE handler, but When Others can handler can handle this
exception.
Ex2:
---------
declare
l_sal emp.sal%type;
begin
dbms_output.put_line('Welcome');
select sal into l_Sal from emp
where deptno=10;
dbms_output.put_line('The sal is ....'||l_sal);
dbms_output.put_line('Thank You');
Exception
When others then
dbms_output.put_line('Pl check the code');
end;
/
Output:
-------------
Welcome
Pl check the code
+++++++++++++++++++++++++++++++++++
Non predefinied exception:
-------------------------------------
These exceptions will have exceptio number , but does not have exception name.
Ex:
-------
ORA-2292 exception. This exception is raised when we try to delete from row from the parent
table if correspoding row exists in the child table.
First lets establish parent-child relationship between two tables.
create table student2( sno number(3) primary key,
sname varchar2(20),
marks number(3));
insert into student2 values (101, 'arun',40);
insert into student2 values (102, 'varun',50);
insert into student2 values (103, 'kiran',60);
create table library2 ( roll_no number(3) references student2(sno),
book_name varchar2(20));
insert into library2 values (101,'Java');
insert into library2 values (102,'C++');
insert into library2 values (102,'Oracle');
commit;
begin
dbms_output.put_line('Welcome');
delete from student2 where sno =101;
dbms_output.put_line('Thank You');
end;
/
Output:
-----------
Welcome
Error
Note: We are deleteting the row from the parent table and the corresponding row exists in the
child table. So exception is raised. The exception which is raised in the above program is ORA-
2292. This exception does not have any name. This is an example of non -predefinied exception.
The following steps are to followed to handle non-pre definied exception.
Step 1: Declare the exception
Step 2: Associate the exception
Step 3: Handle then exception.
Syntax:
-----------
Step 1: Declare the exception
<Exception_name> Exception;
Step 2: Associate the exception
raise_application_error ( <exception_no> , <Exception_name> );
Step 3: Handle the exception
Exception
When < Exceptionb_name> then
............
...........
............
end;
/
Ex:
---
In the follwoing program , we perform the three step process to handle Non-pre definied
exceptions.
Declare
MY_EX1 Exception;
Raise_application_error ( -2292 , MY_EX1 );
begin
dbms_output.put_line('Welcome');
delete from student2 where sno =101;
dbms_output.put_line('Thank You');
Exception
When MY_EX1 then
dbms_output.put_line('Cannot delete from the parent table');
end;
/
Output:
------------
Welcome
Cannot delete from the parent table
Ex 3:
--------
Create a Procedure which accepts an empno and increments his salary by 1000.
create or replace procedure inc_sal ( a in number)
is
begin
update emp set sal = sal+1000
where empno = a;
end;
/
Procedure created.
TO execute the procedure:
---------------------------
SQL> exec inc_sal(7900)
We can improve the above procedure code by using %type attribute in procedure parameters.
The above procedure can be re-written as below :
create or replace procedure inc_sal ( a in emp.empno%type)
is
begin
update emp set sal = sal+1000
where empno = a;
end;
/
Ex 4:
-------
Create a procedure which accepts empno and display ename and salary.
create or replace procedure display_emp ( l_empno emp.empno%type)
is
l_ename emp.ename%type;
l_sal emp.sal%type;
begin
select ename, sal into l_ename,l_sal from emp
where empno = l_empno;
dbms_output.put_line(l_ename||'....'||l_sal);
exception
when no_data_found then
dbms_output.put_line('Invalid empno');
end;
/
Ex 5:
--------
Create a procedure which accepts deptno and display ename and salary of employees working in
that department.
create or replace procedure display_emp1 (l_deptno emp.deptno%type)
is
cursor c1
is select ename,sal from emp
where deptno = l_deptno;
begin
for emp_rec in c1 loop
dbms_output.put_line(emp_rec.ename||'...'||emp_rec.sal);
end loop;
end;
Ex 6:
--------
We can call a procedure from another procedure.
create or replace procedure demo1
is
begin
dbms_output.put_line('This is from demo1');
end;
/
create or replace procedure demo2
is
begin
dbms_output.put_line ('Welcome');
demo1;
dbms_output.put_line ('Thank you');
end;
/
SQL> Exec demo2
Ex 7:
---------
We can call multiple procedures at a time using PL/SQL block.
begin
p1;
add_num(10,20);
inc_sal(7900);
end;
/
Ex 8:
--------
If there are any syntax errors in the procedure code, then the
procedcure is created with compilation errors.
create or replace procedure add_num ( a IN number,
b IN number)
is
c number(3);
begin
c := a+b;
dbms_outut.put_line(' The sum is '||c);
end;
/
Procedure is created with compilation errrors.
To see the errors, use the following command.
SQL> sho err
We get error information.
Rectify the error and re compile the code to create procedure successfully.
Ex 9:
---------
Sub procedure: A procedure inside another procedure is called as
Sub procedure.
create or replace procedure test
is
procedure sample
is
begin
dbms_output.put_line('This is from sample');
end;
begin
dbms_output.put_line('This is from test');
sample;
end;
In the above example procedure sample is called as Sub procedure.
A Sub procedure can be invoked from the main procedure only.
SQL> EXEC test
This is from test
This is from sample
We cannot invoke the Sub procedure independently.
The following command will give error.
SQL>EXEC sample
Ex 10:
----------
OUT parameters are used to return the values to the calling environment.
create a procedure which accepts empno and return salary.
create or replace procedure ret_sal( l_empno in emp.empno%type,
l_sal out emp.sal%type)
is
begin
select sal into l_sal from emp
where empno = l_empno;
end;
As the procedure is returning a value using OUT parameter,
we need to have a bind variable to catch the value. We need to follow a 3 step process to execute
the above procedure.
Step 1: Create bind variable
Step 2: Execute the procedure using bind variable
Step 3: Print the value in the bind variable.
Step 1: creating Bind variable
SQL> variable g_sal number
Step 2: Invoking the procedure using bind variable
SQL> Exec ret_sal( 7900, :g_sal)
Step 3: Print the value in the bind variable
SQL> Print g_sal
Ex 11:
-----------------
IN OUT parameters are used to accept the value as well as return the values to the calling
environment.
Create a procedure which accepts a number and return its square.
create or replace procedure cal_square( a In OUT number)
is
begin
a := a*a;
end;
/
To run the above proceure we need to follow a four step process.
Step 1: Create Bind variable
Step 2: Initiate the Bind variable
Step 3: Invoke the procedure using bind varaible
Step 4: Print the value in the bind variable
Step 1:
SQL> Variable n number
Step 2:
begin
:n :=5;
end;
/
Step 3:
SQL> Exec cal_square (:n)
Step 4:
SQL> Print n
Ex 12:
------------
To see the list of procedures, use the following queries
SQL> select object_name from user_objects where
object_type='PROCEDURE';
or
SQL> select procedure_name from user_procedures.
Ex 13:
-------
Using Default keyword:
-----------------------
create or replace procedure add_num3( a number,
b number default 100,
c number default 200)
is
d number(5);
begin
d := a+b+c;
dbms_output.put_line('The sum is ...'||d);
end;
/
Procedure created.
To execute the procedure
SQL> EXEC add_num3(10,20,30)
Packages:-
cPackages are logically related sub programs.
Package creating involves two steps.
Step 1: Creating Package specification (PKS )
Step 2: Creating Package Body ( PKB )
Package Specification:
-----------------------------
It contains declaration of sub programs
Syntax:
----------
create or replace package <package_name>
is
declaration of procedures;
declaration of functions;
end;
/
Package Body:
-----------------------------
It contains definition of sub programs
Syntax:
----------
create or replace package body <package_name>
is
definition of procedures;
definition of functions;
end;
/
Ex:
----
Lets create a package with two procedures and function.
Procedure add_num -- which takes two parameters and display its sum.
Procedure display_emp -- which accepts empno and display ename and sal.
Function cal_tax -- which accepts sal and returns tax value (10% of sal is tax value ).
Package Specification:
---------------------------
create or replace package test_pack
is
procedure add_num ( a number,
b number);
procedure display_emp ( l_empno emp.empno%type);
function cal_tax ( l_sal emp.sal%type)
return number;
end test_pack;
/
Package body:
--------------------------
create or replace package body test_pack
is
procedure add_num ( a number,
b number)
is
c number;
begin
c := a+b;
dbms_output.put_line('The sum is '||c);
end;
procedure display_emp (l_empno emp.empno%type)
is
l_ename emp.ename%type;
l_sal emp.sal%type;
begin
select sal into l_sal from emp
where empno = l_empno;
dbms_output.put_line(l_ename||'.......'||l_sal);
end;
function cal_tax ( l_sal emp.sal%type)
is
l_tax number;
begin
l_tax := l_sal *10/100;
return l_tax;
end;
end test_pack;
/
To invoke sub programs inside the package:
--------------------------------------------
SQL> EXEC test_pack.display_emp (7900)
SQL> select empno, ename, sal, test_pack.cal_tax (sal) from emp;
Procedure overloading using packages:
---------------------------------------
We can achieve procedure overloading using Packages.
Basing on the no of parameters and datatype of the parameters,
the appropriate procedure is invoked.
ex:
----
Create or replace package test_pack2
is
procedure p1 ( a number,
b number);
procedure p1 ( a number);
end test_pack2;
/
create or replace package body test_pack2
is
procedure p1 ( a number,
b number)
is
c number;
begin
c := a+b;
dbms_output.put_line('The sum is'||c);
end;
procedure p1 ( a number)
is
begin
dbms_output.put_line('The square of the number is '||a*a);
end;
end test_pack2;
/
In the above package there are two procedures with the same name.
Appropriate procedure is invoked basing on the no of parameters which are passed
at the time of calling the procedure.
Ex:
--
SQL> exec test_pack2(10, 20);
The sum is 30
SQL> exec test_pack2(10);
The square of the number is 100
Ex:
--------
Create a trigger which restrict delete operation on emp
if job is president.
create or replace trigger trg1
before delete on emp for each row
begin
if :OLD.JOB='PRESIDENT' then
raise_application_error(-20151,
' cannot delete president');
end if;
end;
/
Trigger created.
Event:
---------
delete from emp where ename='KING';
Error:
ORA-20151, cannot delete president
Instead of triggers:
------------------------
Instead of triggers are helpful to perform DML operations on complex view.
Example of complex view:
------------------------
create or replace view emp_dept_v
as
select e.empno, e.ename, e.sal, e.deptno, d.dname, d.loc
from emp e, dept d
where e.deptno = d.deptno;
View created.
Generally, we cannot insert row into complex view.
But, by using the instead of triggers, we can do it.
ex:
----
create or replace trigger trg1
instead of insert on emp_dept_v for each row
begin
insert into dept values (:NEW.deptno,:NEW.dname, :NEW.loc);
insert into emp ( empno, ename,sal, deptno) values
(:NEW.empno, :NEW.ename,:NEW.sal, :NEW.deptno);
end;
/
Trigger Created.
Event:
------
insert into emp_dept_v values (2121,'VIJAY',3000,60,'TRAINING','HYDERABAD');
1 Row created.
To see the list of triggers:
-------------------------------
select trigger_name from user_triggers;
To drop a trigger:
------------------
Drop trigger <trigger_name>;
Ex:
-----
Drop trigger trg1;
Trigger Droped.