0% found this document useful (0 votes)
39 views7 pages

Exp7 DBMS

This document contains 10 PL/SQL code blocks with examples of conditional and iterative statements to: 1. Check if a number is even or odd 2. Print all odd numbers from 1 to 100 3. Count the digits in a given number 4. Check if a number is prime 5. Print all prime numbers from 1 to 100 6. Compute the factorial of a given number 7. Reverse a given number and print it 8. Accept a student roll number and print their details from a student table 9. Accept an employee ID and print the details of their manager from an employee table 10. Accept a student roll number and print their average marks from a marks table
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views7 pages

Exp7 DBMS

This document contains 10 PL/SQL code blocks with examples of conditional and iterative statements to: 1. Check if a number is even or odd 2. Print all odd numbers from 1 to 100 3. Count the digits in a given number 4. Check if a number is prime 5. Print all prime numbers from 1 to 100 6. Compute the factorial of a given number 7. Reverse a given number and print it 8. Accept a student roll number and print their details from a student table 9. Accept an employee ID and print the details of their manager from an employee table 10. Accept a student roll number and print their average marks from a marks table
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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