45
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

PL SQL

Embed Size (px)

Citation preview

Page 1: PL SQL

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 onSQL> / The value of c is 11

Page 2: PL SQL

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: 10old 3: n number(3):=&n;new 3: n number(3):=10;PL/SQL procedure successfully completed.

SQL>set serveroutput onSQL> /Enter value for n: 10old 3: n number(3):=&n;new 3: n number(3):=10; 1 2 3 4 5 6 7 8 9 10

Page 3: PL SQL

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: 10old 3: n number(3):=&n;new 3: n number(3):=10; 1 2 3 4 5 6 7 8 910PL/SQL procedure successfully completed.

Page 4: PL SQL

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

OUTPUT:- Enter value for n: 10old 3: n number(3):=&n;new 3: n number(3):=10; 1 2 3 4 5 6 7 8 910PL/SQL procedure successfully completed.

Page 5: PL SQL

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

OUTPUT:-

Even Numbers between 1 and 50 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32

Page 6: PL SQL

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.

Page 7: PL SQL

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

OUTPUT:-

Enter value for n: 3old 2: n number(3):=&n;new 2: n number(3):=3;

PL/SQL procedure successfully completed.

SQL> set serveroutput onSQL> /Enter value for n: 3old 2: n number(3):=&n;new 2: n number(3):=3;It is prime no

PL/SQL procedure successfully completed.

Page 8: PL SQL

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

OUTPUT:-Enter value for n: 10old 2: n number(3):=&n;new 2: n number(3):=10;

PL/SQL procedure successfully completed.

SQL> set serveroutput onSQL> /Enter value for n: 10old 2: n number(3):=&n;new 2: n number(3):=10;235

Page 9: PL SQL

7

PL/SQL procedure successfully completed.

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

OUTPUT:-SQL>Enter value for n: 20old 5: n number(10):=&n;new 5: n number(10):=20;0112358132134

Page 10: PL SQL

5589144233377610987

7)Write PL/SQL program to find factorial of a given number-------------------------------------------------------------------------------SQL> declare 2 i number(3); 3 n number(3):=&n; 4 Fact number(5):=1; 5 begin 6 for i IN 1..n 7 loop 8 fact:=fact*i; 9 end loop; 10 dbms_output.put_line('Fact of '||n||'='||Fact); 11* end; 12 /

OUTPUT:-SQL>Enter value for n: 4old 3: n number(3):=&n;new 3: n number(3):=4;Fact of 4=24

PL/SQL procedure successfully completed.

Page 11: PL SQL

8Q)Create a Pl/SQL program which include declarative sections, 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).-----------------------------------------------------------------------------------------------------------SQL> declare 2 s number(3):=0; 3 d number(5); 4 no std.sno%type:=&no; 5 name std.sname%type; 6 mr1 std.m1%type; 7 mr3 std.m3%type; 8 mr2 std.m2%type; 9 begin 10 select sno,sname,m1,m2,m3 into no,name,mr1,mr2,mr3 from std where sno=no; 11 s:=mr1+mr2+mr3; 12 dbms_output.put_line('sum of marks:'||s); 13 d:=s/3; 14 dbms_output.put_line('Avg of marks:'||d); 15 case 16 when d>60 then 17 dbms_output.put_line('I division'); 18 when d<60 and d>35 then 19 dbms_output.put_line('II division'); 20 else 21 dbms_output.put_line('III division'); 22 end case; 23 exception 24 when no_data_found then 25 dbms_output.put_line('No record found'); 26 end; 27 /

OUTPUT:-Enter value for no: 11old 4: no std.sno%type:=&no;new 4: no std.sno%type:=11;

Page 12: PL SQL

sum of marks:270Avg of marks:90I division

PL/SQL procedure successfully completed.

SQL> /Enter value for no: 55old 4: no std.sno%type:=&no;new 4: no std.sno%type:=55;No record found

PL/SQL procedure successfully completed.

Page 13: PL SQL

