Вы находитесь на странице: 1из 25

INTRODUCTION TO PL/SQL

PL/SQL stands for PROCEDURAL Language Extensions to SQL.


PL/SQL extends SQL by adding programming structures and subroutines available
in any high level language.
PL/SQL can be used for both server-side and Client side Development.
PL/SQL has syntax and rules that determine how programming statements work
together.
PL/SQL is not a stand alone Programming Language.
PL/SQL is a part of the ORACLE RDBMS and hence can reside in two
environments,the CLIENT and the SERVER.
Any MODULE that is developed using PL/SQL can be moved easily between
SERVER SIDE and CLIENT SIDE applications.
Either in CLIENT/SERVER environments any PL/SQL Block or the PL/SQL Engine
processes Subroutine.
PL/SQL Engine is a special component that processes and executes any PL/SQL
statements and sends any SQL statement to the SQL statement processor.
The SQL statement processes are always located on the ORACLE SERVER.

As per the necessity the PL/SQL Engine can be located either at

SERVER
CLIENT

When PL/SQL Engine is locted upon the SERVER,the whole PL/SQL block is
passed to the PL/SQL Engine on the ORACLE SERVER.
When the PL/SQL Engine is located upon the CLIENT,the PL/SQL processing is
done on the CLIENT SIDE.All SQL statenents that are embedded within the PL/SQL
block,are sent to the ORACLE SERVER for further processing.

If the PL/SQL block does not contain any SQL statements,the entire block is
executed on the CLIENT SIDE.

PL/SQL BLOCK

DECLARE
--Declarations of memory variables,constants,cursors etc.,in PL/SQL
BEGIN
--SQL executable statements
--PL/SQL executable statements
EXCEPTION
/*SQL or PL/SQL code to handle errors that may arise during the execution of the
code block between BEGIN and EXCEPTION section
END;

pg. 1
Dept of ECM
SYNTAX's of CONTROL STATEMENTS in PL/SQL
1. BRANCHING
2. SELECTION
3. LOOPING

BRANCHING STATEMENTS

1.Simple IF
2.ELSIF
3.ELSE IF

SIMPLE IF

IF condition THEN
statement1;
statement2;
END IF;

IF-THEN-ELSE STATEMENT

IF condition THEN
statement1;
ELSE
statement2;
END IF;

ELSIF STATEMENTS

IF condition1 THEN
statement1;
ELSIF condition2 THEN
statement2;
ELSIF condition3 THEN
statement3;
ELSE
statementn;
END IF;

pg. 2
Dept of ECM
NESTED IF

IF condition THEN
statement1;
ELSE
IF condition THEN
statement2;
ELSE
statement3;
END IF;
END IF;
ELSE
statement3;
END IF;

SELECTION IN PL/SQL

SIMPLE CASE

CASE SELECTOR
WHEN Expr1 THEN statement1;
WHEN Expr2 THEN statement2;
:
:
:
ELSE
statementn;

END CASE;

SEARCHED CASE

CASE
WHEN searchcondition1 THEN statement1;
WHEN searchcondition2 THEN statement2;
:
:

pg. 3
Dept of ECM
:
ELSE
statementn;
END CASE;

ITERATIONS IN PL/SQL

SIMPLE LOOP

LOOP
statement1;
EXIT [ WHEN Condition];
END LOOP;

WHILE LOOP

WHILE condition LOOP


statement1;
statement2;
END LOOP;

FOR LOOP

FOR counter IN [REVERSE]


LowerBound..UpperBound
LOOP
statement1;
statement2;
END LOOP;

pg. 4
Dept of ECM
WRITE A PL/SQL PROGRAM TO SWAP TWO NUMBERS
WITH OUT TAKING THIRD VARIABLE

declare
a number(10);
b number(10);
begin
a:=&a;
b:=&b;
dbms_output.put_line('THE PREV VALUES OF A AND B WERE');
dbms_output.put_line(a);
dbms_output.put_line(b);
a:=a+b;
b:=a-b;
a:=a-b;
dbms_output.put_line('THE VALUES OF A AND B ARE');
dbms_output.put_line(a);
dbms_output.put_line(b);
end;

OUTPUT:

SQL> @ SWAPPING.SQL
17 /
Enter value for a: 5
old 5: a:=&a;
new 5: a:=5;
Enter value for b: 3
old 6: b:=&b;
new 6: b:=3;
THE PREV VALUES OF A AND B WERE
5
3
THE VALUES OF A AND B ARE
3
5

PL/SQL procedure successfully completed.

pg. 5
Dept of ECM
WRITE A PL/SQL PROGRAM TO SWAP TWO NUMBERS BY TAKING THIRD
VARIABLE

declare
a number(10);
b number(10);
c number(10);
begin
dbms_output.put_line('THE PREV VALUES OF A AND B WERE');
dbms_output.put_line(a);
dbms_output.put_line(b);
a:=&a;
b:=&b;
c:=a;
a:=b;
b:=c;
dbms_output.put_line('THE VALUES OF A AND B ARE');
dbms_output.put_line(a);
dbms_output.put_line(b);
end;

OUTPUT:

SQL> @ SWAPPING2.SQL
19 /
Enter value for a: 5
old 6: a:=&a;
new 6: a:=5;
Enter value for b: 3
old 7: b:=&b;
new 7: b:=3;
THE PREV VALUES OF A AND B WERE
5
3
THE VALUES OF A AND B ARE
3
5

PL/SQL procedure successfully completed.

WRITE A PL/SQL PROGRAM TO FIND THE LARGEST OF TWO NUMBERS

pg. 6
Dept of ECM
declare
a number;
b number;
begin
a:=&a;
b:=&b;
if a=b then
dbms_output.put_line('BOTH ARE EQUAL');
elsif a>b then
dbms_output.put_line('A IS GREATER');
else
dbms_output.put_line('B IS GREATER');
end if;
end;

OUTPUT:

SQL> @ GREATESTOF2.sql
13 /
Enter value for a: 5
old 5: a:=&a;
new 5: a:=5;
Enter value for b: 2
old 6: b:=&b;
new 6: b:=2;
A IS GREATER

PL/SQL procedure successfully completed.

WRITE A PL/SQL PROGRAM TO FIND THE LARGEST OF THREE NUMBERS

declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=&c;

pg. 7
Dept of ECM
if a=b and b=c and c=a then
dbms_output.put_line('ALL ARE EQUAL');
elsif a>b and a>c then
dbms_output.put_line('A IS GREATER');
elsif b>c then
dbms_output.put_line('B IS GREATER');
else
dbms_output.put_line('C IS GREATER');
end if;
end;

OUTPUT:

SQL> @ GREATESTOF3.sql
17 /
Enter value for a: 8
old 6: a:=&a;
new 6: a:=8;
Enter value for b: 9
old 7: b:=&b;
new 7: b:=9;
Enter value for c: 7
old 8: c:=&c;
new 8: c:=7;
B IS GREATER

PL/SQL procedure successfully completed.

WRITE A PL/SQL PROGRAM TO FIND THE TOTAL AND AVERAGE OF 6


SUBJECTS AND DISPLAY THE GRADE

declare
java number(10);
dbms number(10);
co number(10);
se number(10);
es number(10);
ppl number(10);
total number(10);

pg. 8
Dept of ECM
avgs number(10);
per number(10);
begin
dbms_output.put_line('ENTER THE MARKS');
java:=&java;
dbms:=&dbms;
co:=&co;
se:=&se;
es:=&es;
ppl:=&ppl;
total:=(java+dbms+co+se+es+ppl);
per:=(total/600)*100;
if java<40 or dbms<40 or co<40 or se<40 or es<40 or ppl<40 then
dbms_output.put_line('FAIL');
if per>75 then
dbms_output.put_line('GRADE A');
elsif per>65 and per<75 then
dbms_output.put_line('GRADE B');
elsif per>55 and per<65 then
dbms_output.put_line('GRADE C');
else
dbms_output.put_line('INVALID INPUT');
end if;
dbms_output.put_line('PERCENTAGE IS '||per);
dbms_output.put_line('TOTAL IS '||total);
end;

OUTPUT:

SQL> @ GRADE.sql
31 /
Enter value for java: 80
old 12: java:=&java;
new 12: java:=80;
Enter value for dbms: 70
old 13: dbms:=&dbms;
new 13: dbms:=70;
Enter value for co: 89
old 14: co:=&co;
new 14: co:=89;
Enter value for se: 72
old 15: se:=&se;
new 15: se:=72;
Enter value for es: 76
old 16: es:=&es;

pg. 9
Dept of ECM
new 16: es:=76;
Enter value for ppl: 71
old 17: ppl:=&ppl;
new 17: ppl:=71;
GRADE A
PERCENTAGE IS 76
TOTAL IS 458
PL/SQL procedure successfully completed.

WRITE A PL/SQL PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS


AN ARMSTRONG NUMBER OR NOT

declare
a number;
t number;
arm number;
d number;
begin
a:=&a;
t:=a;
arm:=0;
while t>0
loop
d:=mod(t,10);
arm:=arm+power(d,3);
t:=trunc(t/10);
end loop;
if arm=a then
dbms_output.put_line('given no is an armstrong no'|| a);
else
dbms_output.put_line('given no is not an armstrong no');
end if;
end;

OUTPUT:

pg. 10
Dept of ECM
SQL> @ ARMSTRONGNUM.sql
Enter value for a: 407
old 7: a:=&a;
new 7: a:=407;
given no is an armstrong no407

PL/SQL procedure successfully completed.

SQL> /
Enter value for a: 406
old 7: a:=&a;
new 7: a:=406;
given no is not an armstrong no

PL/SQL procedure successfully completed.

WRITE A PL/SQL PROGRAM TO FIND THE SUM OF DIGITS IN A GIVEN


NUMBER

declare
a number;
d number:=0;
sum1 number:=0;
begin
a:=&a;
while a>0
loop
d:=mod(a,10);
sum1:=sum1+d;
a:=trunc(a/10);
end loop;
dbms_output.put_line('sum is'|| sum1);
end;

OUTPUT:

SQL> @ SUMOFDIGITS.sql
16 /
Enter value for a: 564
old 7: a:=&a;
new 7: a:=564;
sum is15

pg. 11
Dept of ECM
PL/SQL procedure successfully completed.

WRITE A PL/SQL PROGRAM TO DISPLAY THE NUMBER IN REVERSE


ORDER

declare
a number;
rev number;
d number;
begin
a:=&a;
rev:=0;
while a>0
loop
d:=mod(a,10);
rev:=(rev*10)+d;
a:=trunc(a/10);
end loop;
dbms_output.put_line('no is'|| rev);
end;

OUTPUT:

SQL> @ REVERSE2.sql
16 /
Enter value for a: 536
old 6: a:=&a;
new 6: a:=536;
no is635

PL/SQL procedure successfully completed.

pg. 12
Dept of ECM
WRITE A PL/SQL PROGRAM TO DISPLAY NUMBER IN REVERSE ORDER
USING STRING FUNCTION

declare
gn varchar2(5):=4567;
sl number(2);
rv varchar2(5);
begin
sl:=length(gn);
for i in reverse 1..sl
loop
rv:=rv||substr(gn,i,1);
end loop;
dbms_output.put_line('given no r is'||gn);
dbms_output.put_line('given no in reverse order is'||rv);
end;

OUTPUT:

SQL> @ REVERSE.sql
14 /
given no r is4567
given no in reverse order is7654

PL/SQL procedure successfully completed.

pg. 13
Dept of ECM
WRITE A PL/SQL PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS
PRIME OR NOT

declare
a number;
c number:=0;
i number;
begin
a:=&a;
for i in 1..a
loop
if mod(a,i)=0 then
c:=c+1;
end if;
end loop;
if c=2 then
dbms_output.put_line(a ||'is a prime number');
else
dbms_output.put_line(a ||'is not a prime number');
end if;
end;

OUTPUT:

SQL> @ PRIME.SQL
19 /
Enter value for a: 11
old 6: a:=&a;
new 6: a:=11;
11is a prime number

PL/SQL procedure successfully completed.

pg. 14
Dept of ECM
WRITE A PL/SQL PROGRAM TO FIND THE FACTORIAL OF A GIVEN
NUMBER

declare
n number;
f number:=1;
begin
n:=&n;
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line('the factorial is'|| f);
end;

OUTPUT:

SQL> @ FACTORIAL.sql
12 /
Enter value for n: 5
old 5: n:=&n;
new 5: n:=5;
the factorial is120

PL/SQL procedure successfully completed.

pg. 15
Dept of ECM
WRITE A PL/SQL PROGRAM TO GENERATE FIBONACCI SERIES

declare
a number;
b number;
c number;
n number;
i number;
begin
n:=&n;
a:=0;
b:=1;
dbms_output.put_line(a);
dbms_output.put_line(b);
for i in 1..n-2
loop
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
end loop;
end;

OUTPUT:

SQL> @ FIBONACCI.sql
21 /
Enter value for n: 5
old 8: n:=&n;
new 8: n:=5;
0
1
1
2
3

PL/SQL procedure successfully completed.

pg. 16
Dept of ECM
WRITE A PL/SQL CODE BLOCK TO CALCULATE THE AREA OF A CIRCLE
FOR A VALUE OF RADIUS VARYING FROM 3 TO 7.
STORE THE RADIUS AND THE CORRESPONDING VALUES OF
CALCULATED AREA IN AN EMPTY TABLE NAMED AREAS ,CONSISTING
OF TWO COLUMNS RADIUS & AREA

TABLE NAME:AREAS

RADIUS AREA

SQL> create table areas(radius number(10),area number(6,2));

Table created.
--PROGRAM
declare
pi constant number(4,2):=3.14;
radius number(5):=3;
area number(6,2);
begin
while radius<7
loop
area:=pi*power(radius,2);
insert into areas values(radius,area);
radius:=radius+1;
end loop;
end;

OUTPUT:
SQL> @ AREAOFCIRCLE.SQL
13 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM AREAS;
RADIUS AREA
---------- ----------
3 28.26
4 50.24
5 78.5
6 113.04

pg. 17
Dept of ECM
WRITE A PL/SQL CODE BLOCK THAT WILL ACCEPT AN ACCOUNT
NUMBER FROM THE USER,CHECK IF THE USERS BALANCE IS LESS THAN
MINIMUM BALANCE,ONLY THEN DEDUCT RS.100/- FROM THE
BALANCE.THIS PROCESS IS FIRED ON THE ACCT TABLE.

SQL> create table acct(name varchar2(10),cur_bal number(10),acctno number(6,2));

SQL> insert into stud values('&sname',&rollno,&marks);

SQL> select * from acct;

ACCTNO NAME CUR_BAL


---------- ---------- ----------
777 sirius 10000
765 john 1000
855 sam 500
353 peter 800

--PROGRAM

declare
mano number(5);
mcb number(6,2);
minibal constant number(7,2):=1000.00;
fine number(6,2):=100.00;
begin
mano:=&mano;
select cur_bal into mcb from acct where acctno=mano;
if mcb<minibal then
update acct set cur_bal=cur_bal-fine where acctno=mano;
end if;
end;

OUTPUT:

SQL> @ BANKACC.sql
13 /
Enter value for mano: 855
old 7: mano:=&mano;
new 7: mano:=855;

PL/SQL procedure successfully completed.

pg. 18
Dept of ECM
SQL> select * from acct;

ACCTNO NAME CUR_BAL


---------- ---------- ----------
777 sirius 10000
765 john 1000
855 sam 400
353 peter 800

pg. 19
Dept of ECM
PL/SQL PROGRAMS- FUNCTIONS
CUBE

create or replace function cube(n number) return number


as
c number;
begin
c:=n*n*n;
return c;
end;

output:-
SQL> @cub
/

Function created.

SQL> select cube(6)from dual;

CUBE(6)
------
216

pg. 20
Dept of ECM
FACTORIAL
create or replace function fact(n number)return number
as

fac number:=1;
begin
for i in 1..n
loop
fac:=fac*i;
end loop;
return fac;
end;

output:-

SQL> @func.sql;
12 /

Function created.

SQL> select fact(5) from dual;

FACT(5)
----------
120

pg. 21
Dept of ECM
PROCEDURES
IN-PROCEDURE
create or replace procedure inpro(dno number,depname varchar2,city varchar2)
as
begin
insert into dept (deptno,dname,loc)values(dno,depname,city);
end;

OUTPUT:-
SQL> select * from dept;

DEPTNO DNAME LOC


---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

SQL> @pro1
/

Procedure created.

SQL> exec inpro(50,'MARKETING','HYDERABAD');

PL/SQL procedure successfully completed.

SQL> select * from dept;

DEPTNO DNAME LOC


---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 MARKETING HYDERABAD

pg. 22
Dept of ECM
OUT-PROCEDURE

create or replace procedure outpro(dno number,depname out varchar2,city out


varchar2)
as
begin
select dname,loc into depname,city from dept where deptno=dno;
end;

OUTPUT:
SQL> @proc2
/

Procedure created.

SQL> declare
2 x varchar2(20);
3 y varchar2(30);
4 ch number;
5 begin
6 ch:=&ch;
7 outpro(ch,x,y);
8 dbms_output.put_line(x || ' ' || y);
9 end;
10 /
Enter value for ch: 10
old 6: ch:=&ch;
new 6: ch:=10;
ACCOUNTING NEW YORK

PL/SQL procedure successfully completed.

pg. 23
Dept of ECM
TRIGGERS
BEFORE TRIGGER
create or replace trigger tday before insert or delete or update on emp
declare
we varchar2(10);
begin
we:=to_char(sysdate,'dy');
if we='sat' or we='sun' then
raise_application_error(-20015,'its a weekend');
end if;
end;

output:-

SQL> @trig1
/

Trigger created.

SQL> delete from emp where empno=7902;

ERROR at line 1:
ORA-20015: its a weekend
ORA-06512: at "SCOTT.TDAY", line 6
ORA-04088: error during execution of trigger 'SCOTT.TDAY'

pg. 24
Dept of ECM
AFTER TRIGGER

create or replace trigger ttime after insert or delete or update on emp1 for each row
declare
tt varchar2(5);
begin
tt:=to_char(sysdate,'hh24');
if tt not between 10 and 17 then
raise_application_error(-20010,'not working hours');
end if;
end;

output:-
SQL> @trig2;
10 /

Trigger created.

SQL> update emp1 set empno=7777 where empno=7902;

*
ERROR at line 1:
ORA-20010: not working hours
ORA-06512: at "SCOTT.TTIME", line 6
ORA-04088: error during execution of trigger 'SCOTT.TTIME'

pg. 25
Dept of ECM

Вам также может понравиться