佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 詳解Mysql 游標(biāo)的用法及其作用

詳解Mysql 游標(biāo)的用法及其作用

熱門標(biāo)簽:呼叫中心市場(chǎng)需求 AI電銷 地方門戶網(wǎng)站 網(wǎng)站排名優(yōu)化 Linux服務(wù)器 服務(wù)外包 鐵路電話系統(tǒng) 百度競(jìng)價(jià)排名

[mysql游標(biāo)的用法及作用]

例子:

當(dāng)前有三張表A、B、C其中A和B是一對(duì)多關(guān)系,B和C是一對(duì)多關(guān)系,現(xiàn)在需要將B中A表的主鍵存到C中;
常規(guī)思路就是將B中查詢出來然后通過一個(gè)update語句來更新C表就可以了,但是B表中有2000多條數(shù)據(jù),
難道要執(zhí)行2000多次?顯然是不現(xiàn)實(shí)的;最終找到寫一個(gè)存儲(chǔ)過程然后通過循環(huán)來更新C表,
然而存儲(chǔ)過程中的寫法用的就是游標(biāo)的形式。

【簡(jiǎn)介】

游標(biāo)實(shí)際上是一種能從包括多條數(shù)據(jù)記錄的結(jié)果集中每次提取一條記錄的機(jī)制。

​ 游標(biāo)充當(dāng)指針的作用。

​ 盡管游標(biāo)能遍歷結(jié)果中的所有行,但他一次只指向一行。

​ 游標(biāo)的作用就是用于對(duì)查詢數(shù)據(jù)庫所返回的記錄進(jìn)行遍歷,以便進(jìn)行相應(yīng)的操作。

【用法】

 一、聲明一個(gè)游標(biāo): declare 游標(biāo)名稱 CURSOR for table;(這里的table可以是你查詢出來的任意集合)
​ 二、打開定義的游標(biāo):open 游標(biāo)名稱;
​ 三、獲得下一行數(shù)據(jù):FETCH 游標(biāo)名稱 into testrangeid,versionid;
​ 四、需要執(zhí)行的語句(增刪改查):這里視具體情況而定
​ 五、釋放游標(biāo):CLOSE 游標(biāo)名稱;

注:mysql存儲(chǔ)過程每一句后面必須用;結(jié)尾,使用的臨時(shí)字段需要在定義游標(biāo)之前進(jìn)行聲明。

【實(shí)例】

- 
BEGIN 
 
--定義變量 
declare testrangeid BIGINT; 
declare versionid BIGINT; 
declare done int; 
--創(chuàng)建游標(biāo),并存儲(chǔ)數(shù)據(jù) 
declare cur_test CURSOR for 
 select id as testrangeid,version_id as versionid from tp_testrange; 
--游標(biāo)中的內(nèi)容執(zhí)行完后將done設(shè)置為1 
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; 
--打開游標(biāo) 
open cur_test; 
--執(zhí)行循環(huán) 
 posLoop:LOOP 
--判斷是否結(jié)束循環(huán) 
  IF done=1 THEN 
  LEAVE posLoop; 
 END IF; 
--取游標(biāo)中的值 
 FETCH cur_test into testrangeid,versionid; 
--執(zhí)行更新操作 
 update tp_data_execute set version_id=versionid where testrange_id = testrangeid; 
 END LOOP posLoop; 
--釋放游標(biāo) 
CLOSE cur_test; 
 
END 
-

例子2:

我們現(xiàn)在要用存儲(chǔ)過程做一個(gè)功能,統(tǒng)計(jì)iphone的總庫存是多少,并把總數(shù)輸出到控制臺(tái)。

--在windows系統(tǒng)中寫存儲(chǔ)過程時(shí),如果需要使用declare聲明變量,需要添加這個(gè)關(guān)鍵字,否則會(huì)報(bào)錯(cuò)。 
delimiter // 
drop procedure if exists StatisticStore; 
CREATE PROCEDURE StatisticStore() 
BEGIN 
 --創(chuàng)建接收游標(biāo)數(shù)據(jù)的變量 
 declare c int; 
 declare n varchar(20); 
 --創(chuàng)建總數(shù)變量 
 declare total int default 0; 
 --創(chuàng)建結(jié)束標(biāo)志變量 
 declare done int default false; 
 --創(chuàng)建游標(biāo) 
 declare cur cursor for select name,count from store where name = 'iphone'; 
 --指定游標(biāo)循環(huán)結(jié)束時(shí)的返回值 
 declare continue HANDLER for not found set done = true; 
 --設(shè)置初始值 
 set total = 0; 
 --打開游標(biāo) 
 open cur; 
 --開始循環(huán)游標(biāo)里的數(shù)據(jù) 
 read_loop:loop 
 --根據(jù)游標(biāo)當(dāng)前指向的一條數(shù)據(jù) 
 fetch cur into n,c; 
 --判斷游標(biāo)的循環(huán)是否結(jié)束 
 if done then 
  leave read_loop; --跳出游標(biāo)循環(huán) 
 end if; 
 --獲取一條數(shù)據(jù)時(shí),將count值進(jìn)行累加操作,這里可以做任意你想做的操作, 
 set total = total + c; 
 --結(jié)束游標(biāo)循環(huán) 
 end loop; 
 --關(guān)閉游標(biāo) 
 close cur; 
 
 --輸出結(jié)果 
 select total; 
