# 1)Write a PL/SQL program to perform addition of two numbers

-------------------------------------------------------------------------------SQL> Declare
2 a number(3):=5;
3 b number(3):=6;
4 c number(3);
5 Begin
6 c:=a+b;
7 dbms_output.put_line (‘The value of c is’||c);
8 end;
9 /
OUTPUT:PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> /
The value of c is 11

2a)Write PL/SQL program to print 1 to n numbers using simple loop
--------------------------------------------------------------------------------------SQL> declare
2 i number(3):=1;
3 n number(3):=&n;
4 begin
5 loop
6 dbms_output.put_line(i);
7 i:=i+1;
8 exit when i>10;
9 end loop;
10 end;
11 /
OUTPUT:Enter value for n: 10
old 3: n number(3):=&n;
new 3: n number(3):=10;
PL/SQL procedure successfully completed.
SQL>set serveroutput on
SQL> /
Enter value for n: 10
old 3: n number(3):=&n;
new 3: n number(3):=10;
1
2
3
4
5
6
7
8
9
10

2b)Write PL/SQL program to print 1 to n numbers using WHILE loop
--------------------------------------------------------------------------------------SQL> declare
2 i number(3):=1;
3 n number(3):=&n;
4 begin
5 while i<=n
6 loop
7 dbms_output.put_line(i);
8 i:=i+1;
9 end loop;
10 end;
11 /
OUTPUT:Enter value for n: 10
old 3: n number(3):=&n;
new 3: n number(3):=10;
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.

2c)Write PL/SQL program to print 1 to n numbers using FOR loop --------------------------------------------------------------------------------------SQL> declare 2 i number(3):=1. new 3: n number(3):=10. 1 2 3 4 5 6 7 8 9 10 PL/SQL procedure successfully completed. 4 begin 5 for i IN 1. . 8 end loop.. 10 / OUTPUT:Enter value for n: 10 old 3: n number(3):=&n.put_line(i). 9 end.n 6 loop 7 dbms_output. 3 n number(3):=&n.

put_line(i). 8 end if. 17 end.50 5 loop 6 if ( i mod 2 = 0 ) then 7 dbms_output.put_line('Odd Numbers between 1 and 50').put_line(i).put_line('Even Numbers between 1 and 50'). 18 / OUTPUT:Even Numbers between 1 and 50 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 . 10 dbms_output. 4 for i in 1. 11 for i in 1. 15 end if.3Q) Create a PL/SQL program that prints even and odd numbers between 1 to 50 --------------------------------------------------------------------------------------------------------SQL> declare 2 begin 3 dbms_output.. 9 end loop.. 16 end loop.50 12 loop 13 if ( i mod 2 <>0) then 14 dbms_output.

34 36 38 40 42 44 46 48 50 Odd Numbers between 1 and 50 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 PL/SQL procedure successfully completed. .

15 else 16 dbms_output. 3 i number(3):=0. 12 end loop. new 2: n number(3):=3. 11 i:=i+1. 13 if count1=2 then 14 dbms_output.put_line('It is not prime no'). 10 end if. ----------------------------------------------------------------------------------------------------SQL> declare 2 n number(3):=&n. 18 end.4Q) Create a PL/SQL program that checks whether a given no is Prime or Not. It is prime no PL/SQL procedure successfully completed. 5 begin 6 while i<=n 7 loop 8 if n mod i=0 then 9 count1:=count1+1. new 2: n number(3):=3. SQL> set serveroutput on SQL> / Enter value for n: 3 old 2: n number(3):=&n. 19 / OUTPUT:Enter value for n: 3 old 2: n number(3):=&n. 17 end if.put_line('It is prime no'). 4 count1 number(3):=0. . PL/SQL procedure successfully completed.

17 end loop. 4 begin 5 for i IN 1. 18* end. SQL> / OUTPUT:Enter value for n: 10 old 2: n number(3):=&n.n 6 loop 7 c:=0. 13 end loop.. 3 c number(3). new 2: n number(3):=10. 2 3 5 . 5Q)Write PL/SQL program to print prime numbers between 1-10 ------------------------------------------------------------------------------------SQL> declare 2 n number(3):=&n. 14 if c=2 then 15 dbms_output. new 2: n number(3):=4. SQL> set serveroutput on SQL> / Enter value for n: 10 old 2: n number(3):=&n.. 16 end if.put_line(i). 12 end if. 8 for j IN 1.i 9 loop 10 if i mod j=0 then 11 c:=c+1.SQL> / Enter value for n: 4 old 2: n number(3):=&n. new 2: n number(3):=10. PL/SQL procedure successfully completed. It is not prime no PL/SQL procedure successfully completed.

