您好,登錄后才能下訂單哦!
一、游標的定義:
create procedure p12()
begin
declare row_name varchar(20);
declare row_num int;
declare myCursor cursor for select name,num from goods;//定義游標myCursor
open myCursor;//打開游標myCursor
fetch myCursor into row_name,row_num;//使用游標myCursor獲取第一行
select row_name, row_num;
fetch myCursor into row_name,row_num;//使用游標myCursor獲取第二行;每fetch一次游標就自動往下游一次.
select row_name, row_num;
close myCursor;//關閉游標myCursor
end;
二、游標+repeat循環-->實現遍歷行:
create procedure p13()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare row_count int;
declare i int default 0;
declare myCursor cursor for select gid,name,num from goods;
select count(1) into row_count from goods;
open myCursor;
repeat
fetch myCursor into row_gid,row_name,row_num;
select row_gid,row_name,row_num;
set i=i+1;
until i>row_count end repeat;
close myCursor;
end;
三、游標+continue handler實現遍歷行:
continue handler 當fetch觸發此handler后,后面的語句繼續執行。
所以會多執行一次select row_gid,row_name,row_num;
此handler常用。
create procedure p15()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare you int default 1;
declare myCursor cursor for select gid,name,num from goods;
declare continue handler for NOT FOUND set you=0;
open myCursor;
repeat
fetch myCursor into row_gid,row_name,row_num;
select row_gid,row_name,row_num;
until you=0 end repeat;
close myCursor;
end;
四、游標+exit handler實現遍歷行:
exit handler 當fetch觸發此handler后,觸發后后面的語句不再執行。
所以select row_gid,row_name,row_num;不會再被執行。
此handler不常用。
create procedure p16()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare you int default 1;
declare myCursor cursor for select gid,name,num from goods;
declare exit handler for NOT FOUND set you=0;
open myCursor;
repeat
fetch myCursor into row_gid,row_name,row_num;
select row_gid,row_name,row_num;
until you=0 end repeat;
close myCursor;
end;
五、游標+while實現遍歷行:
create procedure p15()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare over int default 1;
declare myCursor cursor for select gid,name,num from goods;
declare continue handler for NOT FOUND set over =0;
open myCursor;
fetch myCursor into row_gid,row_name,row_num;
while over do
select row_gid,row_name,row_num;
fetch myCursor into row_gid,row_name,row_num;
end while;
close myCursor;
end;
六、游標+loop實現遍歷行:
-- loop 與 leave,iterate 實現循環
-- loop 標志位無條件循環;leave 類似于Java break 語句,跳出循環,即跳出 begin end;
iterate 類似于java continue ,結束本次循環,繼續下一次循環。
--loop的優點在于可以根據條件結束本次循環或者根據條件跳出循環。
create procedure p17()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare over int default 0;
declare myCursor cursor for select gid,name,num from goods;
declare continue handler for NOT FOUND set over=1;
open myCursor;
cursor_loop:loop
fetch myCursor into row_gid,row_name,row_num;
if over then
leave cursor_loop;
end if;
select row_gid,row_name,row_num;
end loop cursor_loop;
close myCursor;
end;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。