Anda di halaman 1dari 24

LAB EXERCISE 10

PL/SQL (BASICS)
1. Write a PL/SQL block to find the area of the circle for radius=4. Also the radius
and the new area should be inserted into a table called Circle.
Sol.:

1.circle_area

declare
v_radius number(5,2):=1;
v_area number(8,2):=1;
c_pi constant number(3,2):=3.14;

begin
v_radius:= &V_radius;
V_area := c_pi*power(v_radius,2);
insert into circle values(v_radius, V_area);

end;

2. Write a PL/SQL block to find the area of the circle for different radius until
area is less than 200. Also the radius and the new area should be inserted
into a table called Different_Area. Solve this question using all the three
loops separately.

Simple Loop
If-then Loop
While Loop

(a) different_area

declare
v_radius number(5,2):=1;
v_area number(8,2):=1;
c_pi constant number(3,2):=3.14;

begin
v_radius:= 4;
Loop
V_area := c_pi*power(v_radius,2);
insert into different_circle values(v_radius, V_area);
v_radius:=v_radius+1;
exit when v_radius=10;
end loop;
end;

(b) diff_area

declare
v_radius number(5,2):=1;
v_area number(8,2):=1;
c_pi constant number(3,2):=3.14;

begin
v_radius:= 4;
while (v_radius<=10)
Loop
V_area := c_pi*power(v_radius,2);
insert into different_circle1 values(v_radius, V_area);
v_radius:=v_radius+1;
end loop;
end;

(c) diff_area1

declare
v_radius number(5,2):=1;
v_area number(8,2):=1;
c_pi constant number(3,2):=3.14;

begin
v_radius:= 4;
while (v_radius<=10)
Loop
V_area := c_pi*power(v_radius,2);
insert into different_circle1 values(v_radius, V_area);
v_radius:=v_radius+1;
end loop;
end;

LAB EXERCISE 11
PL/SQL(CURSORS)
1. Write a PL/SQL block to display the emp_name, designation and
dept_no from Employee table where dept_no=10.

Solution:

declare
em employee%rowtype;

