0% found this document useful (0 votes)
7 views

Cursor

The document describes creating a database table to store student records with fields for student number, name, subject scores, total score, percentage, and grade. It inserts data for 4 students and then defines a stored procedure to calculate the total, percentage, and grade for each student and update the table. The procedure is called and the updated student records are selected.

Uploaded by

Ankita Bhide
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Cursor

The document describes creating a database table to store student records with fields for student number, name, subject scores, total score, percentage, and grade. It inserts data for 4 students and then defines a stored procedure to calculate the total, percentage, and grade for each student and update the table. The procedure is called and the updated student records are selected.

Uploaded by

Ankita Bhide
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cursor

create table std1(rno int,name varchar(30),sub1 int, sub2 int,sub3 int,total int,percent int,grade varchar(20));
Query OK, 0 rows affected (0.92 sec)
mysql> insert into std1(rno,name,sub1,sub2,sub3) values(1,'Harry',68,70,65);Query OK, 1 row affected (0.14 sec)
mysql> insert into std1(rno,name,sub1,sub2,sub3)
values(2,'Hazel',72,85,82),(3,'carry',81,70,73),(4,'Ram',69,45,51);
mysql> select * from std1;
+ + + + + +
| rno | name | sub1 | sub2 | sub3 |
+ + + + + +
| 1 | Harry | 68 | 70 | 65 |
| 2 | Hazel | 72 | 85 | 82 |
| 3 | carry | 81 | 70 | 73 |
| 4 | Ram | 69 | 45 | 51 |
+ + + + + +

mysql> alter table std1 add total int;


mysql> alter table std1 add percentage int;
mysql> alter table std1 add grade varchar(30);
mysql> select * from std1;
+ + + + + + + + +
| rno | name | sub1 | sub2 | sub3 | total | percentage | grade |
+ + + + + + + + +
| 1 | Harry | 68 | 70 | 65 | NULL | NULL | NULL |
| 2 | Hazel | 72 | 85 | 82 | NULL | NULL | NULL |
| 3 | carry | 81 | 70 | 73 | NULL | NULL | NULL |
| 4 | Ram | 69 | 45 | 51 | NULL | NULL | NULL |
+ + + + + + + + +
create procedure sresult()
-> begin
-> declare res varchar(40);
-> declare pname varchar(40);
-> declare prno int(14);
-> declare psub1 int(14);
-> declare psub2 int(14);
-> declare psub3 int(14);
-> declare ptotal int(14);
-> declare ppercentage int(14);
-> declare pgrade varchar(40);
-> declare i int(14);
-> declare done int default false;
-> declare std_cursor cursor for select rno,name,sub1,sub2,sub3 from std1;
-> declare continue handler for not found set done=true;
-> set i=1;
-> open std_cursor;
-> readnextrow:loop
-> fetch std_cursor into prno,pname,psub1,psub2,psub3;
-> if done then
-> leave readnextrow;
-> end if;
-> set ptotal=psub1+psub2+psub3;
-> set ppercentage=ptotal/3;
-> if ppercentage>=70 then
-> set pgrade='distinction';
-> end if;
-> if ppercentage>=60 and ppercentage<70 then
-> set pgrade='first class';
-> end if;
-> if ppercentage>=50 and ppercentage<60 then
-> set pgrade='second class';
-> end if;
-> update std1 set total=ptotal,percentage=ppercentage,grade=pgrade where rno=i;
-> set i=i+1;
-> end loop;
-> close std_cursor;
-> end $
Query OK, 0 rows affected, 7 warnings (0.16 sec)

mysql> call sresult();


-> select * from std1;
-> delimiter $
Query OK, 0 rows affected (0.39 sec)

+ + + + + + + + +
| rno | name | sub1 | sub2 | sub3 | total | percentage | grade |
+ + + + + + + + +
| 1 | Harry | 68 | 70 | 65 | 203 | 68 | first class |
| 2 | Hazel | 72 | 85 | 82 | 239 | 80 | distinction |
| 3 | carry | 81 | 70 | 73 | 224 | 75 | distinction |
| 4 | Ram | 69 | 45 | 51 | 165 | 55 | second class |
+ + + + + + + + +

You might also like