5 n number(10):=&n. 4 f3 number(3). 13 dbms_output. 17 end loop.put_line(f1). 7 begin 8 dbms_output.put_line(f3). 19 / OUTPUT:SQL>Enter value for n: 20 old 5: n number(10):=&n. 10 while(i<(n-2)) 11 loop 12 f3:=f1+f2.put_line(f2). 15 f2:=f3. new 5: n number(10):=20. 6 i number(10):=0.7 PL/SQL procedure successfully completed. 3 f2 number(3):=1. 0 1 1 2 3 5 8 13 21 34 . 9 dbms_output. 6Q)Write PL/SQL program to print FIBONOCII SEQUENCE from 1-20 -------------------------------------------------------------------------------------------SQL> declare 2 f1 number(3):=0. 14 f1:=f2. 18 end. 16 i:=i+1.

10 dbms_output. new 3: n number(3):=4. 4 Fact number(5):=1.. 5 begin 6 for i IN 1.put_line('Fact of '||n||'='||Fact).n 7 loop 8 fact:=fact*i. 9 end loop. 11* end. 3 n number(3):=&n. .55 89 144 233 377 610 987 7)Write PL/SQL program to find factorial of a given number ------------------------------------------------------------------------------SQL> declare 2 i number(3). Fact of 4=24 PL/SQL procedure successfully completed. 12 / OUTPUT:SQL>Enter value for n: 4 old 3: n number(3):=&n.

mr1. new 4: no std. 3 d number(5).put_line('Avg of marks:'||d). 27 / OUTPUT:Enter value for no: 11 old 4: no std. executable sections and exception handling section (Student marks can be selected from the table and print division and raise the exception if no records found). 9 begin 10 select sno.sname%type.m1.put_line('sum of marks:'||s). ----------------------------------------------------------------------------------------------------------SQL> declare 2 s number(3):=0. 20 else 21 dbms_output. 11 s:=mr1+mr2+mr3.put_line('II division'). 15 case 16 when d>60 then 17 dbms_output. 14 dbms_output. 8 mr2 std.m3 into no.8Q)Create a Pl/SQL program which include declarative sections.sno%type:=&no.m2%type. 22 end case. 18 when d<60 and d>35 then 19 dbms_output. 12 dbms_output.name. 6 mr1 std. . 23 exception 24 when no_data_found then 25 dbms_output.mr3 from std where sno=no. 26 end. 5 name std. 7 mr3 std. 4 no std.sno%type:=&no.put_line('III division').m3%type.put_line('I division'). 13 d:=s/3.m2.sname.sno%type:=11.m1%type.mr2.put_line('No record found').

sno%type:=55. SQL> / Enter value for no: 55 old 4: no std.sno%type:=&no. new 4: no std. . No record found PL/SQL procedure successfully completed.sum of marks:270 Avg of marks:90 I division PL/SQL procedure successfully completed.

