0% found this document useful (0 votes)
13 views6 pages

Assingment 1

The document contains multiple PL/SQL blocks for managing student records in a database. It includes a block for inserting student details and calculating their class based on percentage, a block for retrieving and displaying a student's record with their grade based on percentage, and a block for checking two numbers and displaying odd numbers between them if both are positive. Each block utilizes different PL/SQL features such as variable declarations, conditionals, and exception handling.

Uploaded by

gauritilekar7
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)
13 views6 pages

Assingment 1

The document contains multiple PL/SQL blocks for managing student records in a database. It includes a block for inserting student details and calculating their class based on percentage, a block for retrieving and displaying a student's record with their grade based on percentage, and a block for checking two numbers and displaying odd numbers between them if both are positive. Each block utilizes different PL/SQL features such as variable declarations, conditionals, and exception handling.

Uploaded by

gauritilekar7
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/ 6

Set a 2.

Write a PL/SQL block which will accept student details, calculate the
class using per value and insert the record into Student (rno, sname,
class,
per, class) table.

CREATE TABLE Student (


rno NUMBER PRIMARY KEY,
sname VARCHAR2(100),
class VARCHAR2(20),
per NUMBER
);
DECLARE
v_rno NUMBER;
v_sname VARCHAR2(100);
v_per NUMBER;
v_class VARCHAR2(20);
BEGIN
-- Accepting student details (Replace with actual values)
v_rno := 101; -- Example Roll Number
v_sname := 'John Doe'; -- Example Student Name
v_per := 85; -- Example Percentage

-- Determining class based on percentage


IF v_per >= 75 THEN
v_class := 'Distinction';
ELSIF v_per >= 60 THEN
v_class := 'First Class';
ELSIF v_per >= 50 THEN
v_class := 'Second Class';
ELSIF v_per >= 35 THEN
v_class := 'Pass';
ELSE
v_class := 'Fail';
END IF;
-- Inserting record into the Student table
INSERT INTO Student (rno, sname, class, per)
VALUES (v_rno, v_sname, v_class, v_per);

-- Commit the transaction


COMMIT;

DBMS_OUTPUT.PUT_LINE('Record inserted successfully!');


END;
/
Set B 2.

Write a PL/SQL block which will accept roll number from


student, select
name and percentage of the student and calculate grade using
percentage
value. Display the record.(use %TYPE)

DECLARE
v_rno Student.rno%TYPE;
v_name Student.sname%TYPE;
v_per Student.per%TYPE;
v_grade VARCHAR2(15);
BEGIN
-- Accept user input for roll number
v_rno := &Enter_Roll_Number;

-- Retrieve the student's name and percentage


SELECT sname, per INTO v_name, v_per
FROM Student
WHERE rno = v_rno;

-- Calculate Grade Based on Percentage


IF v_per >= 75 THEN
v_grade := 'Distinction';
ELSIF v_per >= 60 THEN
v_grade := 'First Class';
ELSIF v_per >= 50 THEN
v_grade := 'Second Class';
ELSIF v_per >= 35 THEN
v_grade := 'Pass';
ELSE
v_grade := 'Fail';
END IF;

-- Display the Record


DBMS_OUTPUT.PUT_LINE('Student Record:');
DBMS_OUTPUT.PUT_LINE('Roll No: ' || v_rno);
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);
DBMS_OUTPUT.PUT_LINE('Percentage: ' || v_per);
DBMS_OUTPUT.PUT_LINE('Grade: ' || v_grade);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No student found with Roll No '
|| v_rno);
END;
/
Set B 1

Write a PL/SQL block which will accept roll number of a student and
display record of student from student table( use %ROWTYPE attribute)

DECLARE
v_rno Student.rno%TYPE;
v_student_rec Student%ROWTYPE; -- Declare variable with
%ROWTYPE
BEGIN
-- Accept user input for roll number
v_rno := &Enter_Roll_Number;

-- Retrieve the student's record


SELECT * INTO v_student_rec
FROM Student
WHERE rno = v_rno;

-- Display student details


DBMS_OUTPUT.PUT_LINE('Student Details:');
DBMS_OUTPUT.PUT_LINE('Roll No: ' || v_student_rec.rno);
DBMS_OUTPUT.PUT_LINE('Name: ' || v_student_rec.sname);
DBMS_OUTPUT.PUT_LINE('Class: ' || v_student_rec.class);
DBMS_OUTPUT.PUT_LINE('Percentage: ' ||
v_student_rec.per);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No student found with Roll No '
|| v_rno);
END;
/
Set a 3.
Write a PL/SQL block which will accept two numbers from
user, check
whether numbers are positive or negative. If positive number
then display
only the odd numbers between the entered numbers.

DECLARE
start_num NUMBER;
end_num NUMBER;
BEGIN
-- Accepting input from the user
start_num := &start_num;
end_num := &end_num;

-- Check if both numbers are positive


IF start_num > 0 AND end_num > 0 THEN
DBMS_OUTPUT.PUT_LINE('Odd numbers between ' ||
start_num || ' and ' || end_num || ':');

FOR i IN start_num..end_num LOOP


IF MOD(i, 2) <> 0 THEN
DBMS_OUTPUT.PUT_LINE(i);
END IF;
END LOOP;
ELSE
DBMS_OUTPUT.PUT_LINE('Both numbers should be
positive!');
END IF;
END;
/

You might also like