Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

Lab 7

PL/SQL

IS221:
DATABASE MANAGEMENT SYSTEMS
Outline
PL/SQL
Stored Procedures and Functions
Triggers

INFORMATION SYSTEMS DEPARTMENT 2


PL/SQL Architecture
 PL/SQL architecture consist of following three
components, displayed in the diagram below:

3
PL/SQL BLOCK
 In a PL/SQL program, code is written in blocks. Each PL/SQL block has
3 sections, which are:
 Declare section
 Begin section
 Exception section

Followed by END statement at the end.

4
Anonymous PL/SQL Blocks
 PL/SQL blocks are a part of large programs and are either sub-programs, or functions, or procedures,
or triggers etc., but they can be defined independently and can be executed as well.
 When PL/SQL block is defined with no header , it is known as Anonymous Block

Example:
set serveroutput on;
DECLARE
-- declare variable a and b Output:
-- and these variables have integer datatype 84
a number;
b number;
BEGIN
a:= 7;
b:= 77;
dbms_output.put_line('Sum of the number is: ' || (a+b) );
END;
/
5
Example :

New  Stu table

concepts

%TYPE &
%rowtype
attribute

6
Now we will define v_id variable we can use
%type
v_id stu.id%type;

Benefits: If the id field type is changed, v_id field


type will automatically change with it.

Also we can use %rowtype


v_row stu%rowtype;

Benefits: all field types v_row always consistent


with the stu field in a table that type.

INFORMATION SYSTEMS DEPARTMENT 7


Exercise-1 : PL/SQL
Write a PL/SQL code to calculate tax for an employee of an organization
–XYZ and to display his/her name & tax, by creating a table under
employee database as below.

Employee _salary

INFORMATION SYSTEMS DEPARTMENT 8


PL/SQL
SOLUTION:

INFORMATION SYSTEMS DEPARTMENT 9


Note :( the pl/sql code has been typed in notepad and saved as 15.Sql under e: directory.
Hence the command e:/15.Sql)

INFORMATION SYSTEMS DEPARTMENT 10


PL/SQL: OUTPUT

INFORMATION SYSTEMS DEPARTMENT 11


A cursor
is a pointer that points to a result of a query

An explicit cursor is an SELECT statement declared explicitly in the


declaration section of the current block

For an explicit cursor, you have control over its execution cycle
from OPEN, FETCH, and CLOSE.

INFORMATION SYSTEMS DEPARTMENT 12


A cursor
Declare a cursor
Before using an explicit cursor, you must declare it in the declaration section of a
block as follows:
CURSOR cursor_name IS query;
In this syntax:
First, specify the name of the cursor after the CURSOR keyword.
Second, define a query to fetch data after the IS keyword.
Before start fetching rows from the cursor, you must open it. To open a cursor, you
use the following syntax:
OPEN cursor_name;

INFORMATION SYSTEMS DEPARTMENT 13


Fetch from a cursor
The FETCH statement places the contents of the current row into variables. The
syntax of FETCH statement is as follows:
FETCH cursor_name INTO variable_list;
To retrieve all rows in a result set, you need to fetch each row till the last one.
Closing a cursor
After fetching all rows, you need to close the cursor with the CLOSE statement:
CLOSE cursor_name;
Closing a cursor instructs Oracle to release allocated memory at an appropriate
time

INFORMATION SYSTEMS DEPARTMENT 14


Exercise 2: PL/SQL
Write a PL/SQL code to calculate the total and the Percentage of marks
of the students in four subjects from the table- Student with the
schema given below.

STUDENT ( RNO , S1 , S2, S3, S4, total, percentage)

INFORMATION SYSTEMS DEPARTMENT 15


Exercise 2: PL/SQL
SOLUTION:
SQL> create table student(rno number(10),s1 number(10),s2
number(10),s3number(10),s4 number(10),total number(20),percentage
number(6));
Table created.
SQL> insert into student(rno,s1,s2,s3,s4)values(10011,56,78,79,56);
1 row created
SQL> insert into student(rno,s1,s2,s3,s4)values(10012,45,67,34,58);
1 row created.

INFORMATION SYSTEMS DEPARTMENT 16


Exercise 2 – PL/SQL
SQL> insert into student(rno,s1,s2,s3,s4)values(10013,76,86,94,58);
1 row created.
SQL> insert into student(rno,s1,s2,s3,s4)values(10014,57,48,39,92);
1 row created.

SQL> select * from student;

INFORMATION SYSTEMS DEPARTMENT 17


Exercise- 2 : PL/SQL

INFORMATION SYSTEMS DEPARTMENT 18


Exercise – PL/SQL
declare
 t student.total%type;