area. 11 insert into area values(i. radius.2) CIRCUMFERENCE NUMBER(5. RADIUS AREA CIRCUMFERENCE . 6 begin 7 for i in 1.14.2)). SQL> / OUTPUT:PL/SQL procedure successfully completed. 3 area number(5.area number(5.-------. 13* end. 12 end loop. Table created.circum).9Q)Calculate area and circumference of a circle (radius 1-7) and insert area.---------------------------RADIUS NUMBER(5) AREA NUMBER(5.7 8 loop 9 area:=pi*i*i. 5 pi number(5.2).2):=3.2) SQL> declare 2 i number(5).2).circumference number(5. SQL> select * from area. 10 circum:=2*pi*i..2). SQL> desc area Name Null? Type ----------------------------------------. circumference into new table called area ----------------------------------------------------------------------------------------------------------SQL> create table area(radius number(5). 4 circum number(5.

put_line('After rollback').sno||' '||std_rec. 15 for std_rec in std_cur loop 16 dbms_output.sno||' '||std_rec.68 7 153. 7 savepoint s1.------------1 3.86 43.84 4 50.&m1.56 3 28. 3 cursor std_cur is select *from std.m2||' '||std_rec.5 31.&m3).'&sname'.&m2.14 6.&m3).'&sname'. 19 end.m1||' '||std_rec.m3).m1||' '||std_rec.m2||' '||std_rec.---------.put_line(std_rec. 6 insert into std values(&sno.&m2. 18 commit.sname||' '||std_rec. 13 dbms_output.sname||' '||std_rec. .24 25.&m2. 14 rollback to s1.96 7 rows selected.&m1.m3). 10 for std_rec in std_cur loop 11 dbms_output. 9 dbms_output. 17 end loop. 4 begin 5 insert into std values(&sno.'&sname'.04 37.56 12. 10Q)insert data into student table and use commit.28 2 12.put_line(std_rec.&m3).&m2.26 18.---------.&m3).&m1. 8 insert into std values(&sno. rollback and savepoint in PL/SQL block -----------------------------------------------------------------------------------------------------------SQL> declare 2 std_rec std%rowtype. 20 / OUTPUT:Enter value for sno: 11 Enter value for sname: Robert Enter value for m1: 98 Enter value for m2: 98 Enter value for m3: 78 old 5: insert into std values(&sno.&m1. 12 end loop.'&sname'.4 6 113.put_line('before rollback').12 5 78.

'&sname'.new 5: insert into std values(11.'&sname'.'Andy'. new 8: insert into std values(33. Enter value for sno: 22 Enter value for sname: George Enter value for m1: 89 Enter value for m2: 89 Enter value for m3: 78 old 6: insert into std values(&sno. before rollback 11 Robert 98 98 78 22 George 89 89 78 33 Andy 87 86 85 After rollback 11 Robert 98 98 78 22 George 89 89 78 PL/SQL procedure successfully completed.'George'.78).78).&m2.&m2.98.89.'Robert'.85).&m1. . new 6: insert into std values(22.87.&m1.89.98.&m3).86.&m3). Enter value for sno: 33 Enter value for sname: Andy Enter value for m1: 87 Enter value for m2: 86 Enter value for m3: 85 old 8: insert into std values(&sno.

11Q)Create a Program to find biggest of 2 Nos. 10 end.put_line('The value of a is bigger:'||a). 4 begin 5 if a>b then 6 dbms_output. Enter value for b: 15 old 3: b number(5):=&b. 7 else 8 dbms_output. 11 / OUTPUT:Enter value for a: 10 old 2: a number(5):=&a.put_line('The value of b is bigger:'||b). -------------------------------------------------------------------------------SQL> declare 2 a number(5):=&a. The value of b is bigger:15 PL/SQL procedure successfully completed. new 3: b number(5):=15. . new 2: a number(5):=10. 9 end if. 3 b number(5):=&b.

12Q)Create a Program to find biggest of 3 Nos. 14 end . new 3: b number:=15. 13 end if. . 10 else 11 dbms_output. 4 c number:=&c. Enter value for b: 15 old 3: b number:=&b. b is bigger15 PL/SQL procedure successfully completed. 5 begin 6 if a>b and a>c then 7 dbms_output. new 4: c number:=14. new 2: a number:=10.put_line('a is bigger'||a). 3 b number:=&b. 12 end if. Enter value for c: 14 old 4: c number:=&c.put_line('c is bigger'||c). 8 else if b>c then 9 dbms_output.put_line('b is bigger'||b). -------------------------------------------------------------------------------SQL> declare 2 a number:=&a. 15 / OUTPUT:Enter value for a: 10 old 2: a number:=&a.

12 end.13Q)Create a PL/SQL Program which uses case. SQL> / Enter value for a: 2 old 2: a number(3):=&a.put_line('U r in default case'). new 2: a number(3):=2. 7 when a=2 then 8 dbms_output.put_line(1). 13 / OUTPUT:Enter value for a: 1 old 2: a number(3):=&a. 3 begin 4 case 5 when a=1 then 6 dbms_output. -------------------------------------------------------------------------------SQL> declare 2 a number(3):=&a. 2 . 11 end case. new 2: a number(3):=1. 1 PL/SQL procedure successfully completed.put_line(2). 9 else 10 dbms_output.

14Q)Create a Program which uses user defined exceptions . new 2: a number(3):=3. U r in default case PL/SQL procedure successfully completed. SQL> / Enter value for a: 3 old 2: a number(3):=&a. 9 end. 3 begin 4 raise invalid_deptid. 5 dbms_output. 6 exception 7 when invalid_deptid then 8 dbms_output. . -------------------------------------------------------------------------------SQL> declare 2 invalid_deptid exception.PL/SQL procedure successfully completed.put_line('Invalid dept id').put_line('U have entered correct deptid'). 10 / OUTPUT:Invalid dept id PL/SQL procedure successfully completed.

