Lab Cycle 3 Solutions
Lab Cycle 3 Solutions
declare
n number:=&n;
i number;
j number;
s number;
begin
for i in 1 .. n loop
s:=0;
for j in 1..(i-1) loop
if(mod(i,j)=0) then
s:=s+j;
end if;
end loop;
if(s=i) then
dbms_output.put_line(i);
end if;
end loop;
end;
Output:
SQL> @Z:\PLSQL\perfect.sql
Input truncated to 1 characters
Enter value for n: 500
old 2: n number:=&n;
new 2: n number:=500;
6
28
496
INVENTORY
Item_ID Item_nam Quantity ROL ROQ
e
declare
id inventory.item_id%type := '&id';
q inventory.quantity%type;
rlinventory.rol%type;
rqinventory.roq%type;
n number;
begin
select quantity,rol,roq into q,rl,rq from inventory where item_id = id;
if q <rl then
n:=trunc((rl-q)/rq);
dbms_output.put_line('quantity is less than reorder level, place'||
n||' items');
else
dbms_output.put_line('quantity is sufficient');
end if;
end;
/
Output:
SQL> @ Z:\PLSQL\inv1.sql;
Enter value for id: I101
old 2: id inventory.item_id%type := '&id';
new 2: id inventory.item_id%type := 'I101';
SQL> @ Z:\PLSQL\inv1.sql;
Enter value for id: I102
old 2: id inventory.item_id%type := '&id';
new 2: id inventory.item_id%type := 'I102';
quantity is sufficient
3. Write a PL/SQL program to read two numbers, calculate their HCF and LCM
and store the details in the numbers table.
NUMBERS
Number Number HCF LCM
1 2
declare
a number := &a;
b number := &b;
g number;
l number;
a1 number;
b1 number;
begin
a1:=a;
b1:=b;
while (a <> b) loop
if (a < b) then
b := b - a;
else
a := a - b;
end if;
end loop;
g := a;
l:= (a1*b1)/g;
insert into demo_tab values(a1,b1,l,g);
end;
Output:
SQL> @prg1.sql;
22 /
Enter value for a: 4
old 2: a number := &a;
new 2: a number := 4;
Enter value for b: 8
old 3: b number := &b;
new 3: b number := 8;
N1 N2 LCM HCF
---------- ---------- ---------- ----------
4 8 8 4
Purchase_Record
Product_I Statu
Date
D s
Write a PL/SQL block to read the quantity of a product from inventory and
if it is > 0 reduce the quantity by 1 and record the status of purchase of that
product as ‘PURCHASED’. Otherwise record the status of purchase of that
product as ‘OUT OF STOCK’. While recording the status of a purchase,
record the date of transaction.
declare
id inven.pid%type := &id;
q inven.qty%type;
begin
select qty into q from inven where pid = id;
if q > 0 then
update inven set qty = qty - 1 where pid = id;
insert into pur_rec values(id,'PURCHASED',sysdate);
else
insert into pur_rec values(id,'OUTOFSTOCK',sysdate);
end if;
end;
output:
SQL>select * from invent;
SQL> @C:\Users\Administrator\Desktop\inven1.sql;
13 /
Enter value for id: 4
old 2: id inven.product_id%type := &id;
new 2: id inven.product_id%type := 4;
declare
s employee.ssn%type ;
n number := 10;
m number;
begin
select ssn into s from employee;
m := n/0;
select ssn into s from employee where dno = 100;
exception
when too_many_rows then
dbms_output.put_line('too many rows returned where only 1 is expected');
when zero_divide then
dbms_output.put_line('attempting to divide by zero');
when no_data_found then
dbms_output.put_line('no such department exists');
end;
SQL> @p34;
14 /
too many rows returned where only 1 is expected
SQL> @p34;
14 /
attempting to divide by zero
Output:
SQL> select * from employ;
SQL> @birth.sql;
22 /
Enter value for no: 103
old 2: no number:=&no;
new 2: no number:=103;
invalid birthdate
SQL> @birth.sql;
22 /
Enter value for no: 104
old 2: no number:=&no;
new 2: no number:=104;
valid birthdate
Enrollment
Student_I Coursei
Instructor_name
d d
Del_Audit
CourseI
Rows_deleted date
d
Write a PL/SQL block to delete records of all courses for which no
student is enrolled and then record the Course ID, no of rows deleted
and date on which deletion occurred in the Del_Audit table.
Output:
SQL> select * from course38;
COURSEID NAME OFFERING_DEPTNO
---------- -------------------- ---------------
CS221 CO 4
MATH101 DMS 2
CS223 DBMS 4
CS224 OS 3
CS225 CO 5
MATH101 DMS 3
6 rows selected.
SQL> @course1.sql;
15 /
CS225 1
MATH101 2
8. Write a PL/SQL block that displays the employee names and their total
earnings from the Emp Table.
Hint: Total earning of an employee = 12*(gross_salary+commission)
declare
s empcy2.sal%type;
name empcy2.ename%type;
c empcy2.comm%type;
cursor emp_cr is select ename,sal,comm from empcy2;
begin
open emp_cr;
loop
Output:
SQL>select * from empcy2;
SQL> /
14 rows selected.
SQL> @employ1.sql;
16 /
KING 60000
BLAKE 34200
CLARK 29400
JONES 35700
MARTIN 31800
ALLEN 22800
TURNER 18000
JAMES 11400
WARD 21000
FORD 36000
SMITH 9600
SCOTT 38400
ADAMS 13200
MILLER 15600
begin
update customer1 set rating=decode(annualincome,'Above
100K',1,'50K-100K',2,3);
end;
SQL> @C:\Users\Administrator\Desktop\rating1.sql;
4 /