p student.percentage%type;
cursor stu is select * from student;
rw stu%rowtype;
begin
open stu;
loop
fetch stu into rw;
exit when stu%notfound;
t:=rw.s1+rw.s2+rw.s3+rw.s4;
p:=t*0.25;
update student set total=t,percentage=p where rno=rw.rno;
end loop;
close stu;
end;
/

INFORMATION SYSTEMS DEPARTMENT 19


Exercise 2 : PL/SQL

INFORMATION SYSTEMS DEPARTMENT 20


Named blocks
Named blocks have a specific and unique name for them. They are
stored as the database objects in the server. Since they are available as
database objects, they can be referred to or used as long as it is present
on the server.

INFORMATION SYSTEMS DEPARTMENT 21


PL/SQL: Stored Procedure

A Procedure is a subprogram unit that consists of a group of PL/SQL statements.


Each procedure in Oracle has its own unique name by which it can be referred.
This subprogram unit is stored as a database object.

Syntax:
CREATE OR REPLACE PROCEDURE
<procedure_name>
(
<parameterl IN/OUT <datatype>

)
[ IS | AS ] <declaration_part>
BEGIN
<execution part>
EXCEPTION
<exception handling part>
END;

22
Example: Stored procedure
CREATE OR REPLACE PROCEDURE Sum
(a IN number, b IN number)
IS
c number;
BEGIN
c := a+b;
dbms_output.put_line('Sum of two nos= '|| c);
END;
/
Output:
Procedure created

23
Example: Stored Procedure
For calling the procedure created following code will be executed:

set serveroutput on;


DECLARE
x number;
y number; OUTPUT:
BEGIN
x := &x; Enter value for x: 10
y := &y; Enter value for y: 20
Sum(x,y);
Sum of two nos= 30
PL/SQL procedure successfully created.
END;
/

24
PL/SQL : Function
These parameters should be included in the calling statement.
Function can also return the value through OUT parameters other than
using RETURN.

25
Example: Functions
set serveroutput on;
CREATE OR REPLACE FUNCTION Sum_f(a IN number, b IN number)
RETURN Number IS c number;
BEGIN
c := a+b;
RETURN c;
END;
/

Output: Function Created

26
Example : Function
For calling the function Sum following code will be executed:

set serveroutput on;


DECLARE
no1 number; Output:
no2 number; Enter value for no1 :5
result number; Enter value for no2 :5
BEGIN Sum of two nos=10
no1 := &no1; PL/SQL procedure successfully created.
no2 := &no2;
result := Sum_f(no1,no2);
dbms_output.put_line('Sum of two nos='||result);
END;
/

27
Exercise :
Trigger: Syntax and Example
CREATE OR REPLACE TRIGGER <trigger_name>
BEFORE/AFTER/INSTEAD OF
INSERT/DELETE/UPDATE ON <table_name>
REFERENCING (OLD AS O, NEW AS N)
FOR EACH ROW WHEN (test_condition)
DECLARE
-- Variable declaration;
BEGIN
-- Executable statements;
EXCEPTION
-- Error handling statements;
END <trigger_name>;
END;

28
Example: Trigger
drop table student_age;

create table student_age(


rno number(10),
name varchar2(20),
course varchar2(10),
age number(10)
);

29
Example: Trigger
CREATE OR REPLACE TRIGGER CheckAge
BEFORE
INSERT OR UPDATE ON student_age
FOR EACH ROW
BEGIN
IF :new.age>30 THEN
raise_application_error(-20001, 'Age should not be greater than 30');
END IF;
END;
/ Output:
Trigger created

30
Example: Trigger
Following is the STUDENT_AGE table,
RNO NAME COURSE AGE

11 ALIYA B.SC 20

12 Hanan B.SC 21

13 Fatima B.C.A 18

14 Hana B.D.S 20

15 Nihal M.D.S 19

31
Example: Trigger
After initializing the trigger CheckAge, whenever we will insert any new
values or update the existing values in the above table STUDENT our trigger
will check the age before executing INSERT or UPDATE statements and
according to the result of triggering restriction or condition it will execute
the statement.
Example 1:
INSERT into STUDENT_AGE values(16, 'Saina', 'BCOM‘, 32);

Output: Age should not be greater than 30

Example 2:
INSERT into STUDENT_AGE values(17, 'Anna',, 'BCOM',22);
Output: 1 row created

32
Example: Trigger
Example 3:
UPDATE STUDENT_AGE set age=31 where ROLLNO=12;

Output: Age should not be greater than 30

Example 4:
UPDATE STUDENT_AGE set age=23 where ROLLNO=12;

Output: 1 row updated.

33
Example 2

INFORMATION SYSTEMS DEPARTMENT 34


After creating the trigger
check how it works by the
following

Note :
To create trigger connect to sql using

User name : Connect system

Password : your password

INFORMATION SYSTEMS DEPARTMENT 35


INFORMATION SYSTEMS DEPARTMENT 36

You might also like