begin
select * into em from employee where dept_no=10;
dbms_output.put_line('Employee name:'||em.emp_name ||'
Designation:'||em.designation ||' Dept_no:' || em.dept_no);
end;



2. Write a PL/SQL block to update the value of City in Employee table
using IMPLICIT CURSOR attributes:
%FOUND
%NOTFOUND
%ROWCOUNT

And EXPLICIT CURSOR attributes:
%NOTFOUND
%ROWCOUNT

Solution:
/*-----------------Implict cursor---------------*/
declare
row number(4);
begin
update employee set dept_loc='&city' where emp_no=&emp_no;
if sql%found then
dbms_output.put_line('Sussesfully updated');
end if;
if sql%notfound then
dbms_output.put_line('Employee not found');
end if;
row:=sql%rowcount;
if sql%rowcount>0 then
dbms_output.put_line(row ||' :Employee updated');
end if;
end;

/*-------------------Explicit cursor--------------------*/
declare

cursor cr_emp is select emp_no from employee where emp_no=&emp_no;
v_emp_no employee.emp_name%type;
row number(5);

begin

open cr_emp;

fetch cr_emp into v_emp_no;
if (cr_emp%notfound) then
dbms_output.put_line('Employee not found');
end if;
row:=cr_emp%rowcount;
if(cr_emp%rowcount>0)then
update employee set dept_loc='&loc' where emp_no=v_emp_no;
dbms_output.put_line(row || ' :row updated');
end if;

close cr_emp;
end;

3. Write a program to display the value of
dept_name for a given dept_no. The
dept_no should be used as a parameter for
the display of the dept_name.

declare
cursor c_dep(v_dep_no number)is
select dept_no,dept_loc from
employee where
dept_no=v_dep_no;
v_dep_no employee.dept_no%type;
v_dep_loc employee.dept_loc%type;
begin
v_dep_no:=&dep_no;
open c_dep(v_dep_no);
loop
fetch c_dep into
V_dep_no,v_dep_loc;
if (c_dep%notfound and
c_dep%rowcount>0) then
dbms_output.put_line('Last
employee');
exit;
else


if(c_dep%notfound)then
dbms_output.put_line('no
employee');
end if;
exit when c_dep%notfound;
dbms_output.put_line(v_dep_no ||'
'||v_dep_loc);
end if;
end loop;
close c_dep;
end;
4. Write a PL/SQL code block to display the following report in the format
given below. Given Table is Emp (Empno, name, deptno, salary,
date_of_join):

Years of service No. of persons
<1 ----
>=1 and <5 ----
>=5 and <10 ----
>=10 ----

declare
cursor c_rep is select
emp_no,date_of_join from
employee;
doj date;
e_no number(5);
n1 number(5):=0;
n2 number(5):=0;
n3 number(5):=0;
n4 number(5):=0;
begin
open c_rep;
loop
fetch c_rep into e_no,doj;
exit when c_rep%notfound;
if((sysdate-doj)/365<1)then
n1:=n1+1;
end if;

if((sysdate-doj)/365>=1 and (sysdate-doj)/365<5
)then
n2:=n2+1;
end if;
if((sysdate-doj)/365>=5 and (sysdate-doj)/365<10
)then
n3:=n3+1;
end if;
if((sysdate-doj)/365>=10 )then
n4:=n4+1;
end if;
end loop;
dbms_output.put_line('Years of services No
of person');

dbms_output.put_line('<1 ' ||n1);
dbms_output.put_line('>=1 and <5 ' ||n2);
dbms_output.put_line('>=5 and <10 ' ||n3);
dbms_output.put_line('>=10 ' ||n4);
close c_rep;
end;

5. Write a PL/SQL code block to display the following report
department wise in the format given below based on the
given table:
Emp(Empno, name, deptno, salary, date_of_join)

For Department Number: ----

Salary No. of persons
<1000 ----
>=1000 and <5000 ----
>=5000 and < 10000 ----
>=10000 ----
Total ----

Given Table

Item_master(icode, description, balstock)
Item_tran(icode, tran_type, qty)

create table item_master(icode
varchar2(15)primary key check(icode
like 'I%'),
description varchar2(10),
balstock number(5)not null);


create table item_tran(icode
varchar2(15)primary key check(icode
like 'I%'),
tran_type varchar2(6 )not null
check(tran_type in('I','D','U')),
qty number(5)not null);


declare
cursor c_item is select
icode,tran_type,qty from item_tran;
icode varchar2(15);
type1 varchar2(5);
qty1 number(5);


begin
open c_item;
loop
fetch c_item into icode,type1,qty1;
exit when c_item%notfound;
if(type1='I')then
insert into item_master
values(icode,'&description',qty1);
end if;
end loop;
close c_item;
end;

LAB EXERCISE 12
PL/SQL (PROCEDURES)
1. Write a PL/SQL procedure in which you have to
accept salary from the user. Based on the
following range of salary the employee will
receive his/her D.A:

Salary D.A
>1000 and < 3000 5%
>3000 and <5000 10%
>5000 and < 10000 15%

The D.A which the employees will receive based
on their salary should be notified to them
through proper messages.

create or replace procedure da(sal in
number)as
vsal number(5);

begin
vsal:=sal;

if(vsal>1000 and vsal<3000)then
dbms_output.put_line('5% da');
else
if(vsal>3000 and vsal<5000)then
dbms_output.put_line('10% da');
else
if(vsal>5000 and vsal<10000)then
dbms_output.put_line('15% da');
else
dbms_output.put_line('Input is
not valid');



end if;
end if;
end if;

end;

declare
sal number(5);
begin
sal:=&salary;
da(sal);
end;
2. Write a PL/SQL procedure to
calculate total salary of the
employees from the Employee
table. Display total salary,
Dept_name and Designation if
Emp_no is given.

create or replace procedure
ts(empno1 in number)as
sal1 number(5);
dep emp.deptno%type;
des emp.job%type;
begin
select sal into sal1 from emp where
empno=empno1;
if(sal1>1000 and sal1<3000)then
sal1:=sal1+(sal1*5)/100;
else
if(sal1>3000 and sal1<5000)then
sal1:=sal1+(sal1*10)/100;
else
if(sal1>5000 and sal1<10000)then
sal1:=sal1+(sal1*15)/100;
end if;
end if;
end if;

select deptno,job into dep,des from
emp where empno=empno1;
dbms_output.put_line('Total salary
dept number designation');
dbms_output.put_line(sal1 ||'
'||dep||' '||des);
end;


declare
emp number(4);
begin
emp:=&emp_no;
ts(emp);
end;

Create the Branch_Summary table
with the following specification:

Column Name Data type and Size
Branch_City Varchar2(15)
Reserv_Count Number(3)
Reserv_Amt Number(8)
Cancel_Count Number(3)
Cancel_Amt Number(8)
Create a procedure which receives branch code as a
parameter and calculates the total collection made due to
reservation and lost due to cancellation. It should also
count the total number of reservations and cancellations.

create or replace procedure
upd_sal(id in varchar2,min_sal in
number,max_sal in number)as
u1 exception;
begin
if(min_sal>max_sal)then
raise u1;
end if;
update jobs set
min_salary=min_sal,max_salary=
max_sal where job_id=id;
exception
when no_data_found then
dbms_output.put_line('id not
persent');
when u1 then
dbms_output.put_line('minimum
salary is grater then maximum
salary');
end;



declare
id number(4);
min_s jobs.min_salary%type;
max_s number(8);
begin
id:=&jobs_id;
min_s:=&minimum_salary;
max_s:=&maximum_salary;
upd_sal(id,min_s,max_s);
end;

Anda mungkin juga menyukai