PLSQL
PLSQL
DECLARE
/*Declarative section is here */
BEGIN
/* Executable section is here */ 1
EXCEPTION
/* Exception section is here */
END;
Declaration syntax
Variable_name type[CONSTANT][NOT
NULL][:=VALUE];2
Where variable_name is the name of the variable, type is
the data type, and value is the initial value of the variable
Using %Type
sailor_id sailors.sid%TYPE;
DECLARE
s_id number(2):=99;
s_name varchar2(20):='John';
s_rating number(2):=7;
s_age number(3,1):=34.6;
BEGIN
END; 5
SELECT * FROM Sailors
SID NAME RATING AGE
22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5
99 John 7 34.6 6
Example Program2:
BEGIN
END;
7
SELECT * FROM Sailors
BEGIN
END;
9
SELECT * FROM Sailors
22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5
10
Example Program4:
set serveroutput on;
DECLARE
s_id sailors.sid%TYPE;
BEGIN
END;
Output:
s_id value is 22 11
PL/SQL Control Structures
IF-THEN-ELSE
IF Boolean_expression1 THEN
sequence_of_statements1;
[ELSIF Boolean_expression2 THEN
sequence_of_statements2;]
…
[ELSE
sequence_of_statements3;]
END IF;
12
LOOPS
Simple Loops
We can add one via the EXIT statement, which has this
syntax:
EXIT [WHEN condition]; 13
WHILE Loops
14
Numeric FOR Loops
The syntax is
EXCEPTION HANDLING
18
Oracle Error Equivalent Exception Description
19
ORA-1422 TOO_MANY_ROWS A SELECT..INTO statement
matches more than one row
Table 3.2 20
Short descriptions of some of the predefined exceptions
follow
INVALID_CURSOR
21
NO_DATA_FOUND
INVALID_NUMBER
22
User-Defined Exceptions
For example:
DECLARE
E_TooManyStudents EXCEPTION;
23
Raising Exceptions
24
DECLARE
e_TooManyStudents EXCEPTION; -- Exception to indicate an
error
-- Condition
v_CurrentStudents NUMBER(3); -- current number of students
-- registered for CS-101
v_MaxStudents NUMBER(3); -- Maximum number of students
-- allowed for CS-101
BEGIN
/*Find the current number of registered students,
and the maximum number of students allowed.*/
26
The syntax for the exception section is as follows:
EXCEPTION
WHEN exception_name THEN
sequence_of_statements1;
WHEN exception_name THEN
sequence_of_statements2;
WHEN OTHERS THEN
sequence_of_statements3;
END
The OTHERS Exception Handler
set serveroutput on
DECLARE
s_sname sailors.sname%TYPE;
BEGIN
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('NO DATA WAS FOUND!');
WHEN OTHERS THEN
dbms_output.put_line('SELECT CANNOT BE EXECUTED!');
END;
32
Explicit cursors
33
The four PL/SQL steps necessary for explicit cursor
processing are as follows:
1.Declare the cursor
2.Open the cursor for a query
3.Fetch the results into PL/SQL variables
4.Close the cursor
34
The following PL/SQL program illustrates a cursor
DECLARE
s_id number(2);
s_name varchar2(20);
s_rating number(2);
s_age number(3,1);
Cursor C_Sailors IS
SELECT sid,sname,rating,age
FROM sailors
where rating=7;
35
BEGIN
OPEN C_Sailors;
LOOP
/* Retrieve each row of the active set into PL/SQL
variables */
FETCH C_Sailors INTO s_id,s_name,s_rating,s_age;
/* If there are no more rows to fetch, exit the loop */
EXIT WHEN C_Sailors%NOTFOUND;
dbms_output.put_line(s_id||s_name||s_rating||s_age);
END LOOP;
PROCEDURE
Creating a Procedure
37
CREATE [OR REPLACE] PROCEDURE procedure_name
[(argument[{IN|OUT|INOUT}] type,
…
argument[{IN|OUT|INOUT)] {IS|AS}
procedure_body
Structure of a procedure:
CREATE OR REPLACE PROCEDURE procedure_name
AS
Declarative section
Executable section
EXCEPTION
Exception section
END [procedure_name];
39
Example Program:
BEGIN
area:=3.14 * radius * radius;
circumference:=2 * 3.14 * radius;
END circle_area_circum;
/
Procedure Usage for the above:
set serveroutput on
40
DECLARE
r number(10):=2;
a number(10,3):=0;
c number(10,5):=0;
BEGIN
circle_area_circum(r,a,c); --- call to procedure
dbms_output.put_line(r||' '||a||' '||c);
END;
/
Out put:
2 12.56 12.56
41
FUNCTION
Creating a Function
42
Function Syntax
Example Program:
create or replace function concatenate(pfirst in varchar2,
plast in varchar2) return varchar2 is
begin
return pfirst||plast; /* returning concatenated name */
end concatenate;
set serveroutput on
declare 44
fname varchar2(10):='sree';
lname varchar2(10):='nidhi';
coname varchar2(20);
begin
coname:=concatenate(fname,lname);
dbms_output.put_line(coname);
end;
Output: sreenidhi
45
EMP_NO EMP_NAME EMP_DEPT EMP_SALARY
1 Forbs ross Web Developer 45000
Example: create a function to return employee name based on the employee number
Usage:
select fun1(2) from emp; o/p: marks jems
(or)
Call function from the PL/SQL program
46
Packages
Package Specification
47
Syntax:
CREATE[OR REPLACE]PACKAGE package_name
{IS|AS}
procedure_specification|
function_specification|
variable_declaration|
type_definition|
exception_declaration|
cursor_declaration
END [package_name];
Package Initialization
52
Example Program:
Package usage:
SET SERVEROUTPUT ON
DECLARE
x1 number:= 2; -- radius
f1 number:= 5; -- fact
x number:= 0;
Note: package can be dropped using the following
sa number:= 0; command.
sb number:= 0; DROP PACKAGE package_name;
smin number:= 0;
smax number:= 0;
ssum number:= 0;
BEGIN
x:= demo.FACT(f1);
dbms_output.put_line('Factorial for '||f1||' is '||x);
demo.CIRCLE(x1,sa,sb);
dbms_output.put_line('Area is '||sa||' '||'Circumference is '||sb);
demo.Aggregation(smin,smax,ssum);
dbms_output.put_line('Min '||smin||' '||'Max '||smax||' Sum '||ssum);
END;
53
Triggers
54
Syntax for Creating a Trigger
55
56
57
58