-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemp_cursor.sql
69 lines (58 loc) · 2.12 KB
/
emp_cursor.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
delimiter //
create procedure proc_copy()
begin
declare vfinished int default 0;
declare i int;
declare n varchar(10);
declare s decimal(6,2);
declare dg varchar(12);
declare dn int;
declare gr varchar(12);
declare emp_cursor cursor for select * from emp;
declare continue handler for not found set vfinished=1;
open emp_cursor;
getdata:loop
fetch emp_cursor into i,n,s,dg,dn,gr;
if vfinished=1 then
leave getdata;
end if;
insert into emp_dump values(i,n,s,dg,dn,gr);
end loop getdata;
close emp_cursor;
end;
//
/*
mysql> desc emp;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| eno | int | NO | PRI | NULL | |
| ename | varchar(12) | YES | | NULL | |
| esal | decimal(6,2) | YES | | NULL | |
| edesg | varchar(12) | YES | | NULL | |
| dno | int | YES | | NULL | |
| grade | varchar(12) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
mysql> create table emp_dump(eno int,ename varchar(12),esal decimal(6,2),edesg varchar(12),dno int,grade varchar(12));
Query OK, 0 rows affected (0.68 sec)
mysql> source /home/student/emp_cursor.sql
Query OK, 0 rows affected (0.14 sec)
mysql> call proc_copy();
-> //
Query OK, 0 rows affected (0.87 sec)
mysql> select * from emp_dump;
-> //
+------+-------+---------+-----------+------+--------+
| eno | ename | esal | edesg | dno | grade |
+------+-------+---------+-----------+------+--------+
| 101 | Sai | 1234.12 | Prof | 123 | First |
| 102 | Rahul | 3223.21 | Prof | 103 | Second |
| 256 | Rushi | 4321.34 | Prof | 323 | NULL |
| 265 | yash | 1256.23 | proffesor | 125 | second |
| 432 | abhi | 7654.00 | assoc | 21 | first |
| 562 | Sai | 2343.34 | Asst | 345 | First |
| 652 | Sai | 5454.65 | Assoc | 342 | Two |
+------+-------+---------+-----------+------+--------+
7 rows in set (0.00 sec)
*/