3 name varchar2(20):=&name. 7 exception 8 when no_data_found then 9 RAISE_APPLICATION_ERROR(-20001.15Q)Create a Program which uses RAISE APPLICATION ERROR. 4 begin 5 select sno into n from student where sname=name. ORA-20001: No such student . 6 dbms_output. new 3: name varchar2(20):='k'.'No such student'). 11 / OUTPUT:Enter value for name: 'k' old 3: name varchar2(20):=&name. 10 end.put_line(n). ---------------------------------------------------------------------------------------SQL> declare 2 n number(3).

.16Q)Create a Program on NO_DATA exception. 6 dbms_output.put_line(n). 4 begin 5 select sno into n from student where sname=name. new 3: name varchar2(20):='k'. 3 name varchar2(20):='&name'. 10 end.put_line('No record found'). No record found PL/SQL procedure successfully completed. 11 / OUTPUT:Enter value for name: 'k' old 3: name varchar2(20):=&name. 7 exception 8 when no_data_found then 9 dbms_output. -------------------------------------------------------------------------------SQL> declare 2 n number(3).

10* end. 11 / OUTPUT:Enter value for name: d old 3: name varchar2(10):='&name'. .put_line(no). 4 begin 5 select sno into no from student where sname=name. 6 dbms_output. 3 name varchar2(10):='&name'. SQL> / Enter value for name: a old 3: name varchar2(10):='&name'. new 3: name varchar2(10):='d'. -------------------------------------------------------------------------------SQL> declare 2 no number(3). new 3: name varchar2(10):='a'.put_line('Many records found with same name'). 7 exception 8 when too_many_rows then 9 dbms_output. 5 PL/SQL procedure successfully completed.17Q)Create a Program TOO_MANY_ROWS exception.

18Q)Create a Program ZERO_DIVIDE exception. . 5 begin 6 c:=a/b.put_line('c'||c). SQL> / OUTPUT:Cant Divide By Zero PL/SQL procedure successfully completed. 4 c number. 7 dbms_output.Many records found with same name PL/SQL procedure successfully completed. 11* end. -------------------------------------------------------------------------------SQL> declare 2 a number:=5.put_line('Cant Divide By Zero'). 3 b number:=0. 8 exception 9 when zero_divide then 10 dbms_output.

put_line(c).OUT and IN OUT parameters --------------------------------------------------------------------------------------------------------SQL> create or replace procedure pr(a in number.b out number) is 2 c number:=1.19Q)Create a Procedure to find factorial of a given no using IN. 10 / Procedure created. 7 / OUTPUT:- . 9 end. 8 b:=c. 7 end loop. SQL> declare 2 c number. 6 end. 3 begin 4 pr(5.a 5 loop 6 c:=c*i.c).. 3 begin 4 for i in 1. 5 dbms_output.

SQB(3) ---------9 . 7 end. 20Q)Create a function which finds the SQUARE of a Given No. ---------------------------------------------------------------------------------SQL> create function sqb(a in number) 2 return number is 3 b number. 8 / OUTPUT:Function created. 6 return b.120 PL/SQL procedure successfully completed. SQL> select sqb(3) from dual. 4 begin 5 b:=a*a.

