Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

LabCycle – 4 Solutions

1. Implement a PL/SQL stored procedure for calculating ncr using fact(m), a


recursive function that returns factorial of m.

/* 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 */

3. Write a PL/SQL block that updates salary of an employee in employee table


by using incr function which takes employee number as argument, calculates
increment and returns increment based on the following criteria.
 If salary <= 3000 – increment = 30% of salary
 If salary > 3000 and <= 6000– increment = 20% of salary
 Else increment = 10% of salary.

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;
/

3. Write a PL/SQL block that updates salary of an employee in employee table


by using incr function which takes employee number as argument, calculates
increment and returns increment based on the following criteria.
 If salary <= 3000 – increment = 30% of salary
 If salary > 3000 and <= 6000– increment = 20% of salary
 Else increment = 10% of salary.

/* 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;

5. Write a PL/SQL code to create, a) Package specification b) Package body.


For the insert, update, delete and select operations on the following customer
table.
Cust_ID Name BirthDate Sex AnnualIncom Rating
e
C111 Alex 20-Aug-98 Male Above 100K 3
C112 Wayne 15-Nov-85 Male 70k-80k 2
C234 Tom 03-Jan-79 Male 80k-90k 1

/* 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

procedure insert1(c varchar,n varchar,b date,sex varchar,ai varchar,r number) is


begin
insert into cust values(c,n,b,sex,ai,r);
end insert1;

procedure update1(c varchar,n number) is


customer_id cust.cust_id%type:=c;
new_salary cust.annualincome%type:=n;
begin
update cust set annualincome=new_salary where
cust_id=customer_id;
end update1;

procedure delete1(c varchar) is


customer_id cust.cust_id%type:=c;
begin
delete from cust where cust_id=customer_id;
end delete1;

procedure select1(n number) is


customer_id cust.cust_id%type;
name cust.name%type;
birth cust.birthdate%type;
sex cust.sex%type;
ai cust.annualincome%type;
rating cust.rating%type;
cursor temp is select * from cust;
begin
open temp;
loop
fetch temp into customer_id,name,birth,sex,ai,rating;
exit when temp%notfound;
dbms_output.put_line(customer_id||' '||name||' '||birth||' '||sex||' '||ai||' '||rating);
end loop;
close temp;
end select1;
end operations;

/* 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;

7. Write PL/SQL program to create a Package that implements function


overloading of following functions:
 function add(x number, y number) return number;
 function add(x varchar2, y varchar2) return varchar2;
 function add(x date, y number) return date;

package specification */

create or replace package packadd as


function add(x number,y number) return number;
function add(x varchar,y varchar) return varchar;
function add(x date,y number) return date;
end packadd;
/* package body */

create or replace package body packadd as

function add(x number,y number) return number is


z number;
begin
z:=x+y;
return z;
end add;

function add(x varchar,y varchar) return varchar is


z varchar(10);
begin
z:=x+y;
return z;
end add;
function add(x date,y number) return date is
z date;
begin
z:=x+y;
return z;
end add;
end packadd;

/* package main */

begin
packadd.add(4,5);
packadd.add('12','13');
packadd.add('10-JUN-2000',2);
end;

Consider the following tables


PERSINFO
EMPN NAM AG
O E E
AUDITPERSINFO
EMPN NAM AG OPERATIO ODAT
O E E N E
PERSINFO is the table for which the auditing must be performed and
AUDITPERSINFO is the table which keeps track of the records deleted
or modified. Create a database trigger audit_trial. This trigger is forced
when an UPDATE or a DELETE is performed on the table PERSINFO.
It first checks for the operation being performed on the table. Then
depending on the operation, a variable(that corresponds to operation) is
assigned the value ‘UPDATE’ or ‘DELETE’ and then inserts the
updated/deleted record into AUDITPERSINFO.

create or replace trigger audit_trail


before update or delete on persinfo
begin
if updating then
insert into audirpersinfo
values(:old.empno,:old.name,:old.age,'update',sysdate);
elsif deleting then
insert into audirpersinfo
values(:old.empno,:old.name,:old.age,'delete',sysdate);
end if;
end;

You might also like