code and steps

advertisement
STEP 1 : Create table :
SQL> create table stud( sno number primary key, sname char(15), sub1 number,sub2 number, sub3
number,grade char(15));
Table created.
STEP 2 : Insert the records in table named stud :
The last field of grade is left blank since it would be calculated later using procedure.
SQL> insert into stud values(1,'Pray',98,94,90,' ');
1 row created.
SQL> insert into stud values(2,'Jay',57,74,40,' ');
1 row created.
SQL> insert into stud values(3,'Prisha',58,54,50,' ');
1 row created.
SQL> insert into stud values(4,'Masum',48,44,40,' ');
1 row created.
SQL> insert into stud values(5,'Shyam',40,40,40,' ');
1 row created.
STEP 3 : View the records in table named stud :
SQL> select * from stud;
SNO SNAME
SUB1
SUB2
SUB3
GRADE
---------- --------------- ---------- ---------- ---------- --------------1 Pray
98
94
90
2 Jay
57
74
40
3 Prisha
58
54
50
4 Masum
48
44
40
5 Shyam
40
40
40
STEP 4 : Create a function named givegrade which checks conditions and returns
the grade in variable named g:
create or replace function givegrade(p in number) return char is g char(15);
begin
if p >= 70 then
g:='Distinction';
return g;
elsif p >= 60 then
g:='First';
return g;
elsif p>= 50 then
g:='Pass';
return g;
else
g:='Fail';
return g;
end if;
end;
/
STEP 5 : Write the following procedure that will update the grade in the table :
> declare
cursor c_grade is select sno,sub1,sub2,sub3 from stud;
no stud.sno%type;
s1 stud.sub1%type;
s2 stud.sub2%type;
s3 stud.sub3%type;
t number;
g stud.grade%type;
p number(10,2);
begin
open c_grade;
loop
fetch c_grade into no,s1,s2,s3;
exit when c_grade%notfound;
t:=s1+s2+s3;
p:=t/3;
g:=givegrade(p);
update stud set grade=g where sno=no;
end loop;
close c_grade;
end;
/
SQL procedure successfully completed.
STEP 5 : View the updated table named stud which shows grades of students.
select * from stud;
SNO SNAME
SUB1
SUB2
SUB3 GRADE
----- --------------- ---------- ---------- ---------- --------------1 Pray
98
94
90
Distinction
2 Jay
57
74
40
Pass
3 Prisha
58
54
50
Pass
4 Masum
48
44
40
Fail
5 Shyam
40
40
40
Fail
Download