2025-04-23

MySql中遊標的設定與三種使用方式。

創建遊標

首先在MySql中創建一張數據表:

CREATE TABLE IF NOT EXISTS `store` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(20) NOT NULL,

`count` int(11) NOT NULL DEFAULT '1',

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;

INSERT INTO `store` (`id`, `name`, `count`) VALUES

(1, 'android', 15),

(2, 'iphone', 14),

(3, 'iphone', 20),

(4, 'android', 5),

(5, 'android', 13),

(6, 'iphone', 13);

我們現在要用存儲過程做一個功能,統計iphone的總庫存是多少,並把總數輸出到控制臺。

–在windows系統中寫存儲過程時,如果需要使用declare聲明變量,需要添加這個關鍵字,否則會報錯。

delimiter //

drop procedure if exists StatisticStore;

CREATE PROCEDURE StatisticStore()

BEGIN

–創建接收遊標數據的變量

declare c int;

declare n varchar(20);

–創建總數變量

declare total int default 0;

–創建結束標志變量

declare done int default false;

–創建遊標

declare cur cursor for select name,count from store where name = 'iphone';

–指定遊標循環結束時的返回值

declare continue HANDLER for not found set done = true;

–設置初始值

set total = 0;

–打開遊標

open cur;

–開始循環遊標裡的數據

read_loop:loop

–根據遊標當前指向的一條數據

fetch cur into n,c;

–判斷遊標的循環是否結束

if done then

leave read_loop; –跳出遊標循環

end if;

–獲取一條數據時,將count值進行累加操作,這裡可以做任意你想做的操作,

set total = total + c;

–結束遊標循環

end loop;

–關閉遊標

close cur;

–輸出結果

select total;

END;

–調用存儲過程

call StatisticStore();

fetch是獲取遊標當前指向的數據行,並將指針指向下一行,當遊標已經指向最後一行時繼續執行會造成遊標溢出。

使用loop循環遊標時,他本身是不會監控是否到最後一條數據瞭,像下面代碼這種寫法,就會造成死循環;

read_loop:loop

fetch cur into n,c;

set total = total+c;

end loop;

在MySql中,造成遊標溢出時會引發mysql預設定的NOT FOUND錯誤,所以在上面使用下面的代碼指定瞭當引發not found錯誤時設定一個continue 的事件,指定這個事件發生時修改done變量的值。

declare continue HANDLER for not found set done = true;[sql] view plain copy

–判斷遊標的循環是否結束

if done then

leave read_loop; –跳出遊標循環

end if;

如果done的值是true,就結束循環。繼續執行下面的代碼。

使用方式

遊標有三種使用方式:

第一種就是上面的實現,使用loop循環;

第二種方式如下,使用while循環:

drop procedure if exists StatisticStore1;

CREATE PROCEDURE StatisticStore1()

BEGIN

declare c int;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name,count from store where name = 'iphone';

declare continue HANDLER for not found set done = true;

set total = 0;

open cur;

fetch cur into n,c;

while(not done) do

set total = total + c;

fetch cur into n,c;

end while;

close cur;

select total;

END;

call StatisticStore1();

第三種方式是使用repeat執行:

drop procedure if exists StatisticStore2;

CREATE PROCEDURE StatisticStore2()

BEGIN

declare c int;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name,count from store where name = 'iphone';

declare continue HANDLER for not found set done = true;

set total = 0;

open cur;

repeat

fetch cur into n,c;

if not done then

set total = total + c;

end if;

until done end repeat;

close cur;

select total;

END;

call StatisticStore2();

遊標嵌套

在mysql中,每個begin end 塊都是一個獨立的scope區域,由於MySql中同一個error的事件隻能設定一次,如果多設定的話在編譯時會提示Duplicate handler declared in the same block。

drop procedure if exists StatisticStore3;

CREATE PROCEDURE StatisticStore3()

BEGIN

declare _n varchar(20);

declare done int default false;

declare cur cursor for select name from store group by name;

declare continue HANDLER for not found set done = true;

open cur;

read_loop:loop

fetch cur into _n;

if done then

leave read_loop;

end if;

begin

declare c int;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name,count from store where name = 'iphone';

declare continue HANDLER for not found set done = true;

set total = 0;

open cur;

iphone_loop:loop

fetch cur into n,c;

if done then

leave iphone_loop;

end if;

set total = total + c;

end loop;

close cur;

select _n,n,total;

end;

begin

declare c int;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name,count from store where name = 'android';

declare continue HANDLER for not found set done = true;

set total = 0;

open cur;

android_loop:loop

fetch cur into n,c;

if done then

leave android_loop;

end if;

set total = total + c;

end loop;

close cur;

select _n,n,total;

end;

begin

end;

end loop;

close cur;

END;

call StatisticStore3();[sql] view plain copy

set @sqlStr='select * from table where condition1 = ?';

prepare s1 for @sqlStr;

–如果有多個參數用逗號分隔

execute s1 using @condition1;

–手工釋放,或者是 connection 關閉時, server 自動回收

deallocate prepare s1;

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *