0% found this document useful (0 votes)
47 views2 pages

Ex 6

The document contains 4 examples of PL/SQL code. The first creates a procedure that counts the number of students in different grade ranges based on their average marks in a student_marks table. The second creates a procedure to output each student's total marks. The third creates a function to add two numbers and demonstrates calling it. The fourth creates a function to return the total salary for a given department number and demonstrates calling it.

Uploaded by

Barath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views2 pages

Ex 6

The document contains 4 examples of PL/SQL code. The first creates a procedure that counts the number of students in different grade ranges based on their average marks in a student_marks table. The second creates a procedure to output each student's total marks. The third creates a function to add two numbers and demonstrates calling it. The fourth creates a function to return the total salary for a given department number and demonstrates calling it.

Uploaded by

Barath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

EX.

1. Create a procedure to find the number of students ranging from 100- 70%, 69-60%,
59-50% &
below 49% in each course from the student_marks table given by the procedure as
parameter.

create table student_marks ( Reg_no number(5) Primary key, sname varchar(15), c1


number(3), c2 number(3), c3 number(3));

insert into student_marks values(7170, 'Sam', 90, 80, 70);


insert into student_marks values(7171, 'Ram', 40, 50, 60);
insert into student_marks values(7172, 'Jack', 60, 30, 80);
insert into student_marks values(7173, 'kacl', 80, 45, 70);
insert into student_marks values(7174, 'farck', 49, 50, 89);

CREATE OR REPLACE PROCEDURE students_count IS


p_count_1 NUMBER := 0;
p_count_2 NUMBER := 0;
p_count_3 NUMBER := 0;
p_count_4 NUMBER := 0;
p_avg NUMBER;
BEGIN
FOR rec IN (SELECT reg_no FROM student_marks) LOOP
-- Calculate the average of the three marks for each student
SELECT AVG(c1 + c2 + c3) / 3 INTO p_avg
FROM student_marks
WHERE reg_no = rec.reg_no;

IF p_avg BETWEEN 70 AND 100 THEN


p_count_1 := p_count_1 + 1;
ELSIF p_avg BETWEEN 60 AND 69 THEN
p_count_2 := p_count_2 + 1;
ELSIF p_avg BETWEEN 50 AND 59 THEN
p_count_3 := p_count_3 + 1;
ELSIF p_avg BETWEEN 0 AND 49 THEN
p_count_4 := p_count_4 + 1;
END IF;
END LOOP;

-- Display the results


DBMS_OUTPUT.PUT_LINE('No. of Students ranging from 100-70: ' || p_count_1);
DBMS_OUTPUT.PUT_LINE('No. of Students ranging from 69-60: ' || p_count_2);
DBMS_OUTPUT.PUT_LINE('No. of Students ranging from 59-50: ' || p_count_3);
DBMS_OUTPUT.PUT_LINE('No. of Students ranging from 49-0: ' || p_count_4);
END;
/

SET SERVEROUTPUT ON;


EXEC students_count;

2.Write a procedure to calculate the total marks secured in three subjects of a


student.

create or replace procedure total_marks is


p_tot number:=0;
begin
for rec in (select * from student_marks) loop
p_tot := rec.c1+rec.c2+rec.c3;
dbms_output.put_line('REG_NO:'||rec.reg_no||', Name:'||rec.sname||',
Total:'||p_tot);
end loop;
end;
/
set serverout on;
exec total_marks;

3. Create a function that accepts 2 numbers and returns the addition of passed
values. Also write
the code to call your function.

CREATE OR REPLACE FUNCTION add_numbers( a IN NUMBER, b IN NUMBER) RETURN NUMBER IS


result NUMBER;
BEGIN
result := a + b;
RETURN result;
END add_numbers;
/

DECLARE
v_a NUMBER := &v_a;
v_b NUMBER := &v_b;
v_sum_result NUMBER;
BEGIN
v_sum_result := add_numbers(v_a, v_b);
DBMS_OUTPUT.PUT_LINE('Sum Result: ' || v_sum_result);
END;
/

4.Write a PL/SQL function that accepts department number and returns the total
salary of the
department. Also write a function to call the function.

create or replace function tot_salary(dnumber in number) return number is


v_tot_salary number := 0;

begin
select sum(salary) into v_tot_salary from employee where dno = dnumber;
return v_tot_salary;
end tot_salary;
/

DECLARE
dnumber NUMBER := &dnumber;
v_tot_salary NUMBER;
BEGIN
v_tot_salary := tot_salary(dnumber);

DBMS_OUTPUT.PUT_LINE('Total Salary of the Department-'||dnumber||': ' ||


v_tot_salary);
END;
/

You might also like