9 / OUTPUT:25 720 PL/SQL procedure successfully completed. --------------------------------------------------------------------------------------------------SQL> declare 2 c number.c).21Q)Create a PL/SQL Program which invokes above procedure and function.put_line(c).put_line(c). 8 end. 3 begin 4 c:=sqb(5). . 6 pr(6. 7 dbms_output. 5 dbms_output.

b in number)return number is 3 c number. SQL> create or replace package body p1 is 2 function f1(a in number.22Q)Write a package specification and package body which consists procedure and function. 6 return c. 4 begin 5 c:=a+b. 12 end p1. 4 end p1.z out number) is 9 begin 10 z:=x*y.y in number. 5 / Package created.b in number)return number. 8 procedure pp(x in number. 3 procedure pp(x in number. .z out number). --------------------------------------------------------------------------------------------------------SQL> create or replace package p1 is 2 function f1(a in number.y in number. 7 end f1. 11 end pp.

5. P1.smarks). 10 11 12 13 exit when c1%notfound.sno||' '||std_rec.b). 4 std_rec std%rowtype. 3 begin 4 b:=p1.put_line(b). 5 begin 6 open c1. --------------------------------------------------------------------------------------------------------SQL> declare 2 cursor 3 c1 is select * from std.F1(5.13 / Package body created. 7 dbms_output.f1(5. 5 dbms_output. end. 9 dbms_output.f1(3. end loop.6) from dual. 8 end. 6 p1.6) ---------11 SQL> declare 2 b number.4). 7 loop 8 fetch c1 into std_rec. 9 / OUTPUT:7 20 PL/SQL procedure successfully completed. / OUTPUT:585 George 90 586 Jenny 95 587 Andy 98 584 Steven 94 . 23Q)Write a Program which uses cursors to get student info and displays the same.sname||' '||std_rec.put_line(b).pp(4.put_line(std_rec. SQL> select p1.

ename||' '||emp_rec. 8 end loop.esal from emp where esal>50000.esal). . 10 / OUTPUT:ray 55000 ria 52000 PL/SQL procedure successfully completed. 24Q)Create a PL/SQL program which uses a cursor to get emp info then print emp info if the salary>50000. --------------------------------------------------------------------------------------------------------SQL> declare 2 emp_rec emp%rowtype. 9 end.ename.PL/SQL procedure successfully completed. 3 cursor emp_cur is 4 select eno. 5 begin 6 for emp_rec in emp_cur loop 7 dbms_output.put_line(emp_rec.eno||' '||emp_rec.

8 end.25Q)Create a PL/SQL Program which uses cursor in FOR loop.sno). 4 begin 5 for c1_rec in c1 loop 6 dbms_output. . 7 end loop.put_line(c1_rec. 9 / OUTPUT:581 582 583 584 585 PL/SQL procedure successfully completed. --------------------------------------------------------------------------------SQL> declare 2 cursor c1 is select * from std. 3 c1_rec std%rowtype.

26Q)Create a PL/SQL Program which use FOR_UPDATE cursor and WHERE_CURRENT_OF clause. 5 begin 6 for mgr-rec in mgr-wr loop 7 update emp set esal=esal*0. eno ename esal . 9 commit.esal from emp where eno=2 4 for update. 8 end loop. --------------------------------------------------------------------------------------------------------SQL>declare 2 cursor mgr-cur is 3 select ename. 10 end. 11 / OUTPUT:PL/SQL procedure successfully completed set server output on / select * from emp.1 where current of mgr-wr.

sname.marks2. 7 :new.87.marks3) values(''. OUTPUT:- . statement-level triggers and BEFORE and AFTER triggers.marks1.sno%type. 1 row created. 8 end. 9 / SQL> Trigger created.67.1 abc 200 2 bcd 3 3 fgh 453 45 fhdg 563 7 ret 543 8 ded 233 27Q)Create a Program an row-level.nextval into sid from dual. ----------------------------------------------------------------------------------------------------------SQL> create or replace trigger t1 before insert on student 2 for each row 3 declare 4 sid student. 5 begin 6 select seq.76).'k'.sno:=sid. SQL> insert into student(sno.

'Delete'.-------------------.operation varchar2(30).modifieddate date).SQL> select * from student. AFTER TRIGGERS:SQL> create table del(tablename varchar2(20).---------1a 75 65 70 210 2b 65 65 60 190 . SNO SNAME MARKS1 MARKS2 MARKS3 TOTAL ---------. 5 end.---------.---------.---------1a 75 65 70 210 2b 65 65 60 190 3c 70 60 60 190 4a 65 66 70 201 5d 75 67 72 214 6e 80 67 75 222 7k 67 87 76 7 rows selected.---------. SNO SNAME MARKS1 MARKS2 MARKS3 TOTAL ---------.-------------------. SQL> select * from student.---------.SYSDATE). Table created. SQL> create or replace trigger std_trig after delete on student 2 declare 3 begin 4 insert into del values('Student'.---------. 6 / Trigger created.---------.

--------Student Delete 25-APR-10 28Q) Program To demonstrate Nth Multiplication Table ------------------------------------------------------------------------SQL>declare 2 n number(3):=&n. 2 rows deleted.. TABLENAME OPERATION MODIFIEDDATE -------------------.3c 4a 5d 6e 7k 70 65 75 80 67 60 66 67 67 87 60 70 72 75 76 190 201 214 222 7 rows selected. 3 begin 4 for i in 1. 9 / OUTPUT:SQL>enter value for n:2 n number(3):=&n.10 5 loop 6 dbms-output-line(n||'*'||i||'='||n*i).. SQL> select * from del. 7 end loop.--------------------------. SQL> delete from student where sname='a'. 8 end. 2*1=2 2*2=4 2*3=6 .

sname. SQL> select * from stud.total from student.2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20 PL/SQL procedure successfully completed.total) as select sno. 29Q)Creation of VIEWS.---------1a 210 2b 190 3c 190 4a 201 5d 214 6e 222 . --------------------------------SQL> create view stud(sno.---------------.sname. SNO SNAME TOTAL -------. View created.

6 rows selected. .