0% found this document useful (0 votes)
8 views

PLSQL

The document provides an overview of PL/SQL subprograms, specifically procedures and functions, including their syntax, execution, and examples. It also covers triggers, detailing their creation, execution conditions, and examples of how they operate in response to database modifications. Key concepts include parameter types for procedures, the structure of functions, and the event-condition-action model for triggers.

Uploaded by

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

PLSQL

The document provides an overview of PL/SQL subprograms, specifically procedures and functions, including their syntax, execution, and examples. It also covers triggers, detailing their creation, execution conditions, and examples of how they operate in response to database modifications. Key concepts include parameter types for procedures, the structure of functions, and the event-condition-action model for triggers.

Uploaded by

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

PL/SQL

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.

SQL> exec selectop(5);


maha
PL/SQL procedure successfully completed.
Example 4
SQL> create or replace procedure updateop(a in
number) is
2 n varchar2(10);
3 begin
4 n:='raj';
5 update student set name=n where roll_no=a;
6 end;
7 /
Procedure created.

SQL> exec updateop (5);


PL/SQL procedure successfully completed.
Example 4 ... Contd.
SQL> select * from student;

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.

SQL> exec deleteop(4);


PL/SQL procedure successfully completed.
Example 5 ... Contd.
SQL> select * from student2;

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

PL/SQL procedure successfully completed.


Function
It is a subprogram that computes the value.
Syntax:
Create or replace function <function_name>
[parameter_list] return datatype is
<local declaration>
begin
(executable statements)
[exception](exception handlers)
end;
Example 1
SQL> create or replace function greatest(a in number,b in number,c in number
2 return number is g number;
3 begin
4 if a>b and a>c then
5 g:=a;
6 else
7 if b>c then
8 g:=b;
9 else
10 g:=c;
11 end if;
12 end if;
13 return (g);
14 end;
15 /
Function created.
Example
SQL> declare
1 ... Contd.
2 a number(10); 3 b number(10); 4 c number(10); 5 d
number(10);
6 begin
7 a:=&a; 8 b:=&b; 9 c:=&c;
10 d:=greatest(a,b,c);
11 dbms_output.put_line('greatest number is'||d);
12 end;
13 /
Enter value for a: 4
old 7: a:=&a; new 7: a:=4;
Enter value for b: 5
old 8: b:=&b; new 8: b:=5;
Enter value for c: 23
old 9: c:=&c; new 9: c:=23;
greatest number is 23
Example 2
SQL> select * from student;

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.

{Before| After | Instead of } : This specifies when the


trigger would be executed. The Instead of clause is used
for creating trigger on a view.

{Insert [or] | Update [or] | Delete} : This specifies the


DML operation.

[Of col_name]: This specifies the column name that


would be updated.

[On table_name]: This specifies the name of the table


associated with the trigger.
Trigger ... Contd.
[Referencing old as o New as n]: This allows you to
refer new and old values for various DML statements,
like Insert, Update, and Delete.

[for each row]: This specifies a row level trigger, i.e.,


the trigger would be executed for each row being
affected. Otherwise the trigger will execute just once
when the SQL statement is executed, which is called
a table level trigger.

When(condition): This provides a condition for rows


for which the trigger would fire. This clause is valid
only for row level triggers.
Trigger Example 1
SQL> select * from student;
NAME NO
---------- ----------
a 1
b 2
c 3
d 4
Trigger Example 1...
Contd.
SQL> create or replace trigger dd
2 before insert on student
3 for each row
4 begin
5 delete from student where no=1;
6 end;
7 /

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

SQL> select * from student1;


no rows selected
Trigger Example 2...
Contd.
SQL> update stude set name='ee' where no=1;
1 row updated.
SQL> select * from student1;
NAME NO
---------- ----------
a 1
SQL> select * from student;
NAME NO
---------- ----------
q 2
c 3
d 4
e 5
ee 1
Trigger Example 3
AFTER INSERT trigger would not allow you to set
the inserted value, since the row will already
have been inserted into the table.
SQL> create or replace trigger cc
2 after insert on student
3 for each row
4 begin
5 update student set name='q' where no=2;
6 end;
7 /
Trigger created.
Trigger Example 3...
Contd.
SQL> select * from student;
NAME NO
---------- ----------
ee1 2
c 3
e 5
ee 1
SQL> insert into stude values('h',9);
insert into stude values('h',9)
*
ERROR at line 1:
ORA-04091: table SCOTT.STUDE is mutating,
trigger/function may not see it
ORA-06512: at "SCOTT.CC", line 2
ORA-04088: error during execution of trigger 'SCOTT.CC’

You might also like