Professional Documents
Culture Documents
Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
Unit 4: PL/SQL Programming
(16 Marks)
PL/SQL
PL/SQL stands for Procedural Language/Structured Query Language.
It is a programming language which allows the user to write the program to do the various
operations on the databases.
PL/SQL Block Structure:
The parts of PL/SQL block structure are-
1. Declaration
2. Execution
3. Exception
The basic units of PL/SQL are procedures and functions.
[DECLARE
……..declarations]
BEGIN
…….statements
[EXCEPTION
……….handlers]
END;
1. Declaration:
In this part the variable declaration is done.
Ex: i number;
OR
i number: =1;
2. Begin:
In this part the execution statements are written.
Ex: dbms_output.put_line(‘welcome to SQL’);
3. Exception:
In this part the exceptions due to the BEGIN block can be caught and exception handling
is done to execute the program.
Exceptions are designed to detect and handle run time errors.
Ex: begin
….statements;
exception
When zero_divide then
dbms_output.put_line(‘Number cannot be divided by zero’);
1
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
CONDITIONAL CONTROL
SQL> declare
2 a number;
3 begin
4 a:=&a;
5 if (mod(a,2)=0) then
6 dbms_output.put_line(a||'is even number');
7 else
8 dbms_output.put_line(a||'is odd number');
9 end if;
10 end;
11 /
SQL> /
Enter value for a: 1
old 4: a:=&a;
new 4: a:=1;
1 is odd number
2
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
2. NESTED IF STATEMENTS
Syntax: if <condition1> then
<statement1>;
else
if <condition2> then
<statement2>;
else
<else statement>;
end if;
end if;
3
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
Output 2:
Enter value for a: 1
old 6: a:=&a;
new 6: a:=1;
Enter value for b: 1
old 7: b:=&b;
new 7: b:=1;
Enter value for c: 1
old 8: c:=&c;
new 8: c:=1;
All numbers are equal
4
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
ITERATIVE CONTROL
For the repetition purpose iterative control is used.
1. SIMPLE LOOP
Syntax: loop
Statements;
End loop;
Example:
declare
i number:=0;
begin
loop
dbms_output.put_line(i);
i:=i+5;
exit when i>20;
end loop;
end;
/
Output:
0
5
10
15
20
5
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
2. WHILE LOOP
The WHILE loop is used to iterate the statements till a particular condition is true.
Syntax:
while(condition)
statements;
incrementer/decrementer;
end loop;
3. FOR LOOP
The FOR loop is used to repeat the statements given within it.
declare
i number;
begin
for i in 1..10
loop
dbms_output.put_line(i);
end loop;
end;
6
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
Output:
1
2
3
4
5
6
7
8
9
10
declare
i number;
begin
for i in 1..10
loop
if(mod(i,2)=1) then
dbms_output.put_line(i);
end if;
end loop;
end;
Output:
1
3
5
7
9
7
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
SEQUENTIAL CONTROL
1. GO TO statement
The go to control is used to transfer the control or to change the sequence of the instructions.
Example:
declare
i number;
begin
for i in 1..10
loop
dbms_output.put_line(i);
if i=5 then
goto caught;
end if;
end loop;
<<caught>>
dbms_output.put_line(‘The I is caught’);
end;
Output
1
2
3
4
5
The i is caught
8
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
EXCEPTION HANDLING
Exceptions are designed to detect and handle run time errors.
When error occurs, the control of the program is given or passed to the exception section.
Different exceptions:
1. Predefined exceptions:
Predefined exceptions are exceptions that are already defined by oracle.
Predefined exceptions are ZERO_DIVIDE, TOO_MANY_ROWS, etc.
Example: declare
a number;
b number;
c number:=1;
begin
a:=&a;
b:=&b;
c:=a/b;
dbms_output.put_line('Result is: '||c);
exception
when zero_divide then
dbms_output.put_line('Number cannot be divided by zero');
end;
(The program raises the pre-defined exception zero_divide when user tried
to divide by zero.)
9
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
Output:
2. User-defined exceptions:
A user-defined exception is an error that is defined by the program (user).
User-defined exceptions are defined are declared in the declarative section of the
PL/SQL block.
Example: declare
empname emp.ename%type;
salary emp.sal%type;
less_sal exception; /user defined exception
begin
select ename, sal into empname, salary from emp where ename=’rahul’;
if salary < 10000 then
raise less_sal;
else
dbms_output.put_line(‘salary is greater than 10000’);
end if;
exception
when less_sal then
dbms_output.put_line(‘salary is less than 10000’);
end;
(The program raises the user-defined exception less_sal when salary of
‘rahul’ is less than 10000.)
Output
salary is less than 10000
10
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
CURSORS
SQL statements are processed in a specific area called as context area. The information
required to complete the processing and a pointer are present in the context area.
Cursors are like array that stores the data. Cursors are like single dimension array or double
dimension array.
Cursor data type is used when we need to take out or fetch number of values of the same
column or multiple columns of even multiple rows at a time from the table.
Types of cursor:
1. REF cursor.
2. Dynamic cursor.
3. Static cursor.
Static Cursors:
In static cursor the select statements are known at the compile time.
a. Implicit cursor.
b. Explicit cursor.
a. Implicit cursor:
Implicit cursors are internal cursors used by SQL. They are executed and used by SQL
internally.
Implicit cursors are known as default cursor as they are in built. When user executes DML
statements like INSERT, UPDATE, DELETE the implicit cursors are used.
Implicit cursors are system generated cursors.
Ex: select * from emp_details where sal>5000;
Ex: implicit cursor in PL/SQL
declare
begin
update emp_details set sal=sal+5000 where empno=111;
dbms_output.put_line(sql%rowcount);
end;
11
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
2. %NOTFOUND
This attribute returns FALSE if DML statement affect minimum one row.
This attribute returns TRUE if DML statement does not return single row.
Format: SQL%NOTFOUND
3. %ROWCOUNT
This attribute returns the number of rows that are affected by DML statements.
Ex:
declare
rows number(5);
begin
update emp_details set sal=sal+1000;
if sql%notfound then
dbms_output.put_line('none salaries are updated');
elsif sql%found then
rows:=sql%rowcount;
dbms_output.put_line('salaries of'||rows||'updated');
end if;
end;
b. Explicit cursor:
Explicit cursors are not in built. They are defined by the users.
Explicit cursors can use four attributes like ISOPEN, FOUND, NOTFOUND and ROWCOUNT.
Syntax: cursor_name%attribute
The attributes are as follows:
1. %ISOPEN
Returns TRUE if cursor is open and returns FALSE if cursor is not open.
2. %FOUND
This attribute returns TRUE if DML statement affect minimum one row.
This attribute returns FALSE if DML statement does not return single row.
Returns invalid cursor if cursor is closed.
3. %NOTFOUND
This attribute returns FALSE if DML statement affect minimum one row.
This attribute returns TRUE if DML statement does not return single row.
Views NULL before fetching first row.
4. %ROWCOUNT
This attribute returns the number of rows that are affected by DML statements.
12
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
Steps to create Explicit Cursor:
a. Declaring cursor:
Cursor is declared in the declaration section.
Syntax: cursor <cursor_name>is<select query>;
Ex: cursor a is select ename from emp_details where empno=3;
b. Opening cursor:
After declaring the cursor, the cursor needs to open.
Syntax: open <cursor_name>;
Ex: open a;
c. Fetching a record from cursor:
Once the cursor is declared and opened, we need to get records or rows from the
cursor. These records are accessed using the FETCH statement.
Syntax: fetch <cursor_name> into <variable_list>;
Ex: fetch a into name;
d. Closing cursor:
Once the cursor is opened and processing is over, we need to close it.
Syntax: close <cursor_name>;
Ex: close a;
13
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
PROCEDURES
A procedure can be defined as a subprogram that performs a specific task or action.
Ex 1:
SQL> declare
begin
checksal(3);
end;
salary and commission greater than 30000
Ex 2:
SQL> declare
begin
checksal(101);
end;
salary and commission less than or equal to 30000
14
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
Ex 3:
SQL> declare
begin
checksal(5);
end;
no data found
Procedure created.
15
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
PL/SQL procedure to display Addition of 2 numbers
Procedure created.
16
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
FUNCTIONS
The function is subprogram that returns value to calling program.
The function is a subprogram that is used for the purpose of code reusability.
The difference between the procedures and functions is that, a procedure cannot return a
value but function can return the value.
CREATING FUNCTION:
Syntax: create or replace function <function_name>[arguments] return datatype is variable
declarations;
begin
executable code;
exception
executable code;
end;
Example:
SQL> create or replace function fun1(minsal number,maxsal number) return number
is a number;
begin
select count(*) into a from emp_details where sal between minsal and maxsal;
return a;
end;
Function created.
EXECUTING FUNCTION:
SQL> declare
count1 number;
begin
count1:=fun1(15000,20000);
dbms_output.put_line('No. of records are:'||count1);
end;
/
Output:
No. of records are: 5
DELETING FUNCTION:
17
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
Advantages of functions:
1. Code reusability feature can be used.
2. It saves time and cost.
3. Increases the flexibility of the program.
4. Memory space required is less.
Functions can be called or used by SQL Procedures cannot be called or used by SQL
statements. statements.
18
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
TRIGGERS
Triggers can be referred as stored procedures that are fired or executed when an INSERT,
UPDATE or DELETE statement is given against the associated table.
CREATING TRIGGERS:
Example:
create or replace trigger trg1 before delete on emp_details
declare
begin
raise_application_error(-20000,'can not delete the record');
end;
Trigger created.
Types of triggers:
1. ROW triggers
2. STATEMENT triggers
3. BEFORE triggers
4. AFTER triggers
19
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
1.ROW trigger:
ROW trigger can be executed or fired every time when table is affected.
Example:
create or replace trigger trg2 before update on emp_details for each row
declare
begin
raise_application_error(-20001,'can not update record');
end;
Trigger created.
2.STATEMENT trigger:
STATEMENT trigger is used or fired single time inspite of considering the numbers of rows
present in the table that are affected or row that are not affected.
DELETING TRIGGER:
Triggers Procedures
Triggers are stored procedures used for raising Procedures are sub programs generally
the user defined errors. written for business logic.
Triggers have events and related actions. Procedures does not have events and related
actions.
Triggers are executed when a particular SQL Procedures are executed when they are
command is executed. called.
Triggers does not have in, out parameters. Procedures have in, out parameters.
Syntax: create or replace trigger Syntax: create or replace procedure
20
DMS-22319 Prof. A. A. Nibe
Lecturer, Computer, Vikhe Patil Polytechnic (0030)
Assignment
21