END; 
--調(diào)用存儲(chǔ)過程 
call StatisticStore();

fetch是獲取游標(biāo)當(dāng)前指向的數(shù)據(jù)行,并將指針指向下一行,當(dāng)游標(biāo)已經(jīng)指向最后一行時(shí)繼續(xù)執(zhí)行會(huì)造成游標(biāo)溢出。
使用loop循環(huán)游標(biāo)時(shí),他本身是不會(huì)監(jiān)控是否到最后一條數(shù)據(jù)了,像下面代碼這種寫法,就會(huì)造成死循環(huán);

read_loop:loop 
fetch cur into n,c; 
set total = total+c; 
end loop;

在MySql中,造成游標(biāo)溢出時(shí)會(huì)引發(fā)mysql預(yù)定義的NOT FOUND錯(cuò)誤,所以在上面使用下面的代碼指定了當(dāng)引發(fā)not found錯(cuò)誤時(shí)定義一個(gè)continue 的事件,指定這個(gè)事件發(fā)生時(shí)修改done變量的值。

declare continue HANDLER for not found set done = true;

所以在循環(huán)時(shí)加上了下面這句代碼:

--判斷游標(biāo)的循環(huán)是否結(jié)束 
if done then 
 leave read_loop; --跳出游標(biāo)循環(huán) 
end if;

如果done的值是true,就結(jié)束循環(huán)。繼續(xù)執(zhí)行下面的代碼

使用方式

游標(biāo)有三種使用方式:
第一種就是上面的實(shí)現(xiàn),使用loop循環(huán);
第二種方式如下,使用while循環(huán):

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執(zhí)行:

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();

游標(biāo)嵌套

在mysql中,每個(gè)begin end 塊都是一個(gè)獨(dú)立的scope區(qū)域,由于MySql中同一個(gè)error的事件只能定義一次,如果多定義的話在編譯時(shí)會(huì)提示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();

上面就是實(shí)現(xiàn)一個(gè)嵌套循環(huán),當(dāng)然這個(gè)例子比較牽強(qiáng)。湊合看看就行。

動(dòng)態(tài)SQL

Mysql 支持動(dòng)態(tài)SQL的功能

set @sqlStr='select * from table where condition1 = ?'; 
prepare s1 for @sqlStr; 
--如果有多個(gè)參數(shù)用逗號(hào)分隔 
execute s1 using @condition1; 
--手工釋放,或者是 connection 關(guān)閉時(shí), server 自動(dòng)回收 
deallocate prepare s1;

以上就是詳解Mysql 游標(biāo)的詳細(xì)內(nèi)容,更多關(guān)于Mysql 游標(biāo)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • MySQL 游標(biāo)的定義與使用方式
  • Mysql 存儲(chǔ)過程中使用游標(biāo)循環(huán)讀取臨時(shí)表
  • mysql聲明游標(biāo)的方法
  • mysql游標(biāo)的原理與用法實(shí)例分析
  • 帶你徹底搞懂python操作mysql數(shù)據(jù)庫(cursor游標(biāo)講解)
  • mysql存儲(chǔ)過程之游標(biāo)(DECLARE)原理與用法詳解
  • MySQL游標(biāo)概念與用法詳解
  • mysql的存儲(chǔ)過程、游標(biāo) 、事務(wù)實(shí)例詳解
  • Mysql存儲(chǔ)過程中游標(biāo)的用法實(shí)例
  • Mysql存儲(chǔ)過程循環(huán)內(nèi)嵌套使用游標(biāo)示例代碼
  • MySQL存儲(chǔ)過程中游標(biāo)循環(huán)的跳出和繼續(xù)操作示例
  • MySQL 游標(biāo)的作用與使用相關(guān)

標(biāo)簽:仙桃 蘭州 湘潭 崇左 銅川 黃山 湖南 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解Mysql 游標(biāo)的用法及其作用》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    镇坪县| 曲阜市| 昭苏县| 敦化市| 宣汉县| 东兴市| 平阳县| 游戏| 广东省| 阿拉善左旗| 尉犁县| 襄城县| 海安县| 南阳市| 斗六市| 江山市| 莒南县| 山丹县| 石家庄市| 溧水县| 淮滨县| 江西省| 武山县| 渭南市| 延安市| 遂宁市| 黑河市| 东辽县| 玉屏| 诸城市| 广德县| 休宁县| 台南市| 建德市| 民丰县| 周宁县| 山丹县| 宁武县| 东源县| 双柏县| 高雄市|