Labcycle - 4 Solutions: Student - Marks Sno Name Course Marks
Labcycle - 4 Solutions: Student - Marks Sno Name Course Marks
/* factorial function */
create or replace function fact(n number) return number is
begin
if n=1 then
return 1;
end if;
return n*fact(n-1);
end;
/* procedure */
create or replace procedure ncr(res out number,n in number,r in number) is
begin
res:=trunc(fact(n)/(fact(n-r)*fact(r)));
end;
/* mai program */
declare
n number:=&n;
r number:=&r;
res number;
begin
ncr(res,n,r);
dbms_output.put_line(res);
end;
2. Writ a PL/SQL procedure to find the number of students who got O, A+, A,
B+ and B grades based on the marks obtained by them in a required course from
the student_marks table given below.
Student_Marks
SNO Name Course Marks
12 Akash CS112 67
53 Venkat CS113 78
32 Ram CS112 87
65 Manogna CS113 69
/* procedure */
create or replace procedure grades(m in out number,o out number,a1 out
number,a2 out number,b1 out number,b2 out number)is
begin
if m>=90 and m<=100 then
o:=o+1;
elsif m>=80 and m<90 then
a1:=a1+1;
elsif m>=70 and m<80 then
a2:=a2+1;
elsif m>=60 and m<70 then
b1:=b1+1;
else
b1:=b2+1;
end if;
end;
/
/* main program */
declare
o number:=0;
a1 number:=0;
a2 number:=0;
b1 number:=0;
b2 number:=0;
c varchar(20);
m number(2);
cursor std_cr(c varchar) is select marks from stumarks where course=c;
begin
open std_cr('&c');
loop
fetch std_cr into m;
exit when std_cr%notfound;
grades(m,o,a1,a2,b1,b2);
end loop;
dbms_output.put_line('o grade: '||o);
dbms_output.put_line('A+ grade:'||a1);
dbms_output.put_line('A grade: '||a2);
dbms_output.put_line('B+ grade: '||b1);
dbms_output.put_line('B grade: '||b2);
end;
/
/* function */
create or replace function salary(d number) return number is
sal number(5);
t number(5);
begin
select salary into sal from employe_num where empno=d;
if sal<=3000 then
t:=sal+sal*0.3;
elsif sal>=3000 and sal<=6000 then
t:=sal+sal*0.2;
else
t:=sal+sal*0.1;
end if;
return t;
end;
/
/*main */
declare
d number:=&d;
x number(5);
begin
x:=salary(d);
update employe_num set salary=x where empno=d;
end;
/
4. Write a PL/SQL function that accepts department number and returns the
total salary of the department.
/* function */
create or replace function total(d in number) return number is
total number:=0;
s number:=0;
cursor emp is select salary from employee where dno=d;
begin
open emp;
loop
fetch emp into s;
exit when emp%notfound;
total:=total+s;
end loop;
close emp;
return total;
end;
/* main */
declare
total_salary number:=0;
dno number:=&dno;
begin
total_salary:=total(dno);
dbms_output.put_line('The total salary of department '||dno||' is '||
total_salary);
end;
/* package specification */
create or replace package operations as
procedure insert1(c varchar,n varchar,b date,sex varchar,ai varchar,r number);
procedure update1(c varchar,n number);
procedure delete1(c varchar);
procedure select1(n number);
end operations;
/* package body */
create or replace package body operations as
/* package main */
begin
operations.insert1('C234','Tom','03-JAN-79','Male','81K-90K',1);
operations.update1('C111','70K-80K');
operations.delete1('C112');
operations.select1(4);
end;
6. Create a database trigger that checks whether the new salary of employee is
less than existing salary. If so, raise an appropriate exception and avoid that
updation.
create or replace trigger t
before update on employee
for each row
begin
if :new.salary<:old.salary then
raise_application_error(-20015,'the new salary is less than the old
salary');
end if;
end;
package specification */
/* package main */
begin
packadd.add(4,5);
packadd.add('12','13');
packadd.add('10-JUN-2000',2);
end;