Pl SQL Progs

35
pl/sql lab programes SUM OF TWO NUMBERS Aim: To write a pl/sql program to find sum of two numbers. Procedure: Declare i number(3); j number(3); k number(3); Begin i:=&i; j:=&j; k:=i+j; Dbms_output.put_line('the sum of '||i||','||j||'is:'||k); end; / Execution: SQL> @e:\sqlpl\add.sql Enter value for i: 4 old 6: i:=&i; new 6: i:=4; Enter value for j: 4 old 7: j:=&j; new 7: j:=4; the sum of 4,4 is:8 PL/SQL procedure successfully completed. Conclusion: A pl/sql program is successfully executed for finding sum of two numbers.

description

PL/SQL Programs

Transcript of Pl SQL Progs

Page 1: Pl SQL Progs

pl/sql lab programes

SUM OF TWO NUMBERS

Aim: To write a pl/sql program to find sum of two numbers.

Procedure:

Declare i number(3); j number(3); k number(3); Begin i:=&i; j:=&j; k:=i+j; Dbms_output.put_line('the sum of '||i||','||j||'is:'||k); end; /

Execution:

SQL> @e:\sqlpl\add.sqlEnter value for i: 4old 6: i:=&i;new 6: i:=4;Enter value for j: 4old 7: j:=&j;new 7: j:=4;the sum of 4,4 is:8

PL/SQL procedure successfully completed.

Conclusion: A pl/sql program is successfully executed for finding sum of two numbers.

Page 2: Pl SQL Progs

MULTIPLES OF 5

Aim: To write a pl/sql program for finding Multiples of 5.

Procedure:

declare i number(3):=5; n number(3):=&n;Begin Dbms_output.put_line('the multiples of 5 are:'); while i<=n loop dbms_output.put_line(i); i:=i+5; end loop; end; /

Output:

SQL> @e:\sqlpl\2.sqlEnter value for n: 20old 3: n number(3):=&n;new 3: n number(3):=20;the multiplus of 5 are:5101520

PL/SQL procedure successfully completed.

Conclusion: A pl/sql program is successfully executed for finding Multiples of 5.

MULTIPLICATION TABLES

Aim: To write a pl/sql program for display the Multiplication Tables up to given number.

Page 3: Pl SQL Progs

Procedure: Declare i number(4); j number(3); n number(3):=&n; Begin for i in 1..n loop dbms_output.put_line(i||' Table'); dbms_output.put_line('----------'); for j in 1..10 loop dbms_output.put_line(i||'*'||j||'='||n*j); end loop; end loop; end;/

Execution:

SQL> @e:\plsql\3.sqlEnter value for n: 2old 4: n number(3):=&n;new 4: n number(3):=2;

1 Table----------1*1=21*2=41*3=61*4=81*5=101*6=121*7=141*8=161*9=181*10=20

2 Table----------2*1=22*2=4

Page 4: Pl SQL Progs

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

PL/SQL procedure successfully completed.

Conclusion: A pl/sql program is successfully executed for Multiplication Tables

Page 5: Pl SQL Progs

PRIME NUMBER

Aim: To write a pl/sql program to check weather given number is Prime or not.

Procedure:

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

Execution:

SQL> @e:\plsql\prime.sqlEnter value for num: 5old 6: num:=&num;new 6: num:=5;5 is prime

Conclusion: A pl/sql program is successfully executed to check the given number is prime or not.

FACTORIAL OF A NUMBER.

Page 6: Pl SQL Progs

Aim: To write a pl/sql program to finding factorial of given number.

Procedure:

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

Output:

SQL> @e:\plsql\fact.sqlEnter value for n: 5old 3: n number(4):=&n;new 3: n number(4):=5;the factorial of 5 is:120

PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed for finding factorial of a given number.

Page 7: Pl SQL Progs

Reverse Of A Number

Aim: To write a pl/sql program to generate reverse for given number.

Procedure:

declare n number(4):=&n; s number(4):=0; r number(4); begin while n>0 loop r:=mod(n,10); s:=(s*10)+r; n:=trunc(n/10); end loop; dbms_output.put_line(‘the reverse number is:’); dbms_output.put_line(s);end; /

Execution:

SQL> @e:\plsql\rev.sqlEnter value for n: 457old 2: n number(4):=&n;new 2: n number(4):=457;

the reverse number is:754

PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed to generate reverse number for given number.

FIBINOCCI SERIES

Page 8: Pl SQL Progs

Aim: To write a pl/sql program to generate fibinocci series.

Procedure: declare a number(3):=1; b number(3):=1; c number(3); n number(3):=&n;begin Dbms_output.put_line('the fibinocci series is:'); while a<=n loop dbms_output.put_line(a); c:=a+b; a:=b; b:=c; end loop; end; /

Execution:SQL> @e:\plsql\fibi.sqlEnter value for n: 13old 5: n number(3):=&n;new 5: n number(3):=13;the fibinocci series is:11235813PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed for to generate fibinocci series.

EVEN OR ODD

Aim: To write a pl/sql program to find given number is even or odd.

Procedure:

Page 9: Pl SQL Progs

Declare n number(4):=&n; Begin if mod(n,2)=0 then dbms_output.put_line(n||' even number'); else dbms_output.put_line(n||' odd number'); end if;end;/

Execution:

SQL> @e:\plsql\even.sqlEnter value for n: 5old 2: n number(4):=&n;new 2: n number(4):=5;

5 odd number

PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed for finding even or odd.

Page 10: Pl SQL Progs

LETTER BY LETTER

Aim: To write a pl/sql program to print a string in a letter by letter format.

Procedure:

declare str varchar(10):='&str'; len number(2); i number(3):=1; begin len:=length(str); while i<=len loop Dbms_output.put_line(substr(str,i,1)); i :=i+1; end loop; end; /

Output:

SQL> @e:\plsql\9a.sqlEnter value for str: svcetold 2: str varchar(10):='&str';new 2: str varchar(10):='svcet';svcet

PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed for printing a string letter by letter.

SPACE AFTER EACH LETTER

Page 11: Pl SQL Progs

Aim: To write a pl/sql program to insert a space after each letter in a given string.

Procedure:

declare str varchar(10):='&str'; len number(2); i number(3):=1; newstr varchar(20); begin len:=length(str); while i<=len loop newstr:=newstr||substr(str,i,1)||' '; i:=i+1; end loop; dbms_output.put_line(newstr); end; /

Execution:

SQL> @e:\plsql\9b.sqlEnter value for str: svcetold 2: str varchar(10):='&str';new 2: str varchar(10):='svcet';

s v c e t

PL/SQL procedure successfully completed.

Conclusion:a pl/sql program is successfully executed for to insert a space after each

letter.

Page 12: Pl SQL Progs

INSERTING A ROW USING PL/SQL

Aim: To write a pl/sql program for inserting a row into vender table .

Procedure: 1.Creating vender table.

SQL> create table vendor( 2 v_code number(5) primary key, 3 v_name varchar(10), 4 v_contact varchar(10), 5 v_area varchar(10), 6 v_phone number(10));

Table created. 2. Inserting row using pl/sql

SQL> declare begin insert into vendor values(1012,'bajaj','delhi',121,9949481807); end; /

PL/SQL procedure successfully completed.

3. Displaying the rowSQL> select *From vendor;

V_CODE V_NAME V_CONTACT V_AREA V_PHONE--------- ---- ------------- -------------- --- ------------ -------------- 1012 bajaj delhi 121 9949481807

Conclusion: a pl/sql program is successfully executed for inserting a row into vender table.

COUNTING NO OF PRODUCTS

Page 13: Pl SQL Progs

Aim: To write a pl/sql program to print number of products whose Price between 0 to 50, 50 to 100,100 to 150,150 to 200, 200 to 250.

Procedure: 1.creating product table:

SQL> create table product( 2 p_code number(4), 3 p_name varchar(10), 4 p_date date, 5 P_price number(4));

Table created. 2.Inserting rows into product table:

SQL> insert into product values(101,'boost','12-nov-11',190);1 row created.SQL> insert into product values(102,'horlicks','12-nov-11',149);1 row created.SQL> insert into product values(103,'boornvita','12-nov-11',220);1 row created.SQL> insert into product values(104,'complane','12-nov-11',199);1 row created.SQL> insert into product values(105,'lux','13-aug-11',18);1 row created.SQL> insert into product values(106,'dove','13-aug-11',26);1 row created.SQL> insert into product values(107,'axe','13-aug-11',299);1 row created.SQL> insert into product values(108,'playboy','18-nov-11',250);1 row created.SQL> insert into product values(109,'nike','18-nov-11',240);1 row created.SQL> insert into product values(110,'bite','18-nov-11',90);1 row created.

3.Source code:

declare c number(3):=3; low number(3):=1; hi number(3):=50;

Page 14: Pl SQL Progs

i number(3); begin while hi<=300 loop select count(p_price) into c from product where p_price between low and hi; dbms_output.put_line('the no of productos between '||low||'and'||hi||'is'||c) ; low:=hi; hi:=hi+50; end loop; end; /

Execution:

SQL> @e:\plsql\noitems.sqlthe no of productos between 1and50is2the no of productos between 50and100is1the no of productos between 100and150is1the no of productos between 150and200is2the no of productos between 200and250is3the no of productos between 250and300is2

PL/SQL procedure successfully completed.

Conclusion:a pl/sql program is successfully executed for counting no of

products in specified range.

Page 15: Pl SQL Progs

USING %TYPE

Aim: To write a pl/sql program to display the employee details using %type data type.

Procedure: 1.creating emp table:

SQL> create table emp( 2 eno number(4) primary key, 3 ename varchar(8), 4 job varchar(8)); Table created. 2.Inserting values into emp table:

SQL> insert into emp values(101,'kalam','clerk');1 row created.SQL> insert into emp values(100,'john','sales');1 row created.SQL> insert into emp values(102,'priya','clerk');1 row created.

3. Execution: SQL> declare 2 Eno emp.eno%type; 3 Ename emp.ename%type; 4 begin 5 select eno,ename into Eno,Ename 6 from emp 7 where eno=100; 8 dbms_output.put_line('eno:'||Eno); 9 dbms_output.put_line('ename:'||Ename); 10 end; 11 / eno:100 ename:john PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed to display the employee details using %type data type.

USING %ROWTYPE

Page 16: Pl SQL Progs

Aim: To write a pl/sql program to display the employee details using %rowtype data type.

Procedure: Execution:

SQL> declare 2 E emp%rowtype; 3 begin 4 select * into E 5 from emp 6 where eno=100; 7 dbms_output.put_line('eno:'||E.eno); 8 dbms_output.put_line('ename:'||E.ename); 9 end; 10 /

eno:100ename:john

PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed to display the employee details using %type data type.

CALCULATE STUDENT GRADE

Aim: To Write a pl/sql program to calculate the student grade using case statement.

Procedure:

Page 17: Pl SQL Progs

Display ing student details: SQL>select *From student where sno=101; Sno sname group m1 m2 m3 grade ----- ---------- --------- ---- ---- ---- ------- 100 wasim mpc 75 76 88 B Source code:

declare Grd student.grade%type; begin select grade into Grd from student where sno=100; dbms_output.put_line('Grade:'); case Grd when 'A' then Dbms_output.Put_line('Very Good'); when 'B' then Dbms_output.Put_line('Good'); when 'C' then Dbms_output.Put_line('Avrage'); else Dbms_output.Put_line('Fail'); end case; end; /

Output:

SQL> @e:\plsql\case.sql Grade: Good

Conclusion: a pl/sql program is successfully executed to calculate the student grade using case statement.

Page 18: Pl SQL Progs

DISPLAYING EMPLOYEE DETAILS USING CURSORS.Aim: To write a pl/sql program to displaying employee details using cursors.Source code:

declare no emp.eno%type; name emp.ename%type; cursor emp_cur is select eno,ename from emp; begin open emp_cur; loop fetch emp_cur into no,name; exit when emp_cur%notfound; DBMS_output.put_line('Employee No:'||no||'Employee Name:'||name); end loop; close emp_cur; end; /Execution:

SQL> @e:\plsql\c1r.sql;Employee No:101 Employee Name:kalamEmployee No:100 Employee Name:johnEmployee No:102 Employee Name:priyaEmployee No:103 Employee Name:rajaEmployee No:104 Employee Name:giriEmployee No:105 Employee Name:kohiliEmployee No:106 Employee Name:lathaEmployee No:107 Employee Name:hari

PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed for displaying employee details using cursors.

TOP 10 EMPLOYEESAim: To Write a pl/sql program to to displaying top 10 employee details based on salary using cursorsSource code:

declare

Page 19: Pl SQL Progs

i number(2); E emp%rowtype; cursor ec is select * from emp order by salary desc; begin Dbms_output.put_line('Eno Ename Job Salary'); Dbms_output.put_line('----------------------'); i:=1; open ec; loop fetch ec into E; i:=i+1; Dbms_output.put_line(E.eno||' '||rpad(E.ename,8,' ')||' '|| rpad(E.job,5,' ')||' '||E.salary);

exit when i>10 or ec%notfound; end loop; close ec; end; /Execution:

SQL> @e:\plsql\c2.sql;Eno Ename Job Salary------------------------------------104 giri gm 14000106 latha sales 12800111 kiran agm 12800112 swathi clerk 12600113 wasim agm 12400107 hari sales 6000105 kohili clerk 6000108 lakshmi clerk 5600100 john sales 4880101 kalam clerk 4800PL/SQL procedure successfully completed.

Conclusion : a pl/sql program is successfully executed for program to displaying

top 10 employees.

Page 20: Pl SQL Progs

STUDENT MARK LISTAim: To Write a pl/sql program to print mark list using cursors. Procedure:Creating table: SQL>create table Student( sno number(4), sname varchar(10), m1 numbar(3), m2 numbar(3), m3 numbar(3)); table created.SQL>insert into student values (500,’wasim’,70,75,89);1 row inserted.SQL>insert into student values (500,’siva’,75,69,88);1 row inserted.SQL>insert into student values (500,’vani’,84,75,75);1 row inserted.SQL>insert into student values (500,’naga’,67,50,33);1 row inserted.Source code:

declare stu student%rowtype; total number(4); result varchar(4); cursor c is select * From student;begin for stu in c loop Dbms_output.put_line('STUDENT MARKLIST'); Dbms_output.put_line('----------------------------'); Dbms_output.put_line('sno:'||stu.sno||' sname:'||stu.sname); total:=stu.m1+stu.m2+stu.m3; if stu.M1>35 and stu.M2>35 and stu.M3>35 then result:='pass'; else result:='fail'; end if; Dbms_output.put_line('m1:'||stu.M1||' m2:'||stu.M2||' m3:'||stu.M3); Dbms_output.put_line('total:'||total||' result:'||result); end loop;end;/

Page 21: Pl SQL Progs

Execution: SQL> @e:\plsql\c3.sql

STUDENT MARKLIST---------------------------sno:500 sname:wasimm1:70 m2:75 m3:89total:234 result:pass

STUDENT MARKLIST--------------------------sno:501 sname:sivam1:75 m2:69 m3:88total:232 result:pass

STUDENT MARKLIST--------------------------sno:502 sname:vanim1:84 m2:75 m3:75total:234 result:pass

STUDENT MARKLIST---------------------------sno:504 sname:nagam1:67 m2:50 m3:33total:150 result:fail

PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed for displaying the student mark list using cursors. 

PARAMETERIZED CURSORAim: To Write a pl/sql program to display employees using parameterized cursor. Source code: declare

n number; er emp%rowtype; cursor c(d number) is

Page 22: Pl SQL Progs

select * from emp where emp.eno=d;begin n:=&n; open c(n); if c%isopen then loop fetch c into er; exit when c%notfound; Dbms_output.put_line('Employee No:'||er.eno ||'Employee Name:'||er.ename); end loop; else DBMS_OUTPUT.PUT_LINE('not found'); end if; close c;end; /

Execution: SQL> @e:\plsql\c4.sqlEnter value for n: 100old 7: n:=&n;new 7: n:=100;Employee No:100 Employee Name:mahesh

PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed for displaying employees using parameterized cursor.

INCREMENTING SALARY OF EMPLOYEES.Aim: To Write a pl/sql program to update the commission values for all employees with salary less than 2000 by adding Rs.1000 to existing employees. Source code: declare cursor c is select *From emp for update; emp_rec emp%rowtype;begin for emp_rec in c loop if emp_rec.salary<2000 then update emp set comm=comm+1000

Page 23: Pl SQL Progs

where current of c; end if; end loop; end;/Displaying rows before execution: SQL>Select *From employee; Eno Ename Job Salary comm

----------------------------------------------------106 latha sales 12800 5000111 kiran agm 12800 6000100 john sales 1880 500101 kalam clerk 1800 600105 kohili clerk 1000 200

Execution: SQL>@E:\plsql\incr.sql ; PL/SQL procedure successfully completed.Displaying rows after execution: SQL>Select *From employee; Eno Ename Job Salary comm

----------------------------------------------------106 latha sales 12800 5000111 kiran agm 12800 6000100 john sales 1880 1500101 kalam clerk 1800 1600105 kohili clerk 1000 1200

Conclusion: a pl/sql program is successfully executed for incrementing salary of employees.

Page 24: Pl SQL Progs

DELETING EMPLOYEEAim: To Write a pl/sql program to delete employees whose experience is less then 2 years.Procedure:Employee table: Emp(eno,ename,salary,experience)

Source code:declare cursor c is select *From emp for update; emp_rec emp%rowtype;begin for emp_rec in c loop if emp_rec.experience<2 then delete emp where current of c; end if; end loop;end;/

Execution:

SQL>@del.sql PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed for to delete employees whose experience is less than 2 years.

Page 25: Pl SQL Progs

A PREDEFINED EXCEPTIONAim: To Write a pl/sql program to handle a predefined exception.Source code:

declare n number(4); d number(4); begin n:=&n; d:=n/0; EXCEPTION WHEN ZERO_DIVIDE THEN DBMS_OUTPUT.PUT_LINE('Divide by error exception is caught'); end; /

Execution:SQL> @e:\plsql\exp.sqlEnter value for n: 4old 5: n:=&n;new 5: n:=4;Divide by error exception is caught

PL/SQL procedure successfully completed.

Conclusion: a pl/sql program is successfully executed for handle a predefined exception.

Page 26: Pl SQL Progs

A USER DEFINED EXCEPTIONAim: To Write a pl/sql program to handle a user defined exception.Source code:

declare others Exception; n dept.dno%type; name dept.dname%type; n1 dept.dno%type; begin n1:=&dept_no; select dno,dname into n,name from dept where dno=n1; if n<>n1 then raise others; else update dept set dname='&dname' where dno=n; Dbms_output.put_line('dname updated.'); end if; Exception when others then Dbms_output.put_line('given dno is not found'); end; /

Before execution:

SQL> select *From dept;

DNO DNAME---------- -------- 101 SALES 103 ACCOUNTS 104 ADMIN

Execution:

Page 27: Pl SQL Progs

SQL> @e:\plsql\userexp.sqlEnter value for dept_no: 102old 7: n1:=&dept_no;new 7: n1:=102;Enter value for dname: marketingold 14: update dept set dname='&dname'new 14: update dept set dname='marketing'given dno is not found.PL/SQL procedure successfully completed.

SQL> @e:\plsql\userexp.sqlEnter value for dept_no: 104old 7: n1:=&dept_no;new 7: n1:=104;Enter value for dname: marketold 14: update dept set dname='&dname'new 14: update dept set dname='market'dname updated.

PL/SQL procedure successfully completed.

After execution:

SQL> select *From dept;

DNO DNAME---------- -------- 101 SALES 103 ACCOUNTS 104 market3 rows selected.

Conclusion: a pl/sql program is successfully executed for handle a user defined exception.

Page 28: Pl SQL Progs

SUM OF TWO NUMBERS USING PROCEDURESAim: To Write a pl/sql program for creating a procedure for calculating sum of two numbers.Source code:

create or replace procedure "SUM"(n1 IN NUMBER,n2 IN NUMBER) istotal number(6);begintotal:=n1+n2; dbms_output.put_line('the sum is:'||total);end;

Execution: Method1: SQL> exec sum(10,20); the sum is:30 PL/SQL procedure successfully completed. Method 2: SQL> begin

2 sum(12,13); 3 end; 4 /the sum is:25

PL/SQL procedure successfully completed.