Exp7 DBMS

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Programme : B.

Tech (ECM) Semester : Fall 2022-23


Database Management Systems
Course : Code : CSE2004
(Embedded Lab)
Faculty : Dr. R. Karthik Slot : L43 + L44

Ex. 7. PL/SQL – Part 1: Conditional and Iteration statements

Write PL/SQL blocks for the following:

1. To find if a given number is an even or odd number.


declare
i number;
r number;
begin
i:=&number1;
r:=MOD(i,2);

if r = 0 then
dbms_output.put_line('even');
else
dbms_output.put_line('odd');
end if;
end;
/
2. To print all odd numbers from 1 to 100.
declare
i number;
r number;
begin

for i in 1..100
loop
r:=MOD(i,2);
if r=1 then
dbms_output.put(i || ' ');

end if;
end loop;
dbms_output.put_line('');
end;
/

3. To count the number of digits in a given number.


declare
i number;
c number:=0;

begin
i := &number;
while i>=1
loop
c := c + 1;
i := floor(i/10);
end loop;
dbms_output.put_line(c);
end;
/
declare
i number(6);
x number(6):=0;
begin
i := &Number;
x:=length(i);
dbms_output.put_line(x);
end;

/
4. To check if the given number is a prime number.
declare
i number;
n number;
res number;
flag number := 0;
temp number;
begin
n := &number;
for i in 2..n-1 loop
temp := n/i;
res := temp*i;
if res = n then
flag := flag+1;
end if;
end loop;

if flag = 0 then
dbms_output.put_line('Prime');
else
dbms_output.put_line('Non-Prime');
end if;
end;
/
5. To print all prime numbers from 1 to 100.
declare
i number(6);
j number(6);
res number(6);
temp number(6);
flag number(6);
begin
for i in 2..100 loop
flag:=0;
for j in 2..i-1 loop
temp := i/j;
res := temp*j;
if res = i then
flag := flag+1;
end if;
end loop;
if flag = 0 then
dbms_output.put(i || ' ');
end if;
end loop;
dbms_output.put_line('');
end;
/

6. To compute the factorial of a given number.


declare
i number;
fact number:=1;
n number;

begin
n:=&number;
for i in 2..n loop
fact:=fact*i;
end loop;
dbms_output.put_line(fact);
end;
/
7. To reverse a given number and print it.
declare
i number;
rev number:=0;
last number;

begin
i:=&number;
while i>=1 loop
last := mod(i, 10);
rev := rev*10 + last;
i := floor(i/10);
end loop;
dbms_output.put_line(rev);
end;
/

8. Accept a student roll no as input and print his/her details – Refer Ex.1 for the student
schema.
declare
v_student Student_Details%RowType;
reg Student_Details.Register_Number%Type;

begin
reg:=&reg_no;
select * into v_student from student_details where register_number = reg;
dbms_output.put_line('Details of the student are');
dbms_output.put_line('Name: '||v_student.student_name);
dbms_output.put_line('Register Number: '||v_student.register_number);
dbms_output.put_line('School: '||v_student.school);
dbms_output.put_line('Course: '||v_student.course);
dbms_output.put_line('Contact Number: '||v_student.student_mobile);
dbms_output.put_line('Email: '||v_student.student_email);

end;
/
9. Accept an employee id as input and print his/her manager details – Refer Ex. 2 for the
employee schema.

declare
v_emp Manages%RowType;
eid Manages.emp_id%type;

begin
eid:=&emp_id;
select * into v_emp from Manages where emp_id = eid;

dbms_output.put_line('Details of Manager are');


dbms_output.put_line('Employee_Name: '||v_emp.employee_name);
dbms_output.put_line('Manager_Name: '||v_emp.manager_name);
end;
/
10. Accept a student roll no as input and print his/her average marks - Refer Ex.1 for the
student schema.
declare
v_marks Marks%RowType;
reg Marks.Register_Number%Type;
begin
reg := &Reg_Number;
select * into v_marks from marks
where register_number = reg;
dbms_output.put_line('Average mark of Student is '|| v_marks.total/5);
end;
/

You might also like