9Q)Calculate area and circumference of a circle (radius 1-7) and insert area, radius, circumference into new table called area-----------------------------------------------------------------------------------------------------------

SQL> create table area(radius number(5),area number(5,2),circumference number(5,2));

Table created.

SQL> desc area Name Null? Type ----------------------------------------- -------- ---------------------------- RADIUS NUMBER(5) AREA NUMBER(5,2) CIRCUMFERENCE NUMBER(5,2)

SQL> declare 2 i number(5); 3 area number(5,2); 4 circum number(5,2); 5 pi number(5,2):=3.14; 6 begin 7 for i in 1..7 8 loop 9 area:=pi*i*i; 10 circum:=2*pi*i; 11 insert into area values(i,area,circum); 12 end loop; 13* end;SQL> /OUTPUT:-PL/SQL procedure successfully completed.SQL> select * from area;

RADIUS AREA CIRCUMFERENCE

Page 14: PL SQL

---------- ---------- ------------- 1 3.14 6.28 2 12.56 12.56 3 28.26 18.84 4 50.24 25.12 5 78.5 31.4 6 113.04 37.68 7 153.86 43.96

7 rows selected.10Q)insert data into student table and use commit, rollback and savepoint in PL/SQL block-------------------------------------------------------------------------------------------------------------SQL> declare 2 std_rec std%rowtype; 3 cursor std_cur is select *from std; 4 begin 5 insert into std values(&sno,'&sname',&m1,&m2,&m3); 6 insert into std values(&sno,'&sname',&m1,&m2,&m3); 7 savepoint s1; 8 insert into std values(&sno,'&sname',&m1,&m2,&m3); 9 dbms_output.put_line('before rollback'); 10 for std_rec in std_cur loop 11 dbms_output.put_line(std_rec.sno||' '||std_rec.sname||' '||std_rec.m1||' '||std_rec.m2||' '||std_rec.m3); 12 end loop; 13 dbms_output.put_line('After rollback'); 14 rollback to s1; 15 for std_rec in std_cur loop 16 dbms_output.put_line(std_rec.sno||' '||std_rec.sname||' '||std_rec.m1||' '||std_rec.m2||' '||std_rec.m3); 17 end loop; 18 commit; 19 end; 20 /

OUTPUT:-Enter value for sno: 11Enter value for sname: RobertEnter value for m1: 98Enter value for m2: 98Enter value for m3: 78old 5: insert into std values(&sno,'&sname',&m1,&m2,&m3);

Page 15: PL SQL

new 5: insert into std values(11,'Robert',98,98,78);Enter value for sno: 22Enter value for sname: GeorgeEnter value for m1: 89Enter value for m2: 89Enter value for m3: 78old 6: insert into std values(&sno,'&sname',&m1,&m2,&m3);new 6: insert into std values(22,'George',89,89,78);Enter value for sno: 33Enter value for sname: AndyEnter value for m1: 87Enter value for m2: 86Enter value for m3: 85old 8: insert into std values(&sno,'&sname',&m1,&m2,&m3);new 8: insert into std values(33,'Andy',87,86,85);before rollback11 Robert 98 98 7822 George 89 89 7833 Andy 87 86 85After rollback11 Robert 98 98 7822 George 89 89 78

PL/SQL procedure successfully completed.

Page 16: PL SQL

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

OUTPUT:-Enter value for a: 10old 2: a number(5):=&a;new 2: a number(5):=10;Enter value for b: 15old 3: b number(5):=&b;new 3: b number(5):=15;The value of b is bigger:15

PL/SQL procedure successfully completed.

Page 17: PL SQL

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

OUTPUT:-Enter value for a: 10old 2: a number:=&a;new 2: a number:=10;Enter value for b: 15old 3: b number:=&b;new 3: b number:=15;Enter value for c: 14old 4: c number:=&c;new 4: c number:=14;b is bigger15

PL/SQL procedure successfully completed.

Page 18: PL SQL

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

OUTPUT:-Enter value for a: 1old 2: a number(3):=&a;new 2: a number(3):=1;1

