IS221: Database Management
PL/SQL Lab
1 Information Systems Department
Outline :
PL/SQL
Stored Procedures and Functions
Triggers
2 Information Systems Department
PL/SQL BlOCK STRUCTURE
3
PL/SQL block has the following structure:
DECLARE
Declaration statements
BEGIN
Executable statements
EXCEPTION
Exception-handling statements
END;
/
Example: Sample Program
4
Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be
nested within other PL/SQL blocks using BEGIN and END. Following is the
basic structure of a PL/SQL block −
Example:
BEGIN
dbms_output.put_line('Hello, World!');
END;
/
Example : Sample Program
5
When the above code is executed at the SQL prompt,
it produces the following result
Hello World!
PL/SQL procedure successfully completed.
Anonymous PL/SQL Blocks
6
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
-- and these variables have integer datatype
a number;
Output:
b number; 84
BEGIN
a:= 7;
b:= 77;
dbms_output.put_line('Sum of the number is: ' || (a+b) );
END;
/
Exercise-1 : PL/SQL
7
Write a PL/SQL code to calculate tax for an employee of
an organization to display his/her name & tax, by using
Employee _salary and employee tables.
ICS 424 - 01 (072) Concurrency Control Techniques
PL/SQL
8
SOLUTION:
ICS 424 - 01 (072) Concurrency Control Techniques
9
ICS 424 - 01 (072) Concurrency Control Techniques
PL/SQL: OUTPUT
10
ICS 424 - 01 (072) Concurrency Control Techniques
Case statement
11
ICS 424 - 01 (072) Concurrency Control Techniques
Example : IF Statement
12
ICS 424 - 01 (072) Concurrency Control Techniques
PL/SQL: Stored Procedure
13
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;
14
procedure-name specifies the name of the procedure.
[OR REPLACE] option allows the modification of an existing
procedure.
The optional parameter list contains name, mode and types of
the parameters. IN represents the value that will be passed
from outside and OUT represents the parameter that will be
used to return a value outside of the procedure.
procedure-body contains the executable part.
The AS keyword is used instead of the IS keyword for
creating a standalone procedure.
ICS 424 - 01 (072) Concurrency Control Techniques
Example 1:
15
ICS 424 - 01 (072) Concurrency Control Techniques
16
ICS 424 - 01 (072) Concurrency Control Techniques
Example2: Stored procedure
17
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
Example: Stored Procedure
18
For calling the procedure created following code will be executed:
set serveroutput on;
OUTPUT:
DECLARE
x number; Enter value for x: 10
y number; Enter value for y: 20
BEGIN Sum of two nos= 30
PL/SQL procedure successfully created.
x := &x;
y := &y;
Sum(x,y); set serveroutput on;
BEGIN
END; Sum(4,6);
/ END;
/
.
Example 3
19
create PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
/
ICS 424 - 01 (072) Concurrency Control Techniques
20
set serveroutput on;
DECLARE
a number;
b number;
c number;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
ICS 424 - 01 (072) Concurrency Control Techniques
PL/SQL : Function
21
Function can return the value through OUT parameters other than using
RETURN.
Example: Functions
22
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
Exercise
23
create and call a function. This function returns the
total number of employees in the employee table.
ICS 424 - 01 (072) Concurrency Control Techniques