-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPL_SQL Cursor.txt
68 lines (67 loc) · 1.8 KB
/
PL_SQL Cursor.txt
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
create database class;
use class;
create table O_RollCall(roll_no int(3),name varchar(20));
create table N_RollCall(roll_no int(3),name varchar(20));
insert into O_RollCall values (1,'Himanshu');
insert into O_RollCall values (2,'Ram');
insert into O_RollCall values (3,'Soham');
insert into O_RollCall values (5,'Mohan');
insert into O_RollCall values (6,'Om');
insert into O_RollCall values (9,'Yash');
insert into O_RollCall values (11,'Mayur');
select * from O_RollCall;
select * from N_RollCall;
delimiter //
create procedure cursor_proc_p1()
begin
declare fin integer default 0;
declare old_roll int(3);
declare old_name varchar(20);
declare new_roll int(3);
declare old_csr cursor for select roll_no,name from O_RollCall;
declare new_csr cursor for select roll_no from N_RollCall;
declare continue handler for not found set fin=1;
open old_csr;
open new_csr;
ss:loop
fetch old_csr into old_roll,old_name;
fetch new_csr into new_roll;
if fin=1 then
leave ss;
end if;
if old_roll<>new_roll then
insert into N_RollCall values(old_roll,old_name);
end if;
end loop;
close old_csr;
close new_csr;
end //
create procedure cursor_proc_p2(in r1 int)
begin
declare r2 int;
declare exit_loop boolean;
declare c1 cursor for select roll_no from O_RollCall
where roll_no>r1;
declare continue handler for not found set
exit_loop=true;
open c1;
e_loop:loop
fetch c1 into r2;
if not exists(select * from N_RollCall where roll_no=r2)
then
insert into N_RollCall select * from O_RollCall where roll_no=r2;
end if;
if exit_loop
then
close c1;
leave e_loop;
end if;
end loop e_loop;
end;//
call cursor_proc_p2(5); //
select * from O_RollCall; //
select * from N_RollCall; //
call cursor_proc_p2(3); //
call cursor_proc_p1(); //
select * from O_RollCall; //
select * from N_RollCall; //