PLSQL
PLSQL
Subprograms
Procedures
It is a subprogram that performs a
specific action.
Function
It is a subprogram that computes a
value.
Procedure
Syntax:
Create or replace procedure <proc_name>
[parameter_list] is
<local declaration>
begin
(executable statements)
[exception](exception handlers)
end;
Procedure ... Contd.
Procedure has two parts
Specification
It begin with the keywordprocedure
and ends with procedure name or
parameter list.
Body
It begins with the keyword is and
ends with the keyword end.
Syntax for executing procedure:
exec <proce_name> (parameters);
Procedure ... Contd.
Types of parameter passed to subprogram
In parameter: The in parameter mode is
used to pass values to the subprogram
when invoked.
Out parameter: The out parameter mode
is used to return values to the caller of a
subprogram.
Inout parameter: The inout parameter is
used to pass initial values to subprogram
when invoked and it also returns updated
values to the caller.
Procedure Example 1 -
Creation
SQL> create or replace procedure large(a in number,b in
number,c in number) is
2 begin
3 if a>b and a>c then
4 dbms_output.put_line('the greatest number is'||a);
5 else
6 if b>c then
7 dbms_output.put_line('the greatest number is'||b);
8 else
9 dbms_output.put_line('the greatest number is'||c);
10 end if;
11 end if;
12 end;
13 /
Procedure created.
Procedure Example 1-
Execution
SQL> exec large(5,7,3);
the greatest number is 7
PL/SQL procedure successfully
completed.
Example 2 - Creation
SQL> create or replace procedure large(a in number,b inout number,c out
number) is
2 begin
3 c:=100;
4 if a>b and a>c then
5 b:=a;
6 c:=a;
7 else if b>a and b>c then
8 c:=b;
9 else
10 b:=c;
11 end if;
12 end if;
13 dbms_output.put_line('the greatest number is'||b);
14 end;
15 /
Procedure created.
Example 2 - Execution
SQL> exec large(200,150);
the greatest number is 200
PL/SQL procedure successfully completed.
Example 3
SQL> select * from student;
NAME ROLL_NO
---------- ----------
malini 4
maha 5
Example 3 ... Contd.
SQL> create or replace procedure selectop(a in number) is
2 n varchar2(10);
3 begin
4 select name into n from student where roll_no=a;
5 dbms_output.put_line(n);
6 end;
7 /
Procedure created.
NAME ROLL_NO
---------- ----------
malini 4
raj 5
Example 5
SQL> create or replace procedure deleteop(a in
number) is
2 begin
3 delete from student where roll_no=a;
4 end;
5 /
Procedure created.
NAME ROLL_NO
---------- ----------
raj 5
Example 6
SQL> create or replace procedure fact( n in
number) is
2 fact number(10);
3 begin
4 fact:=1;
5 for i in 1..n
6 loop
7 fact:=fact*i;
8 end loop;
9 dbms_output.put_line('factorial of
number is'||fact);
10 end;
11 /
Example 6 ... Contd.
SQL> exec fact(5);
factorial of number is120
NAME ID FEES
---------- ---------- ----------
sita 78 678
ram 67 908
Example 2 ... Contd.
SQL> create or replace function selectop(roll
in number)
2 return number is n number;
3 begin
4 select fees into n from student
where id=roll;
5 return(n);
6 end;
7 /
Function created.
Example 2 ... Contd.
SQL> declare
2 a number(10);
3 b number(10);
4 begin
5 a:=&a;
6 b:=selectop (a);
7 dbms_output.put_line(b);
8 end;
9 /
Enter value for a: 67
old 5: a:=&a;
new 5: a:=67;
908
PL/SQL procedure successfully completed.
Example 3
SQL> create or replace function fact(n in number) return number
is
2 a number(10);
3 begin
4 a:=1;
5 for i in 1..n
6 loop
7 a:=a*i;
8 end loop;
9 return(a);
10 end;
11 /
Function created.
Example 3 ... Contd.
SQL> declare
2 n number(10);
3 fact number(10);
4 begin
5 n:=&n;
6 fact:=fact(n);
7 dbms_output.put_line('factorial of number is'||fact);
8 end;
9 /
Enter value for n: 4
old 5: n:=&n;
new 5: n:=4;
factorial of number is24
PL/SQL procedure successfully completed.
Trigger
A trigger is a statement that the system
executes automatically as a side effect
of a modification to the database.
Event-Condition-Action model
Trigger mechanism:
Specify when a trigger is to be
executed.
It is broken into an event that
causes the trigger to be checked and
as condition that must be satisfied
for trigger execution to proceed.
Specify the actions to be taken when
Trigger ... Contd.
Create [or Replace] trigger trigger_name
{Before| After | Instead of }
{Insert [or] | Update [or] | Delete}
[of col_name]
on table_name
[Referencing old as o New as n]
[for each row]
When(condition)
Declare
Declaration-statements
Begin
Executable-statements
Exception
Exception-handling-statements
End;
Trigger ... Contd.
Create [or Replace] trigger trigger_name : Creates or
replaces an existing trigger with the trigger_name.
Trigger created.
Trigger Example 1...
Contd.
SQL> insert into student values('e',5);
1 row created.
SQL> select * from student;
NAME NO
---------- ----------
b 2
c 3
d 4
e 5
If you need to set a column value in an inserted
row via trigger, then you need to use a before
insert trigger in order to access the new values.
Trigger Example 2
SQL> create or replace trigger ddd
2 before update on student
3 for each row
4 when (old.no/new.no>0)
5 begin
6 insert into student1
values(:old.name,:old.no);
7 end;
8 /
Trigger created.
Trigger Example 2...
Contd.
SQL> select * from student;
NAME NO
---------- ----------
q 2
c 3
d 4
e 5
a 1