PL SQL
PL SQL
04-PL/SQL Programming
Unit No. 4.PL/SQL
Programming
Unit No. 4.PL/SQL
Programming
Introduction
• PL/SQL stand for Procedural Language /
Structured Query Language
• An extension to SQL with design features of
programming languages .
• Allows user to write a program to do the
various operations on databases.
• PL/SQL is a combination of SQL statements
with the programming Language.
BEGIN – Mandatory
EXCEPTION – Optional
1) Date: This data type is used to represent date and time. The
Default format is : DD-MM-YY , Example : ‘20-oct-14’
6)RAW LONG : - This data type is used store Binary data such as
as Digitized, Picture or Image Maximum storage up to 2 GB.
variable_name tablename.fieldname%TYPE;
K. K. WAGH
POLYTECHNIC, NASHIK
GENERATING OUTPUT
SET SERVEROUTPUT ON;
Put the command at the beginning of the
program, right before the declaration section.
SQL> DECLARE
BEGIN
dbms_output.put_line(Message);
END;
/
Output as follows :
old 6: a := &a;
new 6: a := 2;
SQL> declare
2 radius number(3);
3 pi number:=3.14;
4 area number(8,3);
5 begin
6 radius:=& radius;
7 area:= pi * power(radius,2);
8 dbms_output.put_line('area of circle is' || area);
9 end;
10 /
SQL> declare
2 date1 date;
3 begin
4 date1 := sysdate +7;
5 dbms_output.put_line(date1);
6 end;
7 /
28-AUG-15
SQL> declare
2 name varchar2(30);
3 begin
4 name := '& name';
5 dbms_output.put_line('name is' || lpad(name,15,'*'));
6 end;
7 /
Enter value for name: kunal
old 4: name := '& name';
new 4: name := „kunal';
name is**********kunal
SQL> declare
2 newsal emp.sal%type;
3 val number;
4 begin
5 val:=& val;
6 update emp set sal= sal + val;
7 dbms_output.put_line( 'the salary is increased by' || val);
8 select sal into newsal from emp where ename= 'ajay';
9 dbms_output.put_line('salary of ajay is'|| newsal);
10 end;
11 /
Conditional Control
1)IF - THEN
2)IF – THEN- ELSE
3)IF –THEN –ELSE –IF
K. K. WAGH POLYTECHNIC, NASHIK
1)IF - THEN
• An IF-THEN statement allows you to specify only
one group of actions to take.
• If condition is TRUE then statement get execute ,if
False then if statement does nothing .
Syntax:
IF <condition> then
Statements;
End IF ;
statements;
Else
statements;
End if ;
K. K. WAGH POLYTECHNIC, NASHIK
Program for finding out greatest between two numbers.
Declare
A number(3);
B number(3);
Begin
A:=&A;
B:=&B;
If (A>B) then
Dbms_output.put_line(‘ Greater number=‘||A);
else
Dbms_output.put_line(‘ Greater number=‘||B);
End if;
End;
/ K. K. WAGH POLYTECHNIC, NASHIK
Enter value for a: 10
old 5: A:=&A;
new 5: A:=10;
Greater number=20
1
2
3
4
5
PL/SQL procedure successfully completed.
K. K. WAGH POLYTECHNIC, NASHIK
Program to print the numbers from 20 to 15 in reverse
SQL> declare
2 i number(10):=20;
3 begin
4 loop
5 dbms_output.put_line(i);
6 i:=i-1;
7 exit when i<15;
8 end loop;
9 end;
10 /
20
19
18
17
16
K. K. WAGH POLYTECHNIC, NASHIK
15
Program to print the even numbers from 1 to 10
SQL> declare
2 i number:=1;
3 begin
4 loop
5 if (mod(i,2)=0) then
6 dbms_output.put_line(i);
7 else
8 null;
9 end if;
10 i:=i+1;
11 exit when i>10;
12 end loop;
13 end;
14 /
2
4
6
8
10
K. K. WAGH POLYTECHNIC, NASHIK
PL/SQL procedure successfully completed.
2)While Loop
statements;
END LOOP;
K. K. WAGH POLYTECHNIC, NASHIK
Flowchart of While Loop
SQL> declare
2 a number :=1;
3 begin
4 while a <= 5 loop
5 dbms_output.put_line(a);
6 a:=a+1;
7 end loop;
8 end;
9 /
1
2
3
4
5 K. K. WAGH POLYTECHNIC, NASHIK
Program to find the factorial of number
SQL> declare
2 i number(10):=1;
3 fact number:=1;
4 num number(10);
5 begin
6 num:=#
7 while (i<=num) loop
8 fact:=fact*i;
9 i:=i+1;
10 end loop;
11 dbms_output.put_line(fact);
12 end;
13 /
Enter value for num: 5
old 6: num:=#
new 6: num:=5;
120
PL/SQL procedure successfully completed.
K. K. WAGH POLYTECHNIC, NASHIK
3)For Loop
•A FOR LOOP is used to execute a set of statements for a
predetermined number of times.
•Iteration occurs between the start and end integer values given.
statements;
END LOOP;
SQL> declare
2 a number (10);
3 begin
4 for a in 1..5 loop
5 dbms_output.put_line(a);
6 end loop;
7 end;
8 /
1
2
3
4
5
SQL> declare
2 i number(10);
3 ans number(20):=0;
4 begin
5 for i in 1..10 loop
6 ans:= ans + i;
7 end loop;
8 dbms_output.put_line(ans);
9 end;
10 /
55
PL/SQL procedure successfully completed.
K. K. WAGH POLYTECHNIC, NASHIK
Program to print the numbers from 10 to 1 in reverse
SQL> declare
2 i number(10);
3 begin
4 for i in reverse 1..10 loop
5 dbms_output.put_line(i);
6 end loop;
7 end;
8 /
10
9
8
7
6
5
4
3
2
K. K. WAGH POLYTECHNIC, NASHIK
1
Sequential Control
•Changes the flow of control within the block.
•Entry point is markes using tag
<<userdefined name>
• GOTO Statement is used to jump to this label.
Syntax :
Begin
Statements;
GOTO Lablel_1;
Statements;
<<Lablel_1>>
Statements;
END;
DECLARE
Declaration section
Begin
executable statement;
EXCEPTION
END;
K. K. WAGH POLYTECHNIC, NASHIK
Predefined Exception Or System Defined Exceptions
SQL> declare
2 a number;
3 begin
4 a:=10/0;
5 end;
6 /
declare
*
ERROR at line 1:
ORA-01476: divisor is equal to zero
ORA-06512: at line 4
SQL> declare
2 a number;
3 b number;
4 c number;
5 begin
6 a:=&a;
7 b:=&b;
8 c:= a/b;
9 dbms_output.put_line(c);
10 exception
11 when zero_divide then
12 dbms_output.put_line('divisor is zero');
13 end;
14 /
K. K. WAGH POLYTECHNIC, NASHIK
Enter value for a: 6
old 6: a:=&a;
new 6: a:=6;
divisor is zero
PL/SQL procedure
successfully completed.
K. K. WAGH POLYTECHNIC, NASHIK
SQL> select * from course;
SQL> declare
2 c_id course.cid%type;
3 begin
4 select cid into c_id from course where
sub='VBS';
5 end;
6 /
declare
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 4
K. K. WAGH POLYTECHNIC, NASHIK
NO_DATA_FOUND Exception
SQL> declare
2 c_id course.cid%type;
3 begin
4 select cid into c_id from course where sub='VBS';
5 Exception
6 when NO_DATA_FOUND then
7dbms_output.put_line(' Data is No Found by Select Query');
8 end;
9 /
SQL> declare
2 c_id course.cid%type;
3 begin
4 select cid into c_id from course;
5 end;
6 /
declare
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested
number of rows
ORA-06512: at line 4
K. K. WAGH POLYTECHNIC, NASHIK
TOO_MANY_ROWS Exception
SQL> declare
2 c_id course.cid%type;
3 begin
4 select cid into c_id from course;
5 exception
6 when TOO_MANY_ROWS then
7 dbms_output.put_line(' Many Rows are Fetched .... ');
8 end;
9 /
DECLARE
<exception_name> exception ;
BEGIN
<SQL Sentences>;
if <condition> Then
Raise <exception_name>
End if;
EXCEPTION
END;
K. K. WAGH POLYTECHNIC, NASHIK
declare
my_exe Exception;
D1 course.duration%type;
begin
select duration into d1 from course where sid=3;
if d1 <40 then
raise my_exe;
end if;
exception
when my_exe then
dbms_output.put_line(' Duration is less than 40');
end;
/
Syntax :
1)Implicit
2) Explicit
NO NAME MARKS
---------- -------------------- ----------
1 akash 85
2 nikita 65
3 puja 75
4 nisha 35
5 manas 50
6 akash 56
7 vijay 85
7 rows selected.
SQL> begin
2 delete from student1 where name='puja';
3 dbms_output.put_line('Number of rows
deleted:='||sql%rowcount);
4 end;
5 /
Number of rows deleted:=1
NO NAME MARKS
---------- -------------------- ----------
1 akash 85
2 nikita 65
4 nisha 35
5 manas 50
6 akash 56
7 vijay 85
6 rows selected.
K. K. WAGH POLYTECHNIC, NASHIK
Implicit cursors with update statement
SQL> begin
2 update student1 set marks=55 where no=4;
3 dbms_output.put_line('Number of rows updated:='||sql%rowcount);
4 end;
5 /
NO NAME MARKS
---------- -------------------- ----------
1 akash 85
2 nikita 65
4 nisha 55
5 manas 50
6 akash 56
7 vijay 85
6 rows selected.
NO NAME MARKS
---------- -------------------- ----------
1 akash 86
2 nikita 66
4 nisha 56
5 manas 51
6 akash 57
7 vijay 86
6 rows selected.
2) Open
Syntax:
Open <Cursor_name>;
Example:
open c1;
4) Close
Syntax:
close <Cursor_name>;
Example:
close c1;
K. K. WAGH POLYTECHNIC, NASHIK
SQL> select * from student1;
NO NAME MARKS
---------- -------------------- ----------
1 akash 85
2 nikita 65
3 puja 75
4 nisha 35
5 manas 50
6 akash 56
7 vijay 85
7 rows selected.
Creating Procedure :
[ IS | AS ]
Variable declaration;
Constant declarations;
Begin
Pl/sql subprogram;
NO NAME MARKS
---------- -------------------- ----------
1 akash 85
2 nikita 65
3 puja 75
4 nisha 35
5 manas 50
6 akash 56
7 vijay 85
65 prakash 95
12 punam 75
7 rows selected.
Procedure created.
Synax:
SQL> create or replace function <Function_name>(Parameter
in Data type ) return Data Type
[IS | AS]
Variable declaration;
Constant declarations;
Begin
Pl/sql subprogram;
return Value ;
end <Funcation_name >;
K. K. WAGH POLYTECHNIC, NASHIK
Program to Find Square .
SQL> create or replace function sqr( no in number ) return number
2 AS
3 begin
4 return no*no;
5 end sqr;
6 /
Function created.
SQL> declare
2 val1 number;
3 begin
4 val1 :=&val1;
5 dbms_output.put_line(‘Square’ || sqr(val1));
6 end;
7 /
Enter value for val1: 4
Square 16 K. K. WAGH POLYTECHNIC, NASHIK
Program to Find area of circle
SQL> declare
2 a number(10);
3 begin
4 a:=area20(2);
5 dbms_output.put_line('area is'||a);
6 end;
7 /
area is13
K. K. WAGH POLYTECHNIC, NASHIK
PL/SQL procedure successfully completed.
Program to Find area of circle
Function created.
SQL> declare
2 r1 number;
3 begin
4 r1:=&r1;
5 dbms_output.put_line('area is'||area13(r1));
6 end ;
7 /
Enter value for r1: 2
old 4: r1:=&r1;
new 4: r1:=2;
area is12.56
Function created.
SQL> declare
2 result number(20);
3 begin
4 result:=add1(10,30);
5 dbms_output.put_line('addition is'||result);
6 end;
7 /
addition is 40
PL/SQL procedure successfully completed.
K. K. WAGH POLYTECHNIC, NASHIK
Write a function which will count the number of rows deleted
SQL> create or replace function del(name1 in varchar2)return number as
2 no number(10);
3 begin
4 delete from mp1 where name=name1;
5 no:=sql%rowcount;
6 return no;
7 end del;
8 /
Function created.
SQL> declare
2 a number;
3 begin
4 a:=del(‘manas');
5 dbms_output.put_line(‘number of rows deleted :’||a);
6 end;
7 /
number of rows deleted :1
K. K. WAGH POLYTECHNIC, NASHIK
PL/SQL procedure successfully completed.
Difference between procedures and functions
1. Security
Procedures and functions can help to enforce data security.
2. Performance
It improves database performance.
Amount of information sent over the network is less.
No compilation is required to execute the code.
3. Memory Allocation
Due to only one copy of procedure and functions need to load ,
the amount of memory used reduces
4. Productivity
By writing procedures and functions redundant coding can be
avoided.
5. Integrity
It increases the integrity of data.
{ BEFORE | AFTER}
[OF col_name]
ON <table_name >
WHEN (condition)
BEGIN
END;
/
K. K. WAGH POLYTECHNIC, NASHIK
CREATE [OR REPLACE ] TRIGGER trigger_name - This
clause creates a trigger with the given name or overwrites an
existing trigger with the same name.
Trigger created.
K. K. WAGH POLYTECHNIC, NASHIK
SQL> insert into student1 values(9,'amin',30);
fail
1 row created.
1 row created.
1 row updated.
1 row updated.
E.g.
Trigger dropped.