PL/SQL procedure successfully completed.

SQL> /Enter value for a: 2old 2: a number(3):=&a;new 2: a number(3):=2;2

Page 19: PL SQL

PL/SQL procedure successfully completed.

SQL> /Enter value for a: 3old 2: a number(3):=&a;new 2: a number(3):=3;U r in default case

PL/SQL procedure successfully completed.

14Q)Create a Program which uses user defined exceptions .--------------------------------------------------------------------------------SQL> declare 2 invalid_deptid exception; 3 begin 4 raise invalid_deptid; 5 dbms_output.put_line('U have entered correct deptid'); 6 exception 7 when invalid_deptid then 8 dbms_output.put_line('Invalid dept id'); 9 end; 10 /

OUTPUT:-Invalid dept id

PL/SQL procedure successfully completed.

Page 20: PL SQL

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

OUTPUT:-Enter value for name: 'k'old 3: name varchar2(20):=&name;new 3: name varchar2(20):='k';ORA-20001: No such student

Page 21: PL SQL

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

OUTPUT:-Enter value for name: 'k'old 3: name varchar2(20):=&name;new 3: name varchar2(20):='k';No record found

PL/SQL procedure successfully completed.

Page 22: PL SQL

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

OUTPUT:-Enter value for name: dold 3: name varchar2(10):='&name';new 3: name varchar2(10):='d';5

PL/SQL procedure successfully completed.

SQL> /Enter value for name: aold 3: name varchar2(10):='&name';new 3: name varchar2(10):='a';

Page 23: PL SQL

Many records found with same name

PL/SQL procedure successfully completed.

18Q)Create a Program ZERO_DIVIDE exception.--------------------------------------------------------------------------------SQL> declare 2 a number:=5; 3 b number:=0; 4 c number; 5 begin 6 c:=a/b; 7 dbms_output.put_line('c'||c); 8 exception 9 when zero_divide then 10 dbms_output.put_line('Cant Divide By Zero'); 11* end;SQL> /

OUTPUT:-Cant Divide By Zero

PL/SQL procedure successfully completed.

Page 24: PL SQL

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

Procedure created.

SQL> declare 2 c number; 3 begin 4 pr(5,c); 5 dbms_output.put_line(c); 6 end; 7 /

OUTPUT:-

Page 25: PL SQL

120

PL/SQL procedure successfully completed.

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; 4 begin 5 b:=a*a; 6 return b; 7 end; 8 /

OUTPUT:-Function created.

SQL> select sqb(3) from dual;

SQB(3)---------- 9

Page 26: PL SQL

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

OUTPUT:-25720

PL/SQL procedure successfully completed.

Page 27: PL SQL

22Q)Write a package specification and package body which consists procedure and function.---------------------------------------------------------------------------------------------------------SQL> create or replace package p1 is 2 function f1(a in number,b in number)return number; 3 procedure pp(x in number,y in number,z out number); 4 end p1; 5 /

Package created.

SQL> create or replace package body p1 is 2 function f1(a in number,b in number)return number is 3 c number; 4 begin 5 c:=a+b; 6 return c; 7 end f1; 8 procedure pp(x in number,y in number,z out number) is 9 begin 10 z:=x*y; 11 end pp; 12 end p1;

Page 28: PL SQL

13 /

Package body created.SQL> select p1.f1(5,6) from dual;

P1.F1(5,6)---------- 11SQL> declare 2 b number; 3 begin 4 b:=p1.f1(3,4); 5 dbms_output.put_line(b); 6 p1.pp(4,5,b); 7 dbms_output.put_line(b); 8 end; 9 /OUTPUT:-720

PL/SQL procedure successfully completed.23Q)Write a Program which uses cursors to get student info and displays the same. ---------------------------------------------------------------------------------------------------------SQL> declare 2 cursor 3 c1 is select * from std; 4 std_rec std%rowtype; 5 begin 6 open c1; 7 loop 8 fetch c1 into std_rec; 9 dbms_output.put_line(std_rec.sno||' '||std_rec.sname||' '||std_rec.smarks);

