You are on page 1of 42
PL-SQL Programs... Program 1:- Write a PL/SQL block to find the maximum number from given three numbers. declare a number: b number; e number; begin ana; b&b; eB: if (a>b and a>c) then dbms_output.put_line(‘a is maximum’ || a); elsif (b>a and b>¢) then dbms_output.put_line('b is maximum || b); else dbms_output put_line(‘c is maximum’ | ¢): end if; end; + Program 2:- Write a PL/SQL block to find the sum of first 100 natural nos. declare anumber:-0; begin for iin 1..100 end loop: dbms_output put_line("The sum of 100 natural nos is = '/a); end; SANDIP PATEL(LDRP-ITR) Page 1 + Program 3:- Write a PL/SQL block to find the sum of first 100 odd nos. and even nos) declare odd number:=0: even number:=0; i number; begin for i in 1..100 loop if{i mod 2 = 0) then even:=eventi; alse odd:=odd-i; end if end loo, dbms_output put_line("The Sum of 100 even nos is'|| even); dbms_output.put_line("The Sum of 100 odd nos is "|| odd); end; «Program 4:- Write a PL/SQL block to display the Information of given student on following table Stud (sno, sname, address, city). > Table Creation. create table stud ‘sno number primary key, sname char(15), addr varehar(30), city chan(15)); insert into stud values( | ‘hiral’’2.krishna society'/Mehsana.’); insert into stud values(2,/pinky''4,Kalyaneshwer society’,Mehsana.’); insert into stud values(3,"Dhruvi''24,Pushpavati society’.Mehsana’); ‘>Program. declare no number; number; SANDIP PATEL(LDRP-ITR) Page 2 name char(15); add varchar(50); ce char(15); begin nis&n; select sno,sname,addr.city into no,name.add,e from stud where sn dbms_output put_line(/The Sno is || 0); dbms_output put_line(‘The Sname is ' || name); dbms_output.put_line(The address is "|| add); dbms_output put_line('The city is" | ©); end: Program 5:- wi ite a PL/SQL block for preparing a Net Salary, given employee on following table Emp (eno, ename, address, city) Salary (eno, basie, da, hra, it) Net_Salary (eno, total_allowance, total_deduction, netpay) Notes : D.A. = 59% of basic , R.A. = 500, LT. = 2% of basic Total_Allowance = Basic + D.A. + H-R.A., Total_Deduction = LT. Netpay = Total_Allowance - Total_Deduction. ‘D> Table Creation... create table emp( eno number primary key, ename char(15), addr varchar(30), city chax(15)); insert into emp values(1,‘hiral’ insert into emp values(2,'pinky insert into emp values(3 krishna society Mehsana); “/4.Kalyaneshwer society’ /Mehsana.’); ‘Dhnuvit24,Pushpavati society’/Mebsana’); create table salary( eno number references emp, basic mumber(10,2), da number(10,2) default NULL, hra number(10,2) default 500, it number( 10,2) default NULL); SANDIP PATEL(LDRP-ITR) Page 3 insert into salary(eno, basic) values(1 insert into salary(eno, basic) values(2,30000); insert into salary(eno.basic) values(3,40000); update salary set da=basic*0.59.it-basic"0.02; create table netsal( eno number references emp, totalallow number(10, totalded number( 10.2), netpay number(10.2)): >Program... declare ‘no number; n number; d number( 10,2); number( 10,2) ‘b number( 10.2); i number( 10,2); ta number(10,2); td number(10.2); np number( 10.2); begin n=8n; select eno.basic,da.hra,it into no,b.d.h.i from salary where en insert into netsal values(no.ta,td.np); end; © Program 6: Write a PL/SQL block to raise the salary by 20% of given employee on following tabl SANDIP PATEL(LDRP-ITR) Page 4 Emp_Salary (eno, ename, D> Table Creation... eno number primary key, ename char(15), city char(15), sal number(10,. insert into empsal values(1,Hiral'/Mehsane’ 20000); insert into empsal values(2,’Pinkey’Mehsana’,15000); insert into empsal values(3,'Dhruvi'/Mehsana’,10000); Program... declare number; s number( 1 begin =&n: select sal into s from empsal where en. update empsal set sal=sal~(sal*0.20) where eno=n: end; © Program 7: Write an Implicit Cursor to accept the employee number from the user. You have to delete this record and display the appropriate message on the following table. Emp (eno, ename, address, city) SANDIP PATEL(LDRP-ITR) Page 5 Table Creation... create table emp1( eno number primary key, ename char(15), addr varchar(30), city char(15)); insert into emp1 values(|.‘hiral’’2,krishna society’ /Mehsana.’); insert into emp] values(2,/pinky’,'4.Kalyaneshwer society’ /Mebsana.'); insert into emp] values(3.'Dhruvi'.'24,Pushpavati society’.Mehsana’); n oumber; begin n=8n; delete from emp] where eno=n; if sql%ofound then dbms_output put_line(The record’ ||" success fully deleted’); else dbms_output.put_ine((The record’ In|" not found’); end if, end: © Program 8: ite a Cursor to display the first five records on the following table. Student(sno, sname, address, city) ‘> Table Creation. create table stu( sno number primary key, sname char(15), SANDIP PATEL(LDRP-ITR) Page 6 addr varchar(30), city char(15)) insert into stu values(1hiral’/2,krishna society’ Mehsana.’ insert into stu values(2,'pinky’,'4,Kalyaneshwer society’..Mehsana."); insert into stu values(3,Dhruvi’'24,Pushpavati society’ Mehsana’); insert into stu values(4,'ukti',2,krishna society’Mehsana.’); insert into stu values(5, jaya’'4,Kalyaneshwer society’ Mehsana insert into stu values(6,/prisha’.'2.krishna society’! Ahmedabad insert into stu values(7, pray’ 4,Kalyaneshwer society’Mehsana.’); >Program... declare cursor ¢_stu is select sno,sname,addr.city from stu; n number; ‘no number; name char(13); a varchar(30); © char(15); begin open ¢_stu: iffc_stu%isopen then loop fetch ¢ stu into no.name,a.c; exit when c_stu%rowcount > 5; Program 9:- SANDIP PATEL(LDRP-ITR) dbms_output.put_line(’ ‘nol’ "|jnamel ‘al! ‘eds end loop; end if, close ¢_stu; end; Write a Cursor for preparing a Net Salary for employee's of finance department and Net Pay is more than 10,000 on following tab) Page 7 Emp (eno, ename, department, address, city) Salary (eno, basic, da, hra, it) Net_Salary (eno.total_allowance, total_deduction, netpay) ‘> Table Creation... create table emp2( eno number primary key, ename char(15), dept char(20), addr varchar(30), city char(15)}; insert into emp2 values(|.‘hiral'finace’2,krishna society’ Mehsana’): insert into emp2 values(2,pinky’ {account’4,Kalyaneshwer society’ /Mehsana."); insert into emp? values(3.'Dhruvi’,finace’'24,Pushpavati society’ /Mehsana’): insert into emp2 values(4,ukti'account,'4.Kalyaneshwer society’Mehsana.’); insert into emp2 values(5.'jaya' ‘finace’.'24,Pushpavati society’ Mehsana’): create table salary ( eno number references emp2, basic number(10,2), da number(10,2) default NULL, dhra number(10.2) default 500, it mumber( 10.2) default NULL); insert into salary1(eno,basic) values(1,2000); insert into salary1(eno,basic) values(2,30000); insert into salary] (eno.basic) values(3,40000); insert into salary1(eno,basic) values(4,15000); insert into salary] (eno.basic) values(5,10000); update salary set da~basic*0,59,it-basic*0.0: create table netsalary( eno number references emp2, totalallow number( 10.2), totalded number( 10.2), netpay number(10.2)): SANDIP PATEL(LDRP-ITR) Page 8 Program... declare cursor ¢_salemp is select emp2.eno,basic.da,hra.it from emp2,salary1 where dept~'finace' and emp2.eno~salary1.eno; no number; ¢ aumber( 10.2 b mumber(10, bh mumber(10.2) i number( 10,2 ta number(10,2); td number( 10,2); np mumber( 10,2); begin ‘open c_salemp; loop fetch c_salemp into no.b.d.h.is exit when c_salemp*enotfound; had; thd; nprtactd; ifup > 10000 then insert into netsalary values(no.ta,td.up); end if, end loop; close ¢_salemp; end: © Program 10:- Write a Cursor to display the employee number, name, department and salary of first employee getting the highest salary. Emp (eno, ename, department, address, ci Salary (eno, salary) ‘> Table Creation... SANDIP PATEL(LDRP-ITR) Page 9 >Program. create table emp2( eno number primary key, ename char(15), dept char(20), addr varchar(30), city char(15))s insert into emp2 values(1.‘hiral’finace’2,krishna society’ Mehsana’); insert into emp2 values(2,'pinky’account’'4,Kalyaneshwer society’) Mehsana.’ insert into emp2 values(3,'Dhruvi’,'finace’,'24,Pushpavati society',Mehsana’); insert into emp2 values(4,ukti’account4.Kalyaneshwer society’/Mehsana.’); insert into emp2 values(5,"jaya’,finace' 24, Pushpavati society'/Mehsana’); create table salary2( eno number references emp2, sal number(10, insert into salary2 values(1,22000); insert into salary2 values(1,12000); insert into salary2 values(2,25000); insert into salary2 values(4,10000); declare cursor ¢_empsal is select salary2.eno.ename.dept.sal from salary2,emp2 where sal in(select max(sal) from salary2) and emp2.eno=salary2.eno; n salary2.eno%type ; name emp2.ename%type: s salary2.sal%type: d emp2.dept%type; begin open ¢_empsal; loop fetch c_empsal into n.named.s; exit when c_empsal®notfound: dbms_output.put_line(/The employee no is ‘im); SANDIP PATEL(LDRP-ITR) Page 10 © Program 11:- >Program... dbms_output.put_line('The employee name is "|name); dbms_output put_line('The employee department is Id); dbms_output put_line(/The employee salary is ‘Iss end loop; close c_empsal; end: Writes a Function to check whether the given number is prime or not. create or replace function prime(a in number) return number is j mumber:=0; number:=0; n number: begin b:=n-1; fori in2.b loop if (mod(a,i)=0) then jab exit; end ifs end loop; ~-dbms_output.put_line(The jis'|j); return end: declare a number: j number: begin aaéa; is=prime(a); if(j=1) then SANDIP PATEL(LDRP-ITR) Page 11 dbms_output put _ else dbms_output put_fine (‘prime no’); ine (‘Not prime no’); end ift end; * Program 12:- ‘Write a Function to find the sum of digits of accepted no. Program... create or replace function sumdig(a in number) return number is baumber; ¢ number: ‘m number; begin m:=a; for a in 0..m end loop; return ¢; end; declare a number; c mumber; begin anBa; c:-sumdig(a); dims_output.put_line (‘sum of all digits is =" je); end; SANDIP PATEL(LDRP-ITR) Page 12 * Program 13:- >Program... Write a Function to display first 25 Fibonacci nos. create or replace function fibo(a in number) return mumber is nnumber:=a; m number:=0; s number; number; begin dbms_output.put_line(s dbms_output.put_line('n for in 1..27 loop s:=m-+n; dbms_output.put_line ("|s); © Program 14:- s number; begin sc=fibo(in); end; ‘Write a Function to display the reverse string of a given string. SANDIP PATEL(LDRP-ITR) Page 13 Program... create or replace funetion f_reverse(str in varchar) retum varchar is s varchar(5); I number; begin L=length(ste); for iin reverse 1. loop |subste(strail): end loop; retum s; end; declare str varchar(50); s varchar(50); begin stre—'Sstr; =f reverse(str); dbms_output.put_line(/The reverse string is "/s); end; «Program 15:- Write a Function that take Employee Number and return the salary on following table. Emp (eno, ename, city, salary) ‘DTable Creation... create table emps( eno number primary key, ename char(15), city char(15), sal number(10,2)); insert into emps values(,Hiral'Mehsana',20000); SANDIP PATEL(LDRP-ITR) Page 14 insert into emps values(2,Pinky’Mehsana’.21000); insert into emps values(3,"Dhruvi',Mehsana’,22000); >Program... create or replace function getno(no in number) return number is s number( 10,2); begin select sal into s from emps where eno-no; return s; end; declare no number; s mamber(10.2); begin dbms_output put_line(The salary of * |ino||' iss); end; + Program 16:- Write a Function to count the total number of student having grade ‘PASS’ on following table. Student (sno, sname, grade) ‘> Table Creation... create table stud ( sno number primary key, sname char(15), subl number, sub? mumber, sub3 number, geade char(15)): insert into stud] values(1,Pray’,98,94,90,'Distinetion’); insert into stud values(2,'Jay',57,74,40, First’); insert into stud] values(3,Prisha’,58,54,50,'Second’); insert into stud values(4,Masumy,48,44,40,Pass’); insert into stud! values(5,'Shyam’.40,40,40,'Pass'); SANDIP PATEL(LDRP-ITR) Page 15 Program... create or replace function totalpass(s in char) return number is, no number; cursor ¢ total is select count(sno) from stud] where grade=s; begin open c_total; loop fetch ¢_total into no; exit when ¢_total?enotfound; end loop; close ¢ total; return no; end; declare s char(5 number; dbms_output.put_line(’The total no of student who are pass is '\in); end; Program 17:- Write a Function to assign the grade to the entire student using following table Stud (sno, sname, sub1, sub2, sub3, and grade) Note: If percentage >= 70 then ‘Distinction’ else If percentage >= 60 then ‘First’ else If percentage >= 50 then ‘Second? Otherwise ‘Fail > Table Creation. create table stud1( sno number primary key, name char(15), SANDIP PATEL(LDRP-ITR) Page 16 sub! number, sub2 number, sub3 number, grade char(15)); insert into stud] values(1,Pray’,98,94,90,'Distinetion’); insert into stud! values(2,Jay',57,74,40, First’); insert into stud! values(3,Prisha’,58,54,50, insert into stud values(4,Masum!,48.44,40,'Pass' second’); insert into stud! values(5,Shyam’,40,40,40,'Pass'); Program... SANDIP PATEL(LDRP-ITR) create or replace function givegrade(p in number) return char is g char(15); begin if p>=70 then g=Distinction'; return g; elsif p >= 60 then retum g; else ail’; retum g; end ifs end; declare cursor ¢_grade is select sto,subl,sub2,sub3 from stud; no studi sno%type; sl stud subl%typ: 82 stud] sub2%type; 3 stud.sub3%types t number; g stud] grade%type; pnumber( 10,2); begin open ¢_grade; Page 17 loop fetch c_grade into no,s1,s2,s3; exit when c_grade%notfound; teas]+s2783; prti3; ivegrade(p); update stud! set grade=g where sno-no; end loop; close ¢_grade; end; © Program 18:- Write a Procedure to check the given year is leap year or not. Program... create or replace procedure leapyear(y in number) is begin ify mod 4 =0 and y mod 1000 or y mod 400-0 then dlms_output put_line(/The 'y||"is leap year’); else dbms_output.put_line(/The "| y||" is not leap year’): end if end; declare y number; begin yrky; leapyear(y); end; © Program 19:- Write a Procedure to display the following type of Multiplication Table as per given number. SANDIP PATEL(LDRP-ITR) Page 18 «Program 20:- create or replace procedure mult(n in number) is anumber:=1; begin fori in 1.10 loop dbms_output put fine (n |! * "Vil end loop; end; declare an number; begin =én; muli(n); end; Write a Procedure to display this kind of output on screen. Program... SANDIP PATEL(LDRP-ITR) create or replace procedure disp(n in number) is a number:=0; Page 19 begin foriin ln loop for jin Li loop a=a+l; dbms_output.put( Ja); end loop; dbms_output.put_fine(""): ani: end loop: end; declare anumber; begin n=&n; disp(a); end; «Program 21:- Write a Procedure to convert given octal number to decimal number. >Program, create or replace procedure octdes(n in number) is amumber-=1; no number; ss number:=0; SANDIP PATEL(LDRP-ITR) Page 20 + Program 22:- begin no:=n; while no > 0 loop r=no mod 10; ama *8; no:=trune(no/10); end loop; dbms_output.put_line("The decimal no of octal no is'|s); end; declare no number; begin nno:=&no; octdes(no); end: Write a Procedure that take Employee Number and return all the information related to the employee. Display the following format using table - Emp (eno, ename, city, salary) Employee Number | Employee Name City Salar Records are displayed here Program... SANDIP PATEL(LDRP-ITR) create or replace procedure recdisp(n in number) is, cursor ¢_emps is select eno,ename,city,sal from emps where eno-n; no emps.eno%types name emps.enametype: c emps.city%type; s emps.sal%type; begin ‘open c_emps; Page 21

You might also like