Mc amca04919 plsql programs

1,731 views
1,607 views

Published on

Published in: Technology, Spiritual
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,731
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
76
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mc amca04919 plsql programs

  1. 1. PL-SQL Programs…• Program 1:-Write a PL/SQL block to find the maximum number from given three numbers.declarea number;b number;c number;begina:=&a;b:=&b;c:=&c;if (a>b and a>c) thendbms_output.put_line(a is maximum || a);elsif (b>a and b>c) thendbms_output.put_line(b is maximum || b);elsedbms_output.put_line(c is maximum || c);end if;end;• Program 2:-Write a PL/SQL block to find the sum of first 100 natural nos.declarea number:=0;beginfor i in 1..100loopa:=a+i;end loop;dbms_output.put_line(The sum of 100 natural nos is = ||a);end;SANDIP PATEL(LDRP-ITR) Page 1
  2. 2. • Program 3:-Write a PL/SQL block to find the sum of first 100 odd nos. and even nos)declareodd number:=0;even number:=0;i number;beginfor i in 1..100loopif(i mod 2 = 0) theneven:=even+i;elseodd:=odd+i;end if;end loop;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 onfollowing table Stud (sno, sname, address, city). Table Creation…create table stud(sno number primary key,sname char(15),addr varchar(30),city char(15));insert into stud values(1,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…declareno number;n number;name char(15);add varchar(50);c char(15);SANDIP PATEL(LDRP-ITR) Page 2
  3. 3. beginn:=&n;select sno,sname,addr,city into no,name,add,c from stud where sno=n;dbms_output.put_line(The Sno is || no);dbms_output.put_line(The Sname is || name);dbms_output.put_line(The address is || add);dbms_output.put_line(The city is || c);end;• Program 5:-Write a PL/SQL block for preparing a Net Salary, given employee onfollowing tableEmp (eno, ename, address, city)Salary (eno, basic, da, hra, it)Net_Salary (eno, total_allowance, total_deduction, netpay)Notes : D.A. = 59% of basic , H.R.A. = 500, I.T. = 2% of basicTotal_Allowance = Basic + D.A. + H.R.A., Total_Deduction = I.T.Netpay = Total_Allowance – Total_Deduction. Table Creation…create table emp(eno number primary key,ename char(15),addr varchar(30),city char(15));insert into emp values(1,hiral,2,krishna society,Mehsana.);insert into emp values(2,pinky,4,Kalyaneshwer society,Mehsana.);insert into emp values(3,Dhruvi,24,Pushpavati society,Mehsana);create table salary(eno number references emp,basic number(10,2),da number(10,2) default NULL,hra number(10,2) default 500,it number(10,2) default NULL);insert into salary(eno,basic) values(1,20000);insert into salary(eno,basic) values(2,30000);insert into salary(eno,basic) values(3,40000);SANDIP PATEL(LDRP-ITR) Page 3
  4. 4. update salary set da=basic*0.59,it=basic*0.02;create table netsal(eno number references emp,totalallow number(10,2),totalded number(10,2),netpay number(10,2)); Program…declareno number;n number;d number(10,2);b number(10,2);h number(10,2);i number(10,2);ta number(10,2);td number(10,2);np number(10,2);beginn:=&n;select eno,basic,da,hra,it into no,b,d,h,i from salary where eno=n;ta:=b+d+h;td:=i;np:=ta-td;insert into netsal values(no,ta,td,np);end;• Program 6:-Write a PL/SQL block to raise the salary by 20% of given employeeon following table.Emp_Salary (eno, ename, city, salary) Table Creation…SANDIP PATEL(LDRP-ITR) Page 4
  5. 5. eno number primary key,ename char(15),city char(15),sal number(10,2));insert into empsal values(1,Hiral,Mehsana,20000);insert into empsal values(2,Pinkey,Mehsana,15000);insert into empsal values(3,Dhruvi,Mehsana,10000); Program…declaren number;s number(10,2);beginn:=&n;--select sal into s from empsal where eno=n;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. Youhave to delete this record and display the appropriate message on the followingtable.Emp (eno, ename, address, city) Table Creation…create table emp1(eno number primary key,ename char(15),addr varchar(30),SANDIP PATEL(LDRP-ITR) Page 5
  6. 6. city char(15));insert into emp1 values(1,hiral,2,krishna society,Mehsana.);insert into emp1 values(2,pinky,4,Kalyaneshwer society,Mehsana.);insert into emp1 values(3,Dhruvi,24,Pushpavati society,Mehsana); Program…declaren number;beginn:=&n;delete from emp1 where eno=n;if sql%found thendbms_output.put_line(The record ||n|| success fully deleted);elsedbms_output.put_line(The record ||n|| not found);end if;end;• Program 8:-Write 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),addr varchar(30),city char(15));insert into stu values(1,hiral,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.);SANDIP PATEL(LDRP-ITR) Page 6
  7. 7. 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…declarecursor c_stu is select sno,sname,addr,city from stu;n number;no number;name char(15);a varchar(30);c char(15);beginopen c_stu;if c_stu%isopen thenloopfetch c_stu into no,name,a,c;exit when c_stu%rowcount > 5;dbms_output.put_line( ||no|| ||name|| ||a|| ||c);end loop;end if;close c_stu;end;• Program 9:-Write a Cursor for preparing a Net Salary for employee’s of financedepartment and Net Pay is more than 10,000 on following table.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(SANDIP PATEL(LDRP-ITR) Page 7
  8. 8. eno number primary key,ename char(15),dept char(20),addr varchar(30),city char(15));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,account,4,Kalyaneshwer society,Mehsana.);insert into emp2 values(5,jaya,finace,24,Pushpavati society,Mehsana);create table salary1(eno number references emp2,basic number(10,2),da number(10,2) default NULL,hra number(10,2) default 500,it number(10,2) default NULL);insert into salary1(eno,basic) values(1,2000);insert into salary1(eno,basic) values(2,30000);insert into salary1(eno,basic) values(3,40000);insert into salary1(eno,basic) values(4,15000);insert into salary1(eno,basic) values(5,10000);update salary1 set da=basic*0.59,it=basic*0.02;create table netsalary(eno number references emp2,totalallow number(10,2),totalded number(10,2),netpay number(10,2)); Program…declarecursor c_salemp is select emp2.eno,basic,da,hra,it from emp2,salary1where dept=finace and emp2.eno=salary1.eno;no number;d number(10,2);SANDIP PATEL(LDRP-ITR) Page 8
  9. 9. b number(10,2);h number(10,2);i number(10,2);ta number(10,2);td number(10,2);np number(10,2);beginopen c_salemp;loopfetch c_salemp into no,b,d,h,i;exit when c_salemp%notfound;ta:=b+h+d;td:=d;np:=ta-td;if np > 10000 theninsert into netsalary values(no,ta,td,np);end if;end loop;close c_salemp;end;• Program 10:-Write a Cursor to display the employee number, name, departmentand salary of first employee getting the highest salary.Emp (eno, ename, department, address, city)Salary (eno, salary) 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(1,hiral,finace,2,krishna society,Mehsana.);insert into emp2 values(2,pinky,account,4,Kalyaneshwer society,Mehsana.);SANDIP PATEL(LDRP-ITR) Page 9
  10. 10. insert into emp2 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 salary2(eno number references emp2,sal number(10,2));insert into salary2 values(1,22000);insert into salary2 values(1,12000);insert into salary2 values(2,25000);insert into salary2 values(4,10000); Program…declarecursor c_empsal is select salary2.eno,ename,dept,sal fromsalary2,emp2 where sal in(select max(sal) from salary2) andemp2.eno=salary2.eno;n salary2.eno%type ;name emp2.ename%type;s salary2.sal%type;d emp2.dept%type;beginopen c_empsal;loopfetch c_empsal into n,name,d,s;exit when c_empsal%notfound;dbms_output.put_line(The employee no is||n);dbms_output.put_line(The employee name is ||name);dbms_output.put_line(The employeedepartment is ||d);dbms_output.put_line(The employee salary is ||s);end loop;close c_empsal;end;SANDIP PATEL(LDRP-ITR) Page 10
  11. 11. • Program 11:-Writes a Function to check whether the given number is prime or not. Program…create or replace function prime(a in number) return number isj number:=0;b number:=0;n number:=a;beginb:=n-1;for i in 2..bloopif (mod(a,i)=0) thenj:=1;exit;end if;end loop;--dbms_output.put_line(The j is||j);return j;end;declarea number;j number;begina:=&a;j:=prime(a);if(j=1) thendbms_output.put_line (Not prime no);elsedbms_output.put_line (prime no);end if;end;• Program 12:-Write a Function to find the sum of digits of accepted no.SANDIP PATEL(LDRP-ITR) Page 11
  12. 12.  Program…create or replace function sumdig(a in number) return number isb number;c number:=0;m number;beginm:=a;for a in 0..mloopb:=m mod 10;c:=b+c;m:=trunc(m/10);end loop;return c;end;declarea number;c number;begina:=&a;c:=sumdig(a);dbms_output.put_line (sum of all digits is = ||c);end;• Program 13:-Write a Function to display first 25 Fibonacci nos. Program…create or replace function fibo(a in number) return number isn number:=a;m number:=0;s number;SANDIP PATEL(LDRP-ITR) Page 12
  13. 13. c number;begindbms_output.put_line(m= ||m);dbms_output.put_line(n= ||n);for c in 1..27loops:=m+n;dbms_output.put_line (||s);m:=n;n:=s;end loop;return 0;end;declaren number:=1;s number;begins:=fibo(n);end;• Program 14:-Write a Function to display the reverse string of a given string. Program…create or replace function f_reverse(str in varchar) return varchar iss varchar(5);l number;beginl:=length(str);for i in reverse 1..lloops:=s||substr(str,i,1);end loop;SANDIP PATEL(LDRP-ITR) Page 13
  14. 14. return s;end;declarestr varchar(50);s varchar(50);beginstr:=&str;s:=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 salaryon following table.Emp (eno, ename, city, salary) Table Creation…create table emps(eno number primary key,ename char(15),city char(15),sal number(10,2));insert into emps values(1,Hiral,Mehsana,20000);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 iss number(10,2);beginselect sal into s from emps where eno=no;return s;end;declareno number;SANDIP PATEL(LDRP-ITR) Page 14
  15. 15. s number(10,2);beginno:=&no;s:=getno(no);dbms_output.put_line(The salary of ||no|| is ||s);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 stud1(sno number primary key,sname char(15),sub1 number,sub2 number,sub3 number,grade char(15));insert into stud1 values(1,Pray,98,94,90,Distinction);insert into stud1 values(2,Jay,57,74,40,First);insert into stud1 values(3,Prisha,58,54,50,Second);insert into stud1 values(4,Masum,48,44,40,Pass);insert into stud1 values(5,Shyam,40,40,40,Pass); Program…create or replace function totalpass(s in char) return number isno number;cursor c_total is select count(sno) from stud1 where grade=s;beginopen c_total;loopfetch c_total into no;exit when c_total%notfound;end loop;close c_total;return no;SANDIP PATEL(LDRP-ITR) Page 15
  16. 16. end;declares char(5):=Pass;n number;beginn:=totalpass(s);dbms_output.put_line(The total no of student who are pass is ||n);end;• Program 17:-Write a Function to assign the grade to the entire student using followingtableStud (sno, sname, sub1, sub2, sub3, and grade)Note: If percentage >= 70 then ‘Distinction’ elseIf percentage >= 60 then ‘First’ elseIf percentage >= 50 then ‘Second’Otherwise ‘Fail’ Table Creation…create table stud1(sno number primary key,sname char(15),sub1 number,sub2 number,sub3 number,grade char(15));insert into stud1 values(1,Pray,98,94,90,Distinction);insert into stud1 values(2,Jay,57,74,40,First);insert into stud1 values(3,Prisha,58,54,50,Second);insert into stud1 values(4,Masum,48,44,40,Pass);insert into stud1 values(5,Shyam,40,40,40,Pass); Program…create or replace function givegrade(p in number) return char isg char(15);beginSANDIP PATEL(LDRP-ITR) Page 16
  17. 17. if p >= 70 theng:=Distinction;return g;elsif p >= 60 theng:=First;return g;elsif p>= 50 theng:=Pass;return g;elseg:=Fail;return g;end if;end;declarecursor c_grade is select sno,sub1,sub2,sub3 from stud1;no stud1.sno%type;s1 stud1.sub1%type;s2 stud1.sub2%type;s3 stud1.sub3%type;t number;g stud1.grade%type;p number(10,2);beginopen c_grade;loopfetch c_grade into no,s1,s2,s3;exit when c_grade%notfound;t:=s1+s2+s3;p:=t/3;g:=givegrade(p);update stud1 set grade=g where sno=no;end loop;close c_grade;end;• Program 18:-Write a Procedure to check the given year is leap year or not. Program…SANDIP PATEL(LDRP-ITR) Page 17
  18. 18. create or replace procedure leapyear(y in number) isbeginif y mod 4 =0 and y mod 100 <>0 or y mod 400=0 thendbms_output.put_line(The || y|| is leap year);elsedbms_output.put_line(The || y|| is not leap year);end if;end;declarey number;beginy:=&y;leapyear(y);end;• Program 19:-Write a Procedure to display the following type of Multiplication Table asper given number.5 * 1 = 55 * 2 = 10” ” = ”” ” = ”5 * 10 = 50 Program…create or replace procedure mult(n in number) isa number:=1;beginfor i in 1..10loopa:=n*i;dbms_output.put_line ( n || * ||i|| = ||a);end loop;end;SANDIP PATEL(LDRP-ITR) Page 18
  19. 19. declaren number;beginn:=&n;mult(n);end;• Program 20:-Write a Procedure to display this kind of output on screen.12 33 4 54 5 6 75 6 7 8 9. . . . . 90 91 Program…create or replace procedure disp(n in number) isa number:=0;beginfor i in 1..nloopfor j in 1..iloopa:=a+1;dbms_output.put( ||a);end loop;dbms_output.put_line( );a:=i;end loop;end;SANDIP PATEL(LDRP-ITR) Page 19
  20. 20. declaren number;beginn:=&n;disp(n);end;• Program 21:-Write a Procedure to convert given octal number to decimal number. Program…create or replace procedure octdes(n in number) isa number:=1;no number;ans number:=1;r number;s number:=0;beginno:=n;while no > 0loopr:=no mod 10;ans:=r * a;s:=s+ans;a:=a * 8;no:=trunc(no/10);end loop;dbms_output.put_line(The decimal no of octal no is ||s);end;declareno number;beginno:=&no;octdes(no);SANDIP PATEL(LDRP-ITR) Page 20
  21. 21. end;• Program 22:-Write a Procedure that take Employee Number and return all theinformation related to the employee. Display the following format using table - Emp(eno, ename, city, salary)Employee Number Employee Name City SalaryRecords are displayed here Program…create or replace procedure recdisp(n in number) iscursor c_emps is select eno,ename,city,sal from emps where eno=n;no emps.eno%type;name emps.ename%type;c emps.city%type;s emps.sal%type;beginopen c_emps;loopfetch c_emps into no,name,c,s;exit when c_emps%notfound;dbms_output.put_line(no|| ||name|| ||c|| ||s);end loop;end;declaren number;beginn:=&n;dbms_output.put_line(Employee no Employee name citysalary);recdisp(n);end;• Program 23:-SANDIP PATEL(LDRP-ITR) Page 21
  22. 22. Writes a Package that has a Function that checks the given string ispalindrome or not Program…create or replace package strings asfunction palindrom(s varchar) return varchar;end strings;create or replace package body strings asfunction palindrom(s varchar) return varchar isn number;c varchar(50);beginn:=length(s);for i in reverse 1..nloopc:=c || substr(s,i,1);end loop;dbms_output.put_line(The Entered string is ||s);dbms_output.put_line(The Reverse string is ||c);return(c);end;end strings;declares varchar(50);c varchar(50);begins:=&s;c:=strings.palindrom(s);if s=c thendbms_output.put_line(The given string is Palindrom );elsedbms_output.put_line(The given string is NotPalindrom);end if;end;• Program 24:-Write a Package that has a Procedure to find 1+1/2+1/3+ . . . . . +1/n. Program…SANDIP PATEL(LDRP-ITR) Page 22
  23. 23. create or replace package con asfunction raci(n number) return number;End con;create or replace package body con asfunction raci(n number) return number isa number(10,2):=0;beginfor i in 1..nloopa:=a+1/i;end loop;return(a);end;end con;declaren number;a number(10,2);beginn:=&n;a:=con.raci(n);dbms_output.put_line(The Answer is ||a);end;create or replace package con asprocedure raci(n number);End con;create or replace package body con asprocedure raci(n number) isa number(10,2):=0;beginfor i in 1..nloopa:=a+1/i;end loop;dbms_output.put_line(The Answer is ||a);end;end con;SANDIP PATEL(LDRP-ITR) Page 23
  24. 24. declaren number;beginn:=&n;con.raci(n);end;• Program 25:-Write a Package that has a Function to check given number is not negativeand a Procedure to convert the given number into word. For Example 25 = TwentyFive. Program…create or replace package pac1 asfunction check1(n number) return number;function basic(n number) return varchar;End pac1;create or replace package body pac1 asfunction basic(n number) return varchar asbeginif n=1 thenreturn ( One);elsif n=2 thenreturn ( Two);elsif n=3 thenreturn ( Three);elsif n=4 thenreturn ( Four);elsif n=5 thenreturn ( Five);elsif n=6 thenreturn ( Six);elsif n=7 thenreturn ( Seven);elsif n=8 thenreturn ( Eight);elsif n=9 thenreturn ( Nine);end if;SANDIP PATEL(LDRP-ITR) Page 24
  25. 25. end;function check1(n number) return number ism number;mo varchar(10);r number;l number;beginm:=n;mo:=m;l:=length(mo);dbms_output.put_line(length =||l);if n<0 thendbms_output.put_line(The Number is Negative);return 0;elsif n>0 thendbms_output.put_line(The Number is Positive);if n<=20 thenif n=1 thendbms_output.put_line(n|| = One);elsif n=2 thendbms_output.put_line(n|| = Two);elsif n=3 thendbms_output.put_line(n|| = Three);elsif n=4 thendbms_output.put_line(n|| = Four);elsif n=5 thendbms_output.put_line(n|| = Five);elsif n=6 thendbms_output.put_line(n|| = Six);elsif n=7 thendbms_output.put_line(n|| = Seven);elsif n=8 thendbms_output.put_line(n|| = Eight);elsif n=9 thendbms_output.put_line(n|| = Nine);elsif n=10 thendbms_output.put_line(n|| = Ten);elsif n=11 thendbms_output.put_line(n|| = Elevan);elsif n=12 thendbms_output.put_line(n|| = Twelve);elsif n=13 thendbms_output.put_line(n|| = Thirteen);elsif n=14 thendbms_output.put_line(n|| = Fourteen);elsif n=15 thenSANDIP PATEL(LDRP-ITR) Page 25
  26. 26. dbms_output.put_line(n|| = Fifteen);elsif n=16 thendbms_output.put_line(n|| = Sixteen);elsif n=17 thendbms_output.put_line(n|| = Seventeen);elsif n=18 thendbms_output.put_line(n|| = Eighteen);elsif n=19 thendbms_output.put_line(n|| = Nineteen);elsif n=20 thendbms_output.put_line(n|| = Twenty);end if;end if;if n>=21 and n<=29 thenmo:=substr(mo,1,1);if mo=2 thenmo:=Twenty;while m>0loopr:=m mod 10;exit;end loop;dbms_output.put_line(n||= ||mo||pac1.basic(r));end if;elsif n=30 thendbms_output.put_line(n||= Thirty);elsif n>=31 and n<=39 thenmo:=substr(mo,1,1);if mo=3 thenmo:=Thirty;while m>0loopr:=m mod 10;exit;end loop;dbms_output.put_line(n||= ||mo||pac1.basic(r));end if;elsif n=40 thendbms_output.put_line(n||= Fourty);elsif n>=41 and n<=49 thenmo:=substr(mo,1,1);if mo=4 thenmo:=Fourty;while m>0SANDIP PATEL(LDRP-ITR) Page 26
  27. 27. loopr:=m mod 10;exit;end loop;dbms_output.put_line(n||= ||mo||pac1.basic(r));end if;elsif n=50 thendbms_output.put_line(n||= Fifty);elsif n>=51 and n<=59 thenmo:=substr(mo,1,1);if mo=5 thenmo:=Fifty;while m>0loopr:=m mod 10;exit;end loop;dbms_output.put_line(n||= ||mo||pac1.basic(r));end if;elsif n=60 thendbms_output.put_line(n||= Sixty);elsif n>=61 and n<=69 thenmo:=substr(mo,1,1);if mo=6 thenmo:=Sixty;while m>0loopr:=m mod 10;exit;end loop;dbms_output.put_line(n||= ||mo||pac1.basic(r));end if;elsif n=70 thendbms_output.put_line(n||= Seventy);elsif n>=71 and n<=79 thenmo:=substr(mo,1,1);if mo=7 thenmo:=Seventy;while m>0loopr:=m mod 10;exit;end loop;SANDIP PATEL(LDRP-ITR) Page 27
  28. 28. dbms_output.put_line(n||= ||mo||pac1.basic(r));end if;elsif n=80 thendbms_output.put_line(n||= Eighty);elsif n>=81 and n<=89 thenmo:=substr(mo,1,1);if mo=8 thenmo:=Eighty;while m>0loopr:=m mod 10;exit;end loop;dbms_output.put_line(n||= ||mo||pac1.basic(r));end if;elsif n=90 thendbms_output.put_line(n||= Ninty);elsif n>=91 and n<=99 thenmo:=substr(mo,1,1);if mo=9 thenmo:=Ninty;while m>0loopr:=m mod 10;exit;end loop;dbms_output.put_line(n||= ||mo||pac1.basic(r));end if;elsif n=100 thendbms_output.put_line(n||= Hundred);end if;return 0;elsif n=0 thendbms_output.put_line(n|| = Zero);return 0;end if;end;end pac1;declaren number;a number;SANDIP PATEL(LDRP-ITR) Page 28
  29. 29. beginn:=&n;if n<=100 thena:=pac1.check1(n);elsedbms_output.put_line(Number must be Less than 100);end if;end;• Program 26:-Write a Package that has two object on following table.(i) The Function is used to calculate the NetSalary.(ii) The Procedure is used to display the Pay Slip in following format.Emp (eno, ename, basic)Salary (eno, da, hra, it, gross_sal, net_sal)Notes : D.A. = 59% of basic , H.R.A. = 500, I.T. = 2% of basicGross_salary = Basic + D.A. + H.R.A., Net_salary = Gross_salary – I.T.Employee Number Employee Name Gross Salary Net SalaryTotal ******** ******** Table Creation…create table empac(eno number,ename char(25),basic number(10,2));insert into empac values(1,Hiral,100000);insert into empac values(2,Dhruvi,200000);insert into empac values(3,Pinky,10000);create table salpac(eno number,da number(10,2),hra number(10,2),it number(10,2),SANDIP PATEL(LDRP-ITR) Page 29
  30. 30. gross number(10,2),net number(10,2) default NULL);insert into salpac(eno,da,hra,it) values(1,100000*0.59,500,100000*0.02);insert into salpac(eno,da,hra,it) values(2,200000*0.59,500,200000*0.02);insert into salpac(eno,da,hra,it) values(3,10000*0.59,500,10000*0.02);update salpac set gross = 100000+da+hra where eno=1;update salpac set gross = 200000+da+hra where eno=2;update salpac set gross = 10000+da+hra where eno=3;procedure display(n in number); Program…create or replace package countnetsal asfunction netsalary(n number) return number;procedure display;end countnetsal;create or replace package body countnetsal asfunction netsalary(n number) return number isa number(10,2):=0;d number(10,2);c number(10,2);g number(10,2);no number;b number(10,2);cursor c_emp is select eno,basic from empac where eno=n;beginopen c_emp;if c_emp%isopen thenloopfetch c_emp into no,b;exit when c_emp%notfound;d:=b*0.59;c:=b*0.02;g:=b+c+d;insert into salpac values(n,d,500,c,g,g-500);dbms_output.put_line(Successfully Inserted);end loop;elsedbms_output.put_line(Successfully Not Inserted);SANDIP PATEL(LDRP-ITR) Page 30
  31. 31. end if;close c_emp;return(a);end;procedure display ascursor c_sal is select empac.eno,ename,gross,net from empac,salpac whereempac.eno=salpac.eno;no empac.eno%type;name empac.ename%type;g salpac.gross%type;n salpac.net%type;tg number(10,2):=0;tn number(10,2):=0;beginopen c_sal;loopfetch c_sal into no,name,g,n;exit when c_sal%notfound;tg:=tg+g;tn:=tn+n;dbms_output.put_line( ||no || ||name|| ||g|| ||n);end loop;close c_sal;dbms_output.put_line(__________________________________);dbms_output.put_line( Total tg || ||tn);end;end countnetsal;declaren number;a number(10,2);beginn:=&n;a:=countnetsal.netsalary(n);dbms_output.put_line( Employee No Employee Name GrossSalaryNetsalary);countnetsal.display();end;declarecursor c_sal is select empac.eno,ename,gross,net from empac,salpac whereempac.eno=salpac.eno;no empac.eno%type;SANDIP PATEL(LDRP-ITR) Page 31
  32. 32. name empac.ename%type;g salpac.gross%type;n salpac.net%type;tg number(10,2):=0;tn number(10,2):=0;beginopen c_sal;loopfetch c_sal into no,name,g,n;exit when c_sal%notfound;tg:=tg+g;tn:=tn+n;dbms_output.put_line( ||no|| ||name|| ||g|| ||n);end loop;close c_sal;dbms_output.put_line( Total Gross ||tg);dbms_output.put_line( Total Netsalary ||tn);end;create or replace procedure check1 isbegindbms_output.put_line(HELLO);end;begincheck1();end;• Program 27:-Write a Trigger on Insert to convert the name into capital letters. Program…create or replace trigger t1before insert on studfor each rowdeclareno number;name varchar(10);beginno:=:new.sno;name:=upper(:new.sname);SANDIP PATEL(LDRP-ITR) Page 32
  33. 33. dbms_output.put_line(the ||name||No ||no);:new.sno:=no;:new.sname:=name;end;****************************************Extra*************************************create or replace trigger Upperletterafter insert on studfor each rowdeclaresno number;sname varchar(10);beginsno:=:new.sno;sname:=:new.sname;insert into stud1 values(Sno,Sname);end;*****************************************************************************declaresname varchar(10);beginsname:=upper(:new.sname);end;• Program 28:-Write a Trigger to check the Pincode is exactly six digit or not. Program…create or replace trigger tpinbefore insert on pinfor each rowSANDIP PATEL(LDRP-ITR) Page 33
  34. 34. declareno varchar(10);beginno:=length(:new.p);if no<>6 thenraise_application_error(-20001,Pincode must be six digit);end if;end;• Program 29:-Write a Trigger to check the mark is not zero or negative. Program…create or replace trigger negbefore insert on tranfor each rowdeclareno number;beginno:=:new.obt;if no<=0 thenraise_application_error(-200002,Number is Negative);end if;end;• Program30:-Write a Trigger that check the student_id must be start with ‘M’. Program…create or replace trigger capbefore insert on strfor each rowdeclarename char(10);beginname:=:new.sname;SANDIP PATEL(LDRP-ITR) Page 34
  35. 35. if name not like %M% thenraise_application_error(-20003,Name is not start with M);end if;end;• Program31:-Develop a PL/SQL Project for Student Marksheet using all thedatabase object (Cursor,Function, Procedure, Package and Trigger) on followingtable.Student_Master(sno, sname, total_marks, marks_obtain, result)Subject_Master(sub_code, sub_name, sub_max_mark)Student_Transaction(sno, sub_code, marks_obtain)Student Mark SheetSr. No. Roll. No. Name TotalMarksMarkObtainGrade Table Creation…create table student(sno number primary key,sname char(10),total number,obt number,grade char(10));insert into student(sno,sname,total) values(1,Ronak,250);insert into student(sno,sname,total) values(2,Reena,250);create table subject(sbno number primary key,sbname char(10),sbm number);insert into subject values(301,CPP,48);insert into subject values(302,OR,48);create table tran(sno number references student,sbno number references subject,SANDIP PATEL(LDRP-ITR) Page 35
  36. 36. obt number);insert into tran values(1,301,25);insert into tran values(1,302,48);insert into tran values(2,301,55);insert into tran values(2,302,48); Program…*************************package****************************create or replace package mixt asprocedure stdins(no number);function stdgrd(no number) return number;procedure disp;end mixt;create or replace package body mixt asprocedure stdins(no number) ascursor c_mark is select sum(obt) from tran where sno=no;tt number;beginopen c_mark;loopfetch c_mark into tt;exit when c_mark%notfound;update student set obt=tt where sno=no;end loop;end;function stdgrd(no number) return number ascursor c_mark is select obt from student where sno=no;tt number;g char(10);beginopen c_mark;loopfetch c_mark into tt;exit when c_mark%notfound;if tt>=35 and tt<=50 theng:=Pass;elsif tt>=51 and tt<=100 theng:=Seond;elsif tt>=101 and tt<=200 theng:=First;SANDIP PATEL(LDRP-ITR) Page 36
  37. 37. elsif tt>=200 and tt<=250 theng:=Dist;end if;update student set grade=g where sno=no;end loop;return 0;end;procedure disp asi number:=1;cursor c_stud is select sno,sname,total,obt,grade fromstudent;no student.sno%type;name student.sname%type;t student.total%type;o student.obt%type;g student.grade%type;beginDbms_output.put_line(Sr.No Rollno NameTotalmarks Marksobtain Grade);open c_stud;loopfetch c_stud into no,name,t,o,g;exit when c_stud%notfound;dbms_output.put_line( i || ||no|| ||name|| ||t||||o|| ||g);i:=i+1;end loop;end;end mixt;declaren number;a number;beginn:=&n;mixt.stdins(n);a:=mixt.stdgrd(n);mixt.disp;end;*************************Trigger***************************SANDIP PATEL(LDRP-ITR) Page 37
  38. 38. create or replace trigger tckbefore insert on tranfor each rowdeclaret number;m number;beginselect sum(obt) into t from tran where sno=:new.sno;t:=t+:new.obt;select sbm into m from subject where sbno=:new.sbno;if t>250 or :new.obt>m thenraise_application_error(-200004,Total of obtain marks mustless than 250);end if;end;• Program32:-Develop a PL/SQL Project for Employee Payslip using all thedatabase object (Cursor,Function, Procedure, Package and Trigger) on followingtable.Emp_Master(eno,ename,desc_code, basic, gross_sal, net_sal)Pay_Master(desc_code, desc_name, da, hra, it, max_basic)Emp_Transaction(eno,desc_code, basic)Nisarg Softech Pvt. Ltd. Date :Salary Slip for Month of :-Employee Number Employee Name Gross Salary Net SalaryTotal ******** ******** Table Creation…create table empmas(eno number primary key,ename char(10),dcode number references paymas,basic number,grossal number,SANDIP PATEL(LDRP-ITR) Page 38
  39. 39. netsal number);insert into empmas(eno,ename,dcode,basic) values(1,Ronak,1,10000);insert into empmas(eno,ename,dcode,basic) values(2,Reena,2,20000);create table paymas(dcode number primary key,dname char(10),da number,hra number,it number,mbasic number);insert into paymas values(1,Computer,59,500,2,50000);insert into paymas values(2,Printer,49,400,2,20000);create table emptran(eno number,dcode number,basic number); Program…****************************Package*****************************create or replace package pacemp asfunction countsals(no number) return number;procedure disp;end pacemp;create or replace package body pacemp asfunction countsals(no number) return number ascursor c_empc is select da,hra,it,basic from paymas,emptran whereemptran.dcode=no and emptran.dcode=paymas.dcode;b emptran.basic%type;d paymas.da%type;h paymas.hra%type;i paymas.it%type;n empmas.netsal%type;g empmas.grossal%type;beginSANDIP PATEL(LDRP-ITR) Page 39
  40. 40. open c_empc;loopfetch c_empc into d,h,i,b;exit when c_empc%notfound;d:=b*(d/100);i:=b*(i/100);g:=b+d+h;n:=g-i;update empmas set grossal=g,netsal=n where dcode=no;end loop;return 0;end;procedure disp ascursor c_empt is select eno,ename,grossal,netsal from empmas;no empmas.eno%type;name empmas.ename%type;g empmas.grossal%type;ns empmas.netsal%type;begindbms_output.put_line( Emp No Emp Name Grosssal Net sal);open c_empt;loopfetch c_empt into no,name,g,ns;exit when c_empt%notfound;dbms_output.put_line(no|| ||name|| ||g||||ns);end loop;end;end pacemp;declareno number;a number;beginno:=&no;a:=pacemp.countsals(no);pacemp.disp;end;*****************************Trigger***************************SANDIP PATEL(LDRP-ITR) Page 40
  41. 41. create or replace trigger emptbefore insert on empmasfor each rowdeclareb number;beginselect mbasic into b from paymas where dcode=:new.dcode;if :new.basic>b thenraise_application_error(-200005,Basic less than maximum basic);elseinsert into emptran values(:new.eno,:new.dcode,:new.basic);end if;end;SANDIP PATEL(LDRP-ITR) Page 41

×