10 exit when c1%notfound; 11 end loop; 12 end; 13 /

OUTPUT:-585 George 90586 Jenny 95587 Andy 98584 Steven 94

Page 29: PL SQL

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; 3 cursor emp_cur is 4 select eno,ename,esal from emp where esal>50000; 5 begin 6 for emp_rec in emp_cur loop 7 dbms_output.put_line(emp_rec.eno||' '||emp_rec.ename||' '||emp_rec.esal); 8 end loop; 9 end; 10 /

OUTPUT:- ray 55000 ria 52000

PL/SQL procedure successfully completed.

Page 30: PL SQL

25Q)Create a PL/SQL Program which uses cursor in FOR loop. ---------------------------------------------------------------------------------SQL> declare 2 cursor c1 is select * from std; 3 c1_rec std%rowtype; 4 begin 5 for c1_rec in c1 loop 6 dbms_output.put_line(c1_rec.sno); 7 end loop; 8 end; 9 /

OUTPUT:-581582583584585

PL/SQL procedure successfully completed.

Page 31: PL SQL

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

OUTPUT:-PL/SQL procedure successfully completedset server output on/select * from emp;eno ename esal

Page 32: PL SQL

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, statement-level triggers and BEFORE and AFTER triggers. -----------------------------------------------------------------------------------------------------------

SQL> create or replace trigger t1 before insert on student 2 for each row 3 declare 4 sid student.sno%type; 5 begin 6 select seq.nextval into sid from dual; 7 :new.sno:=sid; 8 end; 9 /SQL> Trigger created.

SQL> insert into student(sno,sname,marks1,marks2,marks3) values('','k',67,87,76);

1 row created.

OUTPUT:-

Page 33: PL SQL

SQL> select * from student;

SNO SNAME MARKS1 MARKS2 MARKS3 TOTAL ---------- -------------------- ---------- ---------- ---------- ---------- 1 a 75 65 70 210 2 b 65 65 60 190 3 c 70 60 60 190 4 a 65 66 70 201 5 d 75 67 72 214 6 e 80 67 75 222 7 k 67 87 76

7 rows selected.

AFTER TRIGGERS:-

SQL> create table del(tablename varchar2(20),operation varchar2(30),modifieddate date);

Table created.

SQL> create or replace trigger std_trig after delete on student 2 declare 3 begin 4 insert into del values('Student','Delete',SYSDATE); 5 end; 6 /Trigger created.

SQL> select * from student;

SNO SNAME MARKS1 MARKS2 MARKS3 TOTAL ---------- -------------------- ---------- ---------- ---------- ---------- 1 a 75 65 70 210 2 b 65 65 60 190

Page 34: PL SQL

3 c 70 60 60 190 4 a 65 66 70 201 5 d 75 67 72 214 6 e 80 67 75 222 7 k 67 87 76

7 rows selected.

SQL> delete from student where sname='a';

2 rows deleted.

SQL> select * from del;

TABLENAME OPERATION MODIFIEDDATE -------------------- --------------------------- --------- Student Delete 25-APR-10

28Q) Program To demonstrate Nth Multiplication Table -------------------------------------------------------------------------SQL>declare2 n number(3):=&n;3 begin4 for i in 1...105 loop6 dbms-output-line(n||'*'||i||'='||n*i);7 end loop;8 end;9 /

OUTPUT:-SQL>enter value for n:2n number(3):=&n;

2*1=22*2=42*3=6

Page 35: PL SQL

2*4=82*5=102*6=122*7=142*8=162*9=182*10=20

PL/SQL procedure successfully completed.

29Q)Creation of VIEWS. ---------------------------------SQL> create view stud(sno,sname,total) as select sno,sname,total from student;

View created.

SQL> select * from stud;

SNO SNAME TOTAL -------- ---------------- ---------- 1 a 210 2 b 190 3 c 190 4 a 201 5 d 214 6 e 222

Page 36: PL SQL

6 rows selected.