0% found this document useful (0 votes)
268 views17 pages

PL-SQL Examples

This document contains examples of PL/SQL blocks, procedures, triggers and cursors. It begins with basic PL/SQL blocks that perform calculations and display output. It then provides examples of blocks using loops, conditions and exceptions. Examples are given for cursors using FOR LOOPS to display records from tables. Triggers are demonstrated that restrict access and generate values. Finally, a procedure is shown that accepts a parameter and works with related tables using a cursor.

Uploaded by

suri2221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
268 views17 pages

PL-SQL Examples

This document contains examples of PL/SQL blocks, procedures, triggers and cursors. It begins with basic PL/SQL blocks that perform calculations and display output. It then provides examples of blocks using loops, conditions and exceptions. Examples are given for cursors using FOR LOOPS to display records from tables. Triggers are demonstrated that restrict access and generate values. Finally, a procedure is shown that accepts a parameter and works with related tables using a cursor.

Uploaded by

suri2221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

ORACLE 9i

Part - III
PL/SQL Blocks
1) Example of a simple Pl/SQL block
declare
a number:=&a;
b number:=&b;
c number;
begin
c:=a+b;
dbms_output.put_line('the sum is'||c);
end;
/
2) Example for LOOP structure which generates a number series
declare
i number:=1;
begin
loop
dbms_output.put_line(i);
i:=i+1;
i i!1" then
e#it;
end i;
end loop;
end;
/
!) Example for "#$LE structure which generates a number series
declare
i number:=1;
begin
$hile i%=1"
loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
/
%) Example for &O' structure which generates a number series
begin
or i in 1..1"
loop
dbms_output.put_line(i);
end loop;
end;
By Subhash Reddy
ORACLE 9i
() PL/SQL block to count no of )owels an* consonants in a gi)en string
declare
name &archar'('"):='&name';
ccnt number:=";
&cnt number:=";
begin
or i in 1.. length(name)
loop
i upper(substr(name(i(1)) in (')'('*'('+'(','('-') then
&cnt:=&cnt+1;
else
ccnt:=ccnt+1;
end i;
end loop;
dbms_output.put_line('&count is'||&cnt);
dbms_output.put_line('ccount is'||ccnt);
end;
/
+) PL/SQL block to print the string in 'E,E'SE
declare
str &archar'(."):='&string';
strn &archar'(.");
begin
or i in re&erse 1..length(str)
loop
strn:=strn||substr(str(i(1);
end loop;
dbms_output.put_line(strn);
end;
/
-) PL/SQL block to fin* the &./0O'$.L of a gi)en number
declare
i number:=#
/ number:=1;
begin
or 0 in 1..i
loop
/:=123;
end loop;
dbms_output.put_line(/);
end;
/
By Subhash Reddy
ORACLE 9i
1) PL/SQL block to print the S0.'S in a triangular form
declare
c &archar(1"):=' ';
begin
or i in 1..4
loop
c:=' ';
or 0 in 1..i
loop
c:=c||'2';
end loop;
dbms_output.put_line(c);
end loop;
end;
/
2) PL/SQL block to con)ert initial of the string to /.P$0.L without using $3$0/.P
declare
name &archar'(14):='&name';
#name &archar'(14);
begin
#name:=upper(substr(name(1(1))||lo$er(substr(name('));
dbms_output.put_line(#name);
end;
/
14) PL/SQL block to up*ate the salar5 of the gi)en emplo5ee number
declare
eno number(5):=&empno;
nsal number(6('):=&sal;
begin
update emp set sal=sal+nsal $here empno=eno;
commit;
dbms_output.put_line(7salar8 is updated or empno: 7||eno);
end;
/
By Subhash Reddy
ORACLE 9i
11) PL/SQL block to up*ate the salar5 of the an emplo5ee on gi)en con*ition
declare
eno number(5):=&eno;
nsal number(6(');
begin
select sal into nsal rom emp $here empno=eno;
i nsal%.""" then
update emp set sal=sal+4"" $here empno=eno;
else
update emp set sal=sal+1""" $here empno=eno;
end i;
commit;
end;
/
12) PL/SQL block to that *eletes recor*s from master an* chil* table
declare
dno number('):=&dno;
begin
delete rom emp $here deptno=dno;
delete rom dept $here deptno=dno;
end;
/
1!) PL/SQL block to insert recor*s into a *atabase table
declare
# dept.deptno9t8pe:=&dno;
8 dept.dname9t8pe:='&dname';
: dept.loc9t8pe:='&loc';
begin
insert into dept &alues(#(8(:);
end;
/
1%) PL/SQL block to that represents the usage of 6 attributes
declare
# emp9ro$t8pe;
8 emp.empno9t8pe:=&eno;
begin
select 2 into # rom emp $here empno=8;
dbms_output.put_line(#.empno||#.ename||#.sal);
end;
/
By Subhash Reddy
ORACLE 9i
Exceptions
1() PL/SQL block toto raise an E7/EP0$O3 when the recor* to search is not foun*
declare
eno emp.empno9t8pe:=&eno;
nsal emp.sal9t8pe;
begin
select sal into nsal rom emp $here empno=eno;
dbms_output.put_line(nsal);
e#ception
$hen no_data_ound then
dbms_output.put_line('no such emplo8ee');
end;
/
1+) PL/SQL block to raise an user *efine* E7/EP0$O3 when commission is null
declare
no_comm e#ception;
eno emp.empno9t8pe:=&eno;
ncomm emp.comm9t8pe;
begin
select comm into ncomm rom emp $here empno=eno;
dbms_output.put_line(ncomm);
i ncomm is null then
raise no_comm;
end i;
e#ception
$hen no_comm then
dbms_output.put_line(';o commision');
end;
/
By Subhash Reddy
ORACLE 9i
Cursors
1-) PL/SQL block using /8'SO' to *ispla5 recor*s from emp table
declare
cursor emp_cur is select 2 rom emp;
emp_rec emp9ro$t8pe;
begin
open emp_cur;
loop
etch emp_cur into emp_rec;
e#it $hen emp_cur9notound;
dbms_output.put_line(emp_rec.empno||emp_rec.ename||emp_rec.sal);
end loop;
close emp_cur;
end;
/
11) PL/SQL block using /8'SO' attributes to *ispla5 emplo5ee recor*s
<eclare
cursor c is select 2 rom emp;
e emp9ro$t8pe;
begin
i c9+s,pen then
dbms_output.put_line('=ursor is )lread8 ,pened');
else
open c;
end i;
loop
etch c into e;
dbms_output.put_line('>r.'||e.ename||' $ho is an '||e.0ob||' dra$s ?s.'||e.sal);
e#it $hen c9;,@A,-;<;
end loop;
dbms_output.put_line(c9?,B=,-;@ ||' ?o$s $ere displa8ed');
i c9+s,pen then
close c;
else
dbms_output.put_line('=ursor is alread8 closed');
end i;
end;
/
By Subhash Reddy
ORACLE 9i
12) PL/SQL block using /8'SO' to *ispla5 the re9uire* highest salar5 from emp
declare
cursor ecur is select distinct(sal) rom emp order b8 sal desc;
nsal emp.sal9t8pe;
i number:=&num;
ctr number:=1;
begin
i ecur9isopen then
close ecur;
end i;
open ecur;
loop
etch ecur into nsal;
i i=ctr then
dbms_output.put_line(nsal);
e#it;
end i;
ctr:=ctr+1;
e#it $hen ecur9notound;
end loop;
close ecur;
end;
/
24) PL/SQL block using /8'SO' &O' loop to *ispla5 emplo5ee *etails
declare
cursor c1 is select 2 rom emp;
begin
or i in c1
loop
dbms_output.put_line(i.ename||i.sal);
end loop;
end;
/
By Subhash Reddy
ORACLE 9i
21) PL/SQL block using /8'SO' &O' loop to *ispla5 *ept an* emp *etails
declare
cursor d is select 2 rom dept;
cursor e(dno number) is select 2 rom emp $here deptno=dno;
begin
or department in d
loop
dbms_output.put_line('<*C)?@>*;@ ;->D*? : ' || department.deptno);
dbms_output.put_line('EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE');
or emplo8ee in e(department.deptno)
loop
dbms_output.put_line('>r.'||emplo8ee.ename||' is $or/ing in department '||
department.dname||
' at '||department.loc||' as '||emplo8ee.0ob);
end loop;
end loop;
end;
22) PL/SQL block using /8'SO' &O' loop to *ispla5 the table name an*
constraints of it
declare
cursor t is select 2 rom tab;
cursor c(tname &archar') is select 2 rom user_constraints $here table_name li/e
upper(tname);
begin
or tables in t
loop
dbms_output.put_line('=onstraints in @able : ' || tables.tname);
dbms_output.put_line('EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE');
or cons in c(tables.tname)
loop
dbms_output.put_line(cons.constraint_name);
end loop;
end loop;
end;
/
By Subhash Reddy
ORACLE 9i
Triggers
2!) PL/SQL block using 0'$::E' to not to up*ate a table before or after office
hours
create or replace trigger trg_secure beore insert on emp
declare
# number;
begin
i to_char(s8sdate('hh'5') not bet$een 1 and '" then
raise_application_error(E'""""('cannot manipulate data in un oicial hrs.F);
elsi to_char(s8sdate(Fhh'5:."F) bet$een 1':." and 1..." then
raise_application_error(E'"""1('cannot manipulate during lunch hrs.');
elsi upper(to_char(s8sdate(Fd8F)) in (7G)@F(G-;F) then
raise_application_error(E'"""'('cannot manipulate data on $ee/ ends.');
end i;
select count(2) into # rom holida8s $here h_date=to_date(s8sdate(FddEmonE88F);
i #!" then
raise_application_error(E'""".('cannot manipulate data on a public holida8.');
end i;
end;
/
2%) PL/SQL block using 0'$::E' to check for salar5 tobe more than (444
create or replace trigger sal_ch/ beore insert on emp or each ro$
begin
i :ne$.sal!4""" then
raise_application_error(E'"""5('Galar8 should be abo&e 4""".');
end i;
end;
/
2() PL/SQL block using 0'$::E' to salar5 with more than ol* salar5
create or replace trigger sal-pdate
beore update on emp or each ro$
begin
i :ne$.sal % :old.sal then
raise_application_error(E'"""4(';e$ salar8 is lesser than ,ld');
end i;
end;
/
2+) PL/SQL block using 0'$::E' not to accept the existing empno ;8ni9ue Empno)
create or relace trigger dup*mpno
beore insert or update on emp
or each ro$
declare
cursor c is select 2 rom emp;
begin
By Subhash Reddy
ORACLE 9i
or i in c
loop
i i.empno=:ne$.empno then
raise_application_error(E'"""H('*mpno alread8 e#ists');
end i;
end loop;
end;
/
2-) PL/SQL block using 0'$::E' to generate auto emplo5ee numbers
create or replace trigger )uto*mpno
beore insert on emp or each ro$
declare
n number;
begin
select n&l(ma#(empno)(6""")+1 into n rom emp;
:ne$.empno:=n;
end;
/
21) PL/SQL block using 0'$::E' allow onl5 the owner to work with table an* onl5
on working *a5s
create or replace trigger emp@rans
beore insert or update or delete on emp
or each ro$
declare
$*nd e#ception;
usr e#ception;
begin
i upper(rtrim(to_char(s8sdate('da8')))='G)@' or
upper(rtrim(to_char(s8sdate('da8')))='G-;' then
raise $*nd;
end i;
i upper(user)%!'G=,@@' then
raise usr;
end i;
e#ception
$hen $*nd then
raise_application_error(E'"""6('@ransactions are not allo$ed at $ee/ ends');
$hen usr then
raise_application_error(E'"""I('@ransactions are allo$ed onl8 b8 Gcott);
end;
/
By Subhash Reddy
ORACLE 9i
22) PL/SQL block using 0'$::E' that will not allow user to work on fri*a5s
create or replace trigger restrict<>J
beore insert or update or delete on emp
or each ro$
declare
& &archar'('");
begin
select upper(rtrim(to_char(s8sdate('da8'))) into & rom dual;
i &='A?+<)K' then
raise_application_error(E'"""L('@ransactions are not allo$ed on A?+<)K');
end i;
end;
!4) )n e#ample o a ro$ trigger ollo$s:
create or replace trigger m8trig'
ater delete or insert or update on emp
or each ro$
begin
i deleting then
insert into emp1 (empno( ename( 0ob( sal( deldate) &alues
(:old.empno( :old.ename( :old.0ob( :old.sal( s8sdate);
elsi inserting then
insert into emp' (empno( ename( 0ob( sal( deldate) &alues
(:ne$.empno( :ne$.ename( :ne$.0ob( :ne$.sal( s8sdate);
elsi updating then
insert into emp. (empno( ename( old_sal( ne$_sal( upddate)
&alues (:old.empno( :old.ename( :old.sal( :ne$.sal( s8sdate);
end i
end;
/
By Subhash Reddy
ORACLE 9i
Procedures
!1) PL/SQL block using P'O/E<8'E that works on *ept an* emp
create or replace procedure <ept*mp(dno number) is
d dept9ro$t8pe;
e emp9ro$t8pe;
cursor c(d number) is select 2 rom emp $here deptno=d;
begin
select 2 into d rom dept $here deptno=dno;
dbms_output.put_line('<epartment : '||d.dname);
dbms_output.put_line('EEEEEEEEEEEEEEEEEEEEEEEEEEE');
or e in c(d.deptno)
loop
dbms_output.put_line(e.ename||' E '||e.0ob);
end loop;
end;
/
!2) PL/SQL block using P'O/E<8'E to fin* the factorial of gi)en number
create or replace procedure Aact(a in number(b out number) is
number(5):=1;
begin
or i in 1..a
loop
:=2i;
end loop;
b:=;
end;
/
!!) PL/SQL block that calls the proce*ure to fin* factorial
declare
# number(5):=&#;
8 number(5);
begin
Aact(#(8);
dbms_output.put_line('Aactorial o ' || # || ' is ' || 8);
end;
/
By Subhash Reddy
ORACLE 9i
!%) PL/SQL block using P'O/E<8'E to work with arithmetic operations
create or replace procedure )rith(a number(b number(c char) is
d number(5);
e# e#ception;
begin
i c='+' then
d:=a+b;
elsi c='E' then
d:=aEb;
elsi c='2' then
d:=a2b;
elsi c='/' then
d:=a/b;
elsi c='9' then
d:=mod(a(b);
else
raise e#;
end i;
dbms_output.put_line(a||' '||c||' '||b||' = '||d);
e#ception
$hen e# then
dbms_output.put_line(' ;ot a Malied ,perator ');
$hen :ero_di&ide then
dbms_output.put_line(' <enominator shouldnot be :ero ');
$hen others then
dbms_output.put_line('GNJ*??,?');
end;
/
By Subhash Reddy
ORACLE 9i
Functions
!() PL/SQL block using &83/0$O3 to fin* the factorial of gi)en number
create or replace unction AunAact(a number) return number is
number(5):=1;
begin
or i in 1..a
loop
:=2i;
end loop;
return ;
end;
/
!+) PL/SQL block that calls the function to fin* factorial
declare
n number('):=&n;
r number(5);
begin
r:=AunAact(n);
dbms_output.put_line('Aactorial o '||n||' is : '||r);
end;
/
By Subhash Reddy
ORACLE 9i
Packages
!-) PL/SQL block for creating Package Specification
create or replace pac/age ban/_pac/ is
minbal e#ception;
cursor c1(no number) is select 2 rom transact $here accno=no;
procedure ne$_acc(name &archar'(oamt number);
unction deposit(no number(amt number) return number;
unction $ithdra$(no number(amt number) return number;
procedure trans(no number(tt8 char(amt number);
procedure close_acc(no number);
procedure details(no number);
end;
/
!1) PL/SQL block for creating Package =o*5
create or replace pac/age bod8 ban/_pac/ is
procedure ne$_acc(name &archar'(oamt number) is
n number;
begin
select n&l(ma#(accno)(")+1 into n rom ban/mast;
i oamt%1""" then
raise minbal;
end i;
insert into ban/mast &alues(n(name(oamt);
dbms_output.put_line(';e$ )ccount or '||name||' is successull8 created');
e#ception
$hen minbal then
dbms_output.put_line('>inimum balance should be ?s.1"""');
end;
unction deposit(no number(amt number) return number is
pb number;
begin
select opamt into pb rom ban/mast $here accno=no;
return pb+amt;
e#ception
$hen no_data_ound then
dbms_output.put_line('+n&alied )ccount ;umber');
end;
unction $ithdra$(no number(amt number) return number is
pb number;
begin
select opamt into pb rom ban/mast $here accno=no;
i pbEamt%'4" then
raise minbal;
end i;
By Subhash Reddy
ORACLE 9i
return pbEamt;
e#ception
$hen no_data_ound then
dbms_output.put_line('+n&alied )ccount ;umber');
$hen minbal then
dbms_output.put_line('+nsuicient Dalance');
end;
procedure trans(no number(tt8 char(amt number) is
bal number;
begin
i tt8 in ('<'('d') then
bal:=deposit(no(amt);
insert into transact &alues(no(tt8(amt(bal);
update ban/mast set opamt=bal $here accno=no;
dbms_output.put_line('<eposited ?s.'||amt||' into )ccount ;o : '||no);
else
bal:=$ithdra$(no(amt);
i (bal!=1""") then
insert into transact &alues(no(tt8(amt(bal);
update ban/mast set opamt=bal $here accno=no;
dbms_output.put_line('Bithdra$n ?s.'||amt||' rom )ccount ;o : '||
no);
else
raise minbal;
end i;
end i;
e#ception
$hen minbal then
dbms_output.put_line('+nsuicient Dalance');
dbms_output.put_line('- need to maintain >in Dalance');
end;
procedure close_acc(no number) is
n ban/mast.name9t8pe;
begin
select name into n rom ban/mast $here accno=no;
delete rom transact $here accno=no;
delete rom ban/mast $here accno=no;
dbms_output.put_line(')ccount o >r.'||n||' is =losed');
end;
procedure details(no number) is
n ban/mast9ro$t8pe;
begin
select 2 into n rom ban/mast $here accno=no;
dbms_output.put_line(')ccount ;o : '||n.accno);
By Subhash Reddy
ORACLE 9i
dbms_output.put_line(';ame : '||n.name);
dbms_output.put_line(',pening Dalance : '||n.opamt);
dbms_output.put_line('EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE');
or i in c1(no)
loop
dbms_output.put_line(i.accno||' '||i.tt8pe||' '||i.tamt||' '||i.cbal);
end loop;
end;
end;
/
By Subhash